Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

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

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

add comment

3 Answers 3

up vote 13 down vote accepted

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

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

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.

share|improve this answer
add comment

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

share|improve this answer
    
Why did this get a -1? –  Dan Green 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 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
add comment

protected by MadHatter Jul 1 '13 at 13:22

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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