The Case of the Missing Cron

On Tuesday this week a tragedy befell me while I was working on a client’s server. For this particular client, we use cron jobs to schedule a whole bunch of tasks to run at different intervals. This was originally set up a couple of years ago, with only small changes having been made since then.

It was my task to investigate some issues that were occurring in one of these scheduled tasks. So naturally, I wanted to look at the cron file to check on the tasks. To view the cron file on linux you’d type something like this:

sudo crontab -e

I typed this:

sudo crontab -r

Such a small difference, but such a big mistake. the -r flag resets the cron file. So the instant I hit the enter key on my keyboard, the file was destroyed. Gone. Never to be seen again.
I was screwed.

 

Frantically searching for ways of restoring an accidentally deleted cron file. I stumbled across a golden nugget of information. Cron jobs have a log file, and that log file has time stamped records of what commands were run. This was my salvation!

I found the log file at /var/log/cron and was able to reconstruct the original file by examining its contents.

 

TL;DR: Always keep a backup of cron files.