1

I have small check if today is first Monday of month which is like this:

['$(date "+%u")' = "1"] && echo 'trąba'

but I get the error when crontab is sending me an email that something went wrong

/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file

I tried changing '$(date "+%u")' to "$(date '+%u')" but didn't help. The title of the email is ["$(date '+ so I think it has a problem with 1st quote marks but this code works just fine when executed inside the terminal. Maybe someone have better "check" to check for the 1st Monday of month

OS: CeontOS 7

contab -l

* * * * * [ "$(date +%u)" -eq 1 ] && echo trąba
Viszman
  • 113

2 Answers2

1

Fixed POSIX code follows:

[ "$(date +%u)" -eq 1 ] && echo trąba

Errors / warnings / infos were:

  • missing spaces in [ .. ] block

  • apostrophes instead of double quotes

  • equal sign instead of POSIX -eq in test [ .. ]

  • you do not have to quote anything after echo

  • you do not have to quote numbers

  • you do not have to quote that date code


Cron

*/1 * * * * [ "$(/usr/bin/date +\%u)" -eq 1 ] && /usr/bin/echo trąba >> ~/cron-test
  • In order to test this, you may try the above code

  • You may have your date and echo binaries elsewhere on your system, to determine where they are, use which date etc.

  • After modifying your Cron, you can simply use this and sit tight

    tail -f ~/cron-test
    
1

In the command part of a crontab entry, % is translated to a line break before the command is executed, unless it's escaped. So you need to escape the one in the date format string.

Other problems: you need spaces between the [ ] and the things inside them, and $( ) won't expand inside single-quotes. Here's the fixed version:

[ "$(date "+\%u")" = "1" ] && echo 'trąba'
  • 1
    with my knowledge crontab with 0 2 1-7 * 1 will run on everyday AND monday – Viszman Jul 05 '21 at 06:11
  • @Viszman You're right; I checked the man page, and it says "Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., don't start with *), the command will be run when either field matches the current time." So I'll remove that part of my answer. – Gordon Davisson Jul 05 '21 at 06:21