Today I Learned — Retrieving Crontab After Accidentally Delete It using Crontab -R
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.
Next — we searched the way out together, and we came with 2 solutions.
- 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.
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
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!