[LINUX] Love is needed to prevent accidental erasure of cron


I will write an article on Qiita for the first time. I am always indebted to everyone who writes articles for Qiita. Thank you very much.


I think that when I was touching cron on Linux, I almost erased cron. (Some people have erased it ...)

Rather than what to do when it is erased, it is how to prevent it from being erased`.


Be sure to add the -i option to the crontab command. (Prevent deletion)


When editing cron

crontab -e

I think that there are many things to do directly. (Recently, it's been said that if you edit directly, you can't go into the server directly, but it's not all that wonderful environment.)

I'm thrilled when I do -e when the option is specified. This guy is on the right side of the keyboard. r

As you know, if you do crontab -r, all cron will disappear. In the first place, there is a feeling that the option of the keyboard layout is specified as to why an accident should occur, but the reality is that.


1. Examine crontab options

If you run man cron tab (cron description) on the linux command line, it says something like this in English.

-i     This  option  modifies  the  -r  option to prompt the user for a
       'y/Y' response before actually removing the crontab.

I don't understand English a little,

-i option-If you do not answer y or Y before deleting cron with the r option, it will not be deleted

Something like that is written. This guy can be used.

I'll give it a try. (Do not test in production even if you make a mistake)

$ crontab -ir
crontab: really delete hogehoge's crontab?

The hogehoge part is the user name. After that, it is safe if you can exit with "Yabe" and ctrl + c. If you press y or Y, cron disappears, but I don't know if you do that.

Every time

crontab -ie

If you press it, it's OK. Even if you accidentally press the terrifying key to the right

crontab -ir

It's safe because it will be heard at. But it's not smart. It's rather annoying. In the first place, there seems to be no problem with -i. (In the case of -l, even if you write -il, you will not be asked "Are you sure you want to erase it?")

2. Alias

Linux has an alias. Please check another site for details. I immediately read it in romaji and remembered a baseball player (I'm a fan of the Kanagawa baseball team).

In this example

alias crontab='crontab -i'

And run If you run crontab, you've done crontab -i It means that we were able to declare.

Then where do you declare this guy?

3. Set to ~ / .bash_profile

I have set it to ~ / .bash_profile. If you write it here, it will be executed automatically when you log in to the black screen with ssh.

There is also a ~ / .bashrc, and even if you write it here, it will be executed without permission. Which one to write depends on the individual's judgment.

Here's why I wrote it in ~ / .bash_profile: When you execute man bash (description of bash)

       The personal initialization file, executed for login shells
       The individual per-interactive-shell startup file

It is written. I don't translate. This time

So I decided to add it to ~ / .bash_profile.


Here's what to do: Add  Ľalias crontab ='crontab -i'to~ / .bash_profile` (Settings are required for each user) We are not responsible for anything that happens, so please test it.


Reduce the amount of time you get excited about cron.

Recommended Posts

Love is needed to prevent accidental erasure of cron
Setting to output the log of cron execution
Use decorators to prevent re-execution of data processing