Search code examples
linuxcrondebian

cron script not running with root crontab


I am testing a cron script in my server (Debian 12). Testing a very simple test script that echo a line in a file:

If I use crontab -e, scripts runs fine with my user. The actual script will need to run as root though, so I tried to install the test script it with sudo crontab -e. In this case, the script never runs. I tried also to put the scripts in /etc/cron.d, still not working. Tried also with sudo crontab -e -u root, not working. No errors, no logs in /var/log.

The only logs that I have found are on the journal, but they're pretty useless:

Jan 26 09:45:01 server-1 cron[607]: (root) RELOAD (crontabs/root)
Jan 26 09:45:01 server-1 CRON[15437]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Jan 26 09:45:01 server-1 CRON[15441]: (root) CMD (/bin/bash -c "/tmp/test.sh")
Jan 26 09:45:01 server-1 CRON[15439]: (CRON) info (No MTA installed, discarding output)
Jan 26 09:45:01 server-1 CRON[15439]: pam_unix(cron:session): session closed for user root

Solution

  • By adding > /tmp/test.log 2>&1 to the cron script, it is possible to print the script output in a file.

    * * * * * /bin/bash -c "/tmp/test.sh" > /tmp/test.log 2>&1
    

    This makes much easier to debug it. In this case it was that the root user could not find the executable since it was not in PATH.