Today I Learned — Retrieving Crontab After Accidentally Delete It using Crontab -R

Roberto Tambunan
Mar 2 · 2 min read

Today was a quite as usual day. It’s already 7pm and I wanted to close my laptop and take my dinner. Suddenly, one of my friend told me that he accidentally run command crontab -r instead of crontab -e to update our cronjob in one of our server. Then, we panicked together.

Photo by Richard Brutyo on Unsplash

Next — we searched the way out together, and we came with 2 solutions.

  1. Trying to find cron log via syslog
grep CRON /var/log/syslog

With this command, we can see all system logs that have been run by our system. The grep function will only catch the log that has word cron in it.

grep CRON /var/log/syslog

From these logs, we can find that command /tmp/test/hello_world.sh runs every 1 minute. Using this information we can fetch all of the commands that we find here, and rewrite them to existing crontab.

2. Trying our luck by checking /tmp/

ll /tmp/ | grep crontab
ll /tmp/ | grep crontab

Many programs don’t directly write to the actual file. Instead, programs will write to /tmp/ file first, and when they finish updating, they will do some steps to check and validate (depend on the program) then finally write to the actual original file.

One of the example cases for crontab is: if there are conflicting changes in crontab (you are trying to update the file while the other is doing the same), there will be new directory inside /tmp/ with prefix crontab.* to store your version of the update.

If luck is with us, somehow in our case - luck is on our side, we can find the old file here. Using those old files, we can rewrite our existing crontab.

After having this case, having regular crontab configuration backup seems a good idea— just write it in the crontab configuration. Then, the cron can save itself in the future. 😂

Finally, we were able to have our crontab configuration back. I finally closed my laptop and had my dinner.

Have a good day everyone!

Easyread

Easy read, easy understanding.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store