3
$ sudo cat /etc/crontab
# ...comment lines elided... 
0 0,12 * * * root sleep 1347 && certbot renew -q --post-hook 'sudo reboot'

sudo crontab -e shows

13 0,12 * * * root sleep 1347 && certbot renew -q 

This VM is a simple default-configured Google Compute Engine VM with Debian Buster. I am logged in as a user joshua, but note that sudo does not require entering a password.

Which one (or perhaps both?) shows what cron actually does? What is the recommended way to edit the cron config, crontab -e or directly editing some file, e.g. with sudo vi?

Joshua Fox
  • 611
  • 6
  • 12
  • Please [edit] your question and clarify. What is the first crontab you show? And are you _sure_ the second is what you see with `sudo crontab -e`? There shouldn't be a `user` field in that file. It looks like both lines you show are actually from `/etc/crontab`. What do you mean by "what cron actually does"? Cron will run everything it is told to run. And no, `sudo crontab -e` will show root's user-crontab, not yours. – terdon Feb 10 '22 at 11:35
  • I edited it (the first line had been "swallowed" by triple tilde. – Joshua Fox Feb 10 '22 at 11:54
  • Thank you. Does "everything it is told to run" include both the configurations above? – Joshua Fox Feb 10 '22 at 11:56
  • Thanks! So, the output of `sudo crontab -e` is wrong, only `/etc/crontab` should have a user as the 6th field, so the line should be `13 0,12 * * * sleep 1347 && certbot renew -q`. – terdon Feb 10 '22 at 11:57
  • "are you sure the second is what you see with sudo crontab -e?" Yes, I just retried. "There shouldn't be a user field" This is a simple default-configured Google Compute Engine VM, I am logged in as a user joshua, but note that sudo does not require entering a password – Joshua Fox Feb 10 '22 at 11:57
  • "sudo crontab -e will show root's user-crontab, not yours." OK, so what is the difference between these two configurations? – Joshua Fox Feb 10 '22 at 11:58
  • "the output of sudo crontab -e is wrong." OK, so that probably means that edited it-- wrongly. – Joshua Fox Feb 10 '22 at 11:59
  • `crontab -e` verifies your edits, refuses to install a crontab with errors, and signals the cron daemon that it should refresh its internal copy of the new file. `vi crontab` does none of those things, and is inviting disaster. Also, `crontab -l` will list your crontab file. Hint: crontab default editor is `nano` on my box -- `EDITOR=vi crontab -e` is my friend here. – Paul_Pedant Feb 10 '22 at 14:35
  • Thank you. So there is never a need to edit `/etc/crontab` – Joshua Fox Feb 10 '22 at 15:09

1 Answers1

7

The commented lines in /etc/crontab will tell you:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

/etc/crontab and /etc/cron.d/* define the system-wide crons. They have a user field in front of the command to be executed.

With sudo crontab -e, you edit root's crontab file which is located at /var/spool/cron/crontabs/root (for debian, different location in other OS).

Note, that you should never edit the files directly, but use crontab -e command.


The user's crontab file should not include the user name, so it should read:

13 0,12 * * * sleep 1347 && certbot renew -q 
terdon
  • 234,489
  • 66
  • 447
  • 667
pLumo
  • 22,231
  • 2
  • 41
  • 66