Cron task doesn't run [duplicate]

I have this crontab task:

$ sudo crontab -l  -u root
0 22 * * * certbot renew --cert-name app.my_website123.net --pre-hook "systemctl nginx stop" --post-hook "systemctl nginx start" >> /home/my_user/cron_log1.log 2>&1

certbot renew requires root.

The log file still doesn't exist, after several days, which implies that the task doesn't get executed. Why is that?


Solution 1:

Your crontab entry looks fine, with one possible exception: certbot should probably have a full path specification. For example:

0 22 * * * /path/to/certbot .....

If that doesn't resolve the issue, we'll need more information. I suspect the issue is the environment, but we'll need to ask cron what environment has been created for the root user. We can phrase our question as follows:

  • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):
$ nano ~/envtst.sh
  • Enter the following in the editor, after adjusting for your system/user:
#!/bin/sh 
/bin/echo "env report follows for user "$USER >> /home/you/envtst.sh.out 
/usr/bin/env >> /home/you/envtst.sh.out 
/bin/echo "env report for user "$USER" concluded" >> /home/you/envtst.sh.out
/bin/echo " " >> /home/you/envtst.sh.out
  • Save the file, exit the editor and set file permissions as executable.
$ chmod a+rx ~/envtst.sh
  • Run the script you just created, and review the output in /home/you/envtst.sh.out. This output will show your current environment as the $USER you're logged in as:
$ ./envtst.sh $$ cat /home/you/envtst.sh.out
  • Open your crontab for editing:
$ crontab -e -u root
  • Enter the following line at the bottom of your crontab:
* * * * *  /home/you/envtst.sh >> /home/you/envtst.sh.err 2>&1

The output file /home/you/envtst.sh.out will contain a listing of the environment for the "root cron user". Once you know that, adjust your crontab entry for certbot accordingly. Post the output here if questions remain.