21

My colleague ran grep | crontab. After that all jobs disappeared. Looks like he was trying to run crontab -l.

So what happened after running the command grep | crontab? Can anyone explain?

TJennings
  • 103

2 Answers2

33

crontab can install new crontab for the invoking user (or the mentioned user as root) reading from STDIN. This is what happended in your case.

grep without any option will generate an error message on STDERR as usual and you are piping the STDOUT of grep to STDIN of crontab which is blank hence your crontab will be gone.

heemayl
  • 56,300
  • 4
    Some versions of crontab require you to use - as the filename to read from standard input. I assume this is because too many people blew away their crontabs with mistakes like this. – Barmar Aug 19 '15 at 19:34
4

How did he terminate the job? Did he type C-c or C-d? If he typed C-d, then it's equivalent to running crontab < /dev/null and you have replaced the user's crontab file with an empty one. On the other hand, if you kill crontab with C-c, then the crontab might have been preserved, but you can easily check that by running crontab -l.

All this program does is edit the crontab files in /var/spool/cron/, so if you happen to have a backup of the file system, you can just restore the user's crontab file from there.

update

I didn't see that there was no argument to the grep, so grep will error out and indeed the crontab file will be blown away always.

user3188445
  • 5,257