22

I have accidentally wiped out all my cron jobs. I'm not sure what I have done. I don't remember deleting entries from it by issuing crontab -e. What are the possible ways that the cron jobs can be completely wiped out?

4 Answers 4

31

If you type crontab with no arguments, it reads a crontab from standard input. If you then type Control-D, it will create an empty crontab, overwriting your previous crontab. (Control-C aborts the command and leaves your crontab alone.)

jpmath's answer correctly points out that crontab -r will also wipe out your cron jobs (that's what it's for).

I avoid this by (almost) never using crontab -e (edit crontab) or crontab with no arguments (which reads from stdin). Instead, I keep my crontab entries in a separate file, which I maintain in a source control system, and run the crontab command with that file name as an argument. (I'll occasionally use crontab -e to make temporary changes.)

(I temporarily clobbered my own crontab while writing this answer, knowing that I could recover it.)

2
  • 4
    If you didn't already save a copy of it, none that I know of. The system probably keeps your crontab in /var/spool/cron/crontabs/foo, where foo is your user name; if you have some way of recovering a previous version of that file, you might have a chance. In other words, it comes down to whether you can recover old deleted files in general. Do you have backups? Commented Mar 15, 2012 at 4:11
  • @JasonYeo you might be able to recover it using PhotoRec (based on TestDisk), but if that section of the disk was overwritten, it's possible you may not be able to recover the file (the filesystem you use will also affect this). Commented Jul 31, 2013 at 17:44
9

If you type crontab -r instead of crontab -e by mistake (e and r are next to each other), your crontab will be removed as well.

1
  • 1
    yup, exactly what I did. Kicking myself now. Luckily I always backup my crontabs. Why, oh why, did they pick 'e' and 'r' options - the keys are so close and so easy to fat-finger???
    – Denis
    Commented Apr 30, 2020 at 21:45
0

Here's how I confirmed why I lost my crontab file:

[me@myUbuntuPC ~]$ history | grep crontab
...
197  crontab
198  man crontab
198  crontab -e
...

And there's the evidence. My employer used to call that "Learn After Doing"!

I can confirm that under Ubuntu 12.04 and 18.04 (so I assume versions in between), if you type crontab by itself, and hit ctrl-C it also wipes your crontab.

I don't use crontab very often, and while setting up a new machine, my faulty memory told me to type crontab on the old one to view the contents (having forgotten it's crontab -e), I then said "Oh, that's not right. Abort!" and hit Ctrl-C. No more crontab! D'oh!

Then I reviewed the man pages, my notes and/or Google... (L.A.D. as we used to say.)

Not sure if other distros act like this with ctrl-C.

So now I added notes inside my crontab file reminding me to "crontab -l > crontab.backup" to save, and "crontab crontab.backup" to restore. Now, when it's time to build a new Ubuntu 22.04LTS, I'll look at crontab.backup, hopefully read the notes, and remember enough to do it right!

I just tested with a LinHES system (Arch based MythTV) and crontab 4.5 gives you the following:

[me@myLinHESpc ~]$ crontab
crontab 4.5
crontab file [-u user]  replace crontab from file
crontab -  [-u user]    replace crontab from stdin
crontab -l [-u user]    list crontab
crontab -e [-u user]    edit crontab
crontab -d [-u user]    delete crontab
crontab -c dir <opts>   specify crontab directory

I like this much better.

Arch (and LinHES) are rolling upgrade distros, so this would reflect the latest versions in the repos as of mid-2018.

So, chances are, you wiped your crontab by typing crontab by itself, and you don't have crontab 4.5!

I've had years of experience running Linux, but Arch has taught me I know very little about it!

0

I accidently did the same today by running just crontab after that the whole crontab was empty, iam happy that I have a backup so did crontab - e and put the content of the backup in it, I hope this is enough?

Is there anyway to prevent this from happening again, maybe blocking crontab without any parameter on it?

Thanks in advance.

3
  • 1
    Create a wrapper script around crontab that aborts when no parameter is specified and put it in some directory on PATH where it will be found before the real crontab.
    – raj
    Commented May 7, 2021 at 8:08
  • You mean something like crontab-edit.sh that calls crontab -e and if no parameter ($#) is giving exit 1?
    – Omexlu
    Commented May 7, 2021 at 8:14
  • 1
    Not quite. Call the script crontab and put it in such folder that it is found before the real crontab. If there are parameters, the script should just call the real crontab (by full absolute path) with exactly the same parameters. If there are no parameters, write an error message and exit 1.
    – raj
    Commented May 7, 2021 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .