Wednesday, May 25, 2022

[SOLVED] Cron Not Running Tasks Ubuntu 18.04

Issue

I've searched everywhere and spent hours trying various solutions, but nothing seems to work... As the title explains, cron simply won't execute commands on my Ubuntu 18.04 machine.

My /etc/crontab file looks like this:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
*/1 *     * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

My test script is a simple echo 'Hello world' script (named echo), which is contained within /etc/cron.hourly:

#!/bin/sh
echo 'Hello world'

Calling run-parts /etc/cron.hourly from any directory prints 'Hello world' to the console as expected, but the task never seems to be executed by cron. I also have a script that automatically backs up a MySQL database in cron.hourly. This also executes and produces the expected file when calling run parts, so it isn't just an issue of cron not showing echo calls. Cron is clearly doing something, however, as I get the following output in /var/log/syslog:

...
Mar 25 03:05:01 ip-172-31-32-110 CRON[10077]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 25 03:06:01 ip-172-31-32-110 CRON[10108]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 25 03:07:01 ip-172-31-32-110 CRON[10122]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 25 03:08:01 ip-172-31-32-110 CRON[10149]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 25 03:09:01 ip-172-31-32-110 CRON[10162]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

Some other things I have tried:

  • sudo crontab -e and creating a new cron command there (this produces same output in /var/log/syslog that /etc/crontab does)
  • using #!/bin/bash instead of #!/bin/sh/
  • sudo service cron restart
  • Made sure an empty line exists at the bottom of crontab
  • sudo chmod 755 echo to ensure the file is executable

This has been giving me a huge headache, so I'd greatly appreciate any help on the matter!


Solution

Okay, I feel silly for not having tried this prior to posting. Using crontab -e (without sudo) and placing the command there solved the issue.

I'd still appreciate if anybody could explain the reason behind this.



Answered By - Riley
Answer Checked By - Dawn Plyler (WPSolving Volunteer)