Crontab disappeared - help!

Posted by Andy Huggins on September 28, 2016

As most of us work, we get used to typing things and usually assume that we will never make a mistake right? We, and I definitely mean me, should probably slow it down just a little bit and be sure to read the commands we type into the terminal before slapping that enter key.

I recently encountered an issue where I needed to edit a cron job command, so of course I logged into the server, quickly typed `sudo bash` entered my password, then typed `crontab -r`....nothing happened, was just given a new prompt. Retype `crontab -e` and Vim opens, but the file is empty.

What the heck?

I logged out of the server, re-logged in, thinking that maybe I somehow messed something up along the way. I typed `crontab -e`, Vim opens and the file is empty.

That sinking feeling sets in, "did I somehow delete the does that even happen?" And immediately, I think "why would it not prompt you to ask 'are you sure?' when you could potentially delete the crontab????"

I asked a co-worker, who looked in the logs...I should have thought to do that, but didn't. He says "looks like the crontab was deleted."


I look back through my terminal history and see that I ran `crontab -r` (did you notice it above?) Yeah, that deletes the crontab for the user you currently are, in my case root.

This is particularly annoying considering that 'e' and 'r' are directly next to each other on the keyboard...I wonder how many hours have been lost by this?

Luckily, in this case I only lost two jobs that I was familiar with and could easily rebuild...sidenote, if you need to recreate your crontab, you might be able to do some reverse engineering from the `/var/log/cron` file. There you basically get a log of what the cron runs and when. 

You could also....and very much SHOULD...create a backup of your crontab just in case this happens. You might even mention it in the README of your project or keep that copy in your version control repository.

If you have control of the server, you could also alias `crontab=crontab -i` for your terminal...this prompts the user if you try to delete the crontab. Which in my opinion should be the default behavior. But at least this way you could enable that functionality if you want to.

Thought I would mention it in the off chance someone else experiences this, and by writing this, I will be more mindful of what could happen with the smallest of typos.