Search code examples
linuxbashcron

crontab - conditional task execution


i'm trying to run following command from cron:

[[ -d some_folder_path ]] && [[ $(date +%u) -lt 6 ]] && touch ~/testOK

Unfortunately it's not working although when I run it outside the cron it works perfectly.

Kind request for help solving the issue.


Solution

  • First thing you have to make sure that your SHELL variable is set to Bash. crontab has special meaning for % so you have to escape it using "\"

    so I tested your cronjob it should be like

    * * * * * [[ -d /Users/mkhade/Downloads ]] && [[ $(date +\%u) -lt 6 ]] && touch ~/testOK