川崎のシステム開発・アプリ開発・Web制作

How to Scheduling Execution Python Script with Cron

What is Cron

Cron is a software utility that allows us to schedule tasks on Unix-like systems.

The actions of cron are driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file that only system administrators can edit.

Cron commands

crontab -e : edit, modify, create crontab
crontab -l : list of crontab
crontab -r : remove, delete crontab

Cron structure

One cron jobs require to have 2 part:
First is time to schedule task with 5 fields : * * * * * (between each ” is a space) with :
1st ‘‘ is represented for a minute (0-59)
2nd ‘
‘ is represented for an hour (0-23)
3rd ‘‘ is represented for the day of the month (1-31)
4th ‘
‘ is represented for the month (1-12)
5th ‘*’ is represented for the day of the week (0-6, Sunday to Saturday)

The second part is the command to execute, for example, you want to run a python file then the command is :python3 file_path/filename.py

Cron expression

Comma ( , ) is used to separate items of a list. For example, run python file at 20 minute and 50 minute at every hour: 20,50 * * *

Hyphen (-) to determine the range of time when setting several execution times of a single command. For example, 0 0-5 14 * * * -> Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day

Slash (/) for creating predetermined intervals of time in a specific range. For example : */30 * * * * -> 30 every minute every day

Last (L) for the specific purpose to determine the last day of the week in a given month. For example, 3L means the last Wednesday.

Weekday (W) to determine the closest weekday of a given time. For example, 1W means if the 1st is a Saturday, the command will run on Monday (the 3rd).

Hash (#) for determining the day of the week, followed by a number ranging from 1 to 5. For example, 1#2 means the second Monday

Asterisk (
) to define all the scheduling parameters.

Question mark (?) to leave blank.

&& mean and

Start create a crontab

Open terminal and type : crontab -e

This should open up a file in vim for you. This file is essentially where you can list out your cron jobs — each job on its own line.

Then push the ‘i’ button to start writing the task in vim. For example :
*/30 * * * * cd file_path && python3 python_file_name.py
After writing a task push the ‘ESC’ button to change to normal model in vim, then type: wq! to save and quite.

Then you will see : crontab: installing new crontab

With evironment :

Anaconda

If conda is on your path:
source activate && python xxx.py && source deactivate

If conda isn’t on your path but is installed:
source /path/to/conda/bin/activate /path/to/desired/env_name/ && python xxx.py && source deactivate

VirtualEnv

./path/to/my/venv/bin/python xxx.py
この記事を書いた人

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です