6

I was trying to create a cron on the production environment and so I created crontab.txt file with my new cron command and ran crontab crontab.txt file but instead of appending to existing cron job list, it accidently deleted all crons and now I only have my cron on prod.

How can I retreive earlier crons that were set and append my new cron to it.

Thanks.

migrated from stackoverflow.com Sep 30 '10 at 16:41

This question came from our site for professional and enthusiast programmers.

16

Would it add insult to injury to suggest restoring your crontab from backups?

  • 6
    Not at all. "Restore from backup" is the correct answer. +1 – jscott Sep 30 '10 at 16:42
  • 1
    Is it appropriate to suggest that /var/spool/cron/* ? – Craig Hyatt Jul 5 '11 at 18:11
7

First of all: Always make backups!

Second:

You can try to have a look at /var/log/cron and build your crontab again.

  1. Have a look at the logfile
  2. Take the first command and do grep commandname1 /var/log/cron
  3. Figure out the systematic behind the times the command was run
  4. put that entry to your new crontab
  5. execute cat /var/log/cron | grep -v commandname1
  6. Take the next command and grep for it
  7. Figure out the systematic behind the times the second command was run
  8. put that entry to your new crontab
  9. execute cat /var/log/cron | grep -v commandname1 | grep -v commandname2
  10. Repeat until you covered all commands

Now you should also execute the grep on older cronlogs to find out jobs that might be run only once in a month.

5

I made a PHP script that does a full recovery of your crontab, based on the log.

It outputs a single instance of every cron command run by the user for the last week.

I put it here

https://github.com/dangreenisrael/recover_crontab

Here is a sample output:

perl ~/sorttv/sorttv.pl

/usr/local/bin/flexget

bash ~/scripts/sort_sports.sh

~/scripts/play_recently_added.sh

  • Why did this get a -1? – Dan Green-Leipciger Jul 1 '13 at 14:48
  • I'll be is that is because it does not answer the immediate problem, not what to do in the future. However, this is only a guess on my part. – mdpc Jul 1 '13 at 16:34
  • I had this problem myself today, and the best answer I could find was sort through 800 lines of log file. So I made a PHP script that sorted through the log and isolated all of cron commands. After I solved my own problem, I uploaded the file to github and added a readme so that others could benefit from it. Then I got a -1/ – Dan Green-Leipciger Jul 1 '13 at 17:44
  • You should have added that it recovers the crontab using the log files in the original answer. – mdpc Jul 1 '13 at 17:52
  • 2
    Hi, I just looked and only found the PHP script. Did not find the sh scripts mentioned in your output. One can also simply: grep CRON /var/log/syslog|grep root|cut -d ":" -f 4-9|sort -u --- now a great wok would be to analyze dates and times of commands ran, and regenerate propper crontab with propper timmings based on the timespan of the log – cusco Nov 23 '15 at 17:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.