I entered crontab -r
instead of crontab -e
and all my cron
jobs have been removed.
What is the best way (or is there one) to recover those jobs?
I entered crontab -r
instead of crontab -e
and all my cron
jobs have been removed.
What is the best way (or is there one) to recover those jobs?
crontab -r
removes the only file containing the cron jobs.
So if you did not make a backup, your only recovery options are:
/var/log/cron
. The file will help you rewrite the jobs again. grep CRON /var/log/syslog
If you have no /var/log/cron
file you can recover the commands (but not the timings) from the syslog
.
grep 'CRON.*(yourusername)' /var/log/syslog
you can then figure out most timings by looking at the datestamps.
/var/log/syslog
stores last day logs or less. In my case I needed sudo zgrep 'CRON.*(yourusername)' /var/log/syslog*
– Kipras Kančys
Nov 20 '20 at 09:02
It sucks, but if you run crontab -r
your crontab is gone forever. And unless you have a backup of it somewhere, you must grep through syslog files to get an idea of what/when jobs were being run, and recreate.
A good trick to avoid such problem is to add the following line to your crontab:
@daily crontab -l > $HOME/.crontab
This way your crontab is backed up every day to $HOME/.crontab
, so if you delete it by accident, a relatively recent copy is available and can be installed by:
crontab < $HOME/.crontab
As an additional preventative measure in addition to backing up data, as mentioned by @anishsane, the -i
flag will "prompt before deleting user's crontab". So, if you run crontab -i -r
, it will give a nice
crontab: really delete <user>'s crontab? (y/n)
At which point you can select y or n. Of course, you don't want to type that out every time, so put this in your bash config and forget about it:
alias crontab="crontab -i"
As the -i
flag doesn't affect any other command
vi /var/spool/cron/*user*
or if you're the root
user then vi /var/spool/cron/root
cron
that the file has been edited. It also won't work when the user has accidentally deleted their crontab
because there is no file to edit.
– Chris Davies
Jun 23 '15 at 13:06
crontab -e
is a really common cron command. – JustinP May 11 '15 at 15:52crontab -e
consider using a variation of this process (assuming$HOME
directory):crontab -l >.crontab ; vi .crontab ; sleep 2 && crontab .crontab
and thereaftervi .crontab ; sleep 2 && crontab .crontab
. I have an extension tovi
that returns status indicating whether or not the file contents changed during the edit. I can thenifvi .crontab && crontab .crontab
. (But that extension is out of scope for a comment.) – Chris Davies Jun 23 '15 at 13:10ls *crontab*
in yourundodir
should pull up a list of files you can parse (lots of gibberish, but lines I cared about were in plaintext). – SnoringFrog Sep 04 '15 at 18:42e
for edit andr
for remove with absolutely no prompt whatsoever!! – DaniG2k Sep 07 '15 at 14:17alias crontab=crontab -i
. But crontab should have made that default, given that e & r are next to each other... – anishsane Dec 08 '15 at 06:570 0 * * * /usr/bin/crontab -l > ~/.crontab.bak
– anishsane Dec 08 '15 at 06:59crontabs
locally or always followcrontab -e
withcrontab -l >$HOME/etc/crontabs/crontab.$(tshhmmss)
.tshhmmss
is adate
alias producing2016-Nov-29-124124
unique to-the-second timestamps. – waltinator Nov 29 '16 at 17:42alias crontab='crontab -i'
tonano ~/.bashrc
. At least this is the login script for Debian. – Mint Dec 03 '22 at 03:49