96

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?

14
  • 55
    Is it just me or wouldn't it make more sense for crontab -r to ask yes/no by default?? Feb 16, 2015 at 22:54
  • 13
    I think having a yes/no prompt would be a great idea. especially since e & r are right next to each other.. and crontab -e is a really common cron command.
    – JustinP
    May 11, 2015 at 15:52
  • 8
    In 7 or so years of admin work I have never had this happen to me. Now I am scared. Time to start backing up the crontabs regularly.
    – Caja
    Jun 23, 2015 at 13:35
  • 12
    This has happened to me twice already. It's the worst idea ever to have e for edit and r for remove with absolutely no prompt whatsoever!!
    – DaniG2k
    Sep 7, 2015 at 14:17
  • 17
    First thing to do, alias crontab=crontab -i. But crontab should have made that default, given that e & r are next to each other...
    – anishsane
    Dec 8, 2015 at 6:57

5 Answers 5

79

crontab -r removes the only file containing the cron jobs.

So if you did not make a backup, your only recovery options are:

  • On RedHat/CentOS, if your jobs have been triggered before, you can find the cron log in /var/log/cron. The file will help you rewrite the jobs again.
  • Another option is to recover the file using a file recovery tool. This is less likely to be successful though, since the system partition is usually a busy one and corresponding sectors probably have already been overwritten.
  • On Ubuntu/Debian, if your task has run before, try grep CRON /var/log/syslog
5
  • I could not voted you up but this answer was some how useful for me. thankx Jun 10, 2014 at 8:24
  • 1
    @RajaRassani You can mark the answer is a correct one with a tick mark, though. Thanks for feed back! Jun 10, 2014 at 8:31
  • this just happened to me :) total bummer.. I do use virtual servers and backup them up regularly..so I'm going to spin up a backup.. and get the deleted crontabs
    – JustinP
    May 11, 2015 at 15:49
  • +1 for being straightforward :}
    – namezero
    Apr 13, 2018 at 17:54
  • You can try your luck looking for a backup made by the editor in /tmp/crontab.wpMhKS (of course the name may change). It may not be the most recent version but in my case it helped a lot
    – Jong Bor
    Aug 3, 2018 at 14:11
26

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.

6
  • 1
    On ubuntu the file is /var/log/syslog. Nov 29, 2016 at 13:02
  • @ Aurelijus Rozenas I accidentally added sys to the directory path it should be /var/log/syslog, I corrected my post.
    – Meow
    Nov 29, 2016 at 17:32
  • 2
    grep 'CRON.*(yourusername)' /var/log/syslog
    – aidan
    Dec 15, 2016 at 22:52
  • @Aidin you are my savior! Note to self: Never edit the crontab file while server space is full.
    – Shadoath
    Feb 6, 2017 at 17:55
  • 2
    Phew that was close! I've now added a cron to backup my crontab everyday :)
    – SidJ
    Jul 5, 2019 at 6:20
5

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
1

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

1
  • Good suggestion but this doesn't really work if you wanted execute -e but instead mistyped to -r 😀
    – Toms Bugna
    Sep 30, 2022 at 7:55
-7

vi /var/spool/cron/*user* or if you're the root user then vi /var/spool/cron/root

2
  • 5
    This is worse. It edits the spool files without notifying 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.
    – roaima
    Jun 23, 2015 at 13:06
  • Upvote from me for actually stating the location of the crontab. I had an entire system backup and could easily retrieve my cronjobs. Thanks!
    – Cookie
    Dec 18, 2015 at 14:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.