[LINUX] Let's execute commands regularly with cron!


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.

What is 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.

How do you feel when you run it regularly?

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)

Specific setting method

There are two main ways to do it.

Set with the crontab (cron table) command

  1. Log in as the user you want to execute and execute the following command in the shell

    crontab -e
  2. 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)


  1. Become root with su command

  2. 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)
  3. Restart crond

    /etc/rc.d/init.d/crond restart

For those who do not know what command to execute

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) スクリーンショット 2020-12-30 4.32.14.png

Referenced, helpful site

-[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

Let's execute commands regularly with cron!
Periodically execute commands with crontab
Execute Python script with cron of TS-220
AWS-Perform web scraping regularly with Lambda + Python + Cron
[Python] Let's execute the module regularly using schedule
Periodically execute python script with cron in venv environment
[Linux] Execute git pull regularly with the crontab command
Scripting with Paver-External Commands
Let's execute the command on time with the bot of discord
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Manage cron jobs with python
Let's run Excel with Python
Let's make Othello with wxPython
Let's play with Amedas data-Part 4
Let's make dice with tkinter
Let's write python with cinema4d.
Let's do R-CNN with Sklearn-theano
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Let's build git-cat with Python