Recently, I've wanted to run batch files and Python programs on Linux on a regular basis, and I've investigated how to do that, so I'll write an article as a memorandum. I think there are several ways to run it regularly, but this time I used cron.
It is a type of resident program (daemon) that is used as standard in many UNIX-based OSs (Linux, etc.), and periodically starts the specified program according to the schedule set by the user.
If you specify the program, command, shell script, etc. you want to execute with the crontab (cron table) command, the execution date and time, and the execution user, the text file (crontab file) with the same name is saved and the settings are made. (* There is also a method to directly rewrite the crontab file in "/ etc/crontab".)
When the above settings are completed, crond (cron daemon), which is one of the daemons resident in the system, will execute the specified program at a fixed date and time according to the schedule written in the crontab file. It is done.
The execution schedule can be specified in minutes, and you can specify hourly, daily, weekly, monthly, restart time, etc., and by enumerating the applicable values or specifying the range, "every Monday, Wednesday, Friday" You can also specify "midnight" and "0 and 30 minutes every hour from 9:00 to 17:00".
By the way, the crontab file is also in each user's home directory, and if you change it, it will be executed by specifying the user. (↑ Users without administrator privileges cannot perform processes that require administrator execution)
There are two main ways to do it.
Log in as the user you want to execute and execute the following command in the shell
crontab -e
The setting file is read by the default editor, so write and save the following settings.
[minute hour day month dow(Day of the week) command]
0 7 * * * (Describe the command or program you want to execute)
Become root with su command
Edit/etc/crontab with an editor. Just add a line like this at the end
[minute hour day month dow(Day of the week) user command]
00 07 * * *(Execution user)(Describe the command or program you want to execute)
Restart crond
/etc/rc.d/init.d/crond restart
There is a site that can be created by pressing the registration command on the website. [UNIX CRONTAB configuration helper] (http://www.japan9.com/cgi/cron.cgi)
-[Regular execution method 3 patterns and examples of their use] (https://qiita.com/shuntaro_tamura/items/c759bc318859a1203211) -[Try touching cron] (https://qiita.com/katsukii/items/d5f90a6e4592d1414f99) -[Automatically execute scripts on a regular basis] (https://qiita.com/msrks/items/6a180e03d7af622f2101) -[About automatic program execution (cron)] (http://tsuttayo.jpn.org/crond/)
Recommended Posts