0

I realise that there are plenty of cron questions on the site and I've spent the last hour searching for a solution but haven't been able to find one. Essentially the cron job does execute, but it doesn't seem to execute or at least output the output to a file that I specify.

I can see that cron is running:

josh@lin-ldn-cen:~$ pgrep cron
734

The cron job executes successfully, as seen in my /var/log/cron file:

Sep 26 08:34:01 lin-ldn-cen CROND[82848]: (josh) CMD (/home/josh/test_script.sh > /home/josh/cron_logs/test.log)

I've used absolute paths as well as setting the PATH in my crontab file. I've included a newline at the end of my crontab file:

josh@lin-ldn-cen:~$ crontab -l
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin

0 */6 * * * /home/josh/youtube-downloads/download_djdj.sh > /home/josh/cron_logs/cron_download_djdj.log
*/5 * * * * /home/josh/youtube-downloads/download_djdj.sh 2>&1 /home/josh/cron_logs/cron_download_djdj.log
* * * * * /home/josh/test_script.sh > /home/josh/cron_logs/test.log

(Having the path at the top hasn't made any difference at all to the execution of my script)

test_script.sh is incredibly simple, and I used absolute paths there too - here it is:

josh@lin-ldn-cen:~$ cat test_script.sh
#!/bin/bash/

/usr/bin/echo Hello world

I've also tried several other things - including a . before my script in the crontab file, using 2>&1 to output errors, double checked paths, double checked that the .sh file is executable with chmod +x test_script.sh, included both sh and bash before the command in my crontab file - none of this seems to work. All I'm expecting from this is to see 'Hello world' in /home/josh/cron_logs/test.log.

2
0

I've since realised that the .sh file was executing - it was just the output that was not executing.

I fixed this issue by adding SHELL=/usr/bin/bash to the top of my crontab, and by changing the relevant line to use &>> to output to a log file:

* * * * * /home/josh/test_script.sh &>> /home/josh/cron_logs/test.log

Credit to this answer - I found it after I realised that the problem was isolated to the output to file segment of my command.

3
  • On Ubuntu, /bin/sh is the same as /bin/bash or /usr/bin/bash. So that's not the reason why this didn't work. The issue may be the line SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin. As this is written in a single line (and should be in two separate lines - or not present at all, as these are basically the defaults cron assumes), this may mess up things. By the way, your second command in the file contains an error, it should be: /home/josh/youtube-downloads/download_djdj.sh > /home/josh/cron_logs/cron_download_djdj.log 2>&1 – raj Sep 26 '20 at 11:48
  • @raj Please consider writing an answer - I agree with almost everything except your first assertion (afaik /bin/sh is the same as /bin/dash in all current Ubuntu releases, and /usr/bin/bash may or may not exist at all, depending whether /bin is symlinked to usr/bin). – steeldriver Sep 26 '20 at 12:19
  • I was so used to the fact that /bin/sh was for long time symlinked to /bin/bash that I didn't ever check it when I installed the new Ubuntu version... You are right, /bin/sh is /bin/dash now. I wonder what are actual syntax differences between bash and dash, ie. how big is the chance that a script written with bash in mind won't work under dash... – raj Sep 26 '20 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.