How do I disable a specific command, for example crontab -r?

It happened to me twice already that I accidentally run that, because my E key is next to the R key. That little typo is enough and your crontab is gone.

  • 2
    Don't forget to make backups of important files!
    – wjandrea
    Jan 12 '17 at 18:54
  • Sure, I have @daily zip -r /home/./scripts/cronjobs-all.zip /var/spool/cron/crontabs
    – user66638
    Jan 12 '17 at 19:06
  • A source code revision control system like Mercurial or Git would be even for managing script files and safe-guarding against accidental alteration or deletion. You don't even need a remote repository for either of the two. :-) Jan 15 '17 at 22:48

I suggest that you include

alias crontab="crontab -i"

in your ~/.bashrc file (start a new shell before testing!)

This means that every time you run crontab, you always select the "-i" option. If you now give the crontab -r command, it is processed at crontab -ir, which prompts before removing the crontab file:

nick@serv2:~$ crontab -r
crontab: really delete nick's crontab? (y/n) n
  • 1
    $ crontab -i gives Unknown option: i on my webhost
    – Ozh
    Apr 20 '20 at 22:12

Use a wrapper around the crontabcommand, for example this function would do:

crontab () { [[ $@ =~ -[iel]*r ]] && echo '"r" not allowed' || command crontab "$@" ;}

This function checks if -r exists in the argument of crontab; if so, exits with the message "r" not allowed, otherwise executes the command.

Put it in your ~/.bashrc to get it loaded upon initialization of all interactive bash sessions.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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