16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cronでpython実行

Last updated at Posted at 2019-10-14

備忘録。

####環境
Ubuntu Server 18.04.3 LTS
####コマンド
#####起動してるか確認

$ sudo /etc/init.d/cron status

#####起動

$ sudo /etc/init.d/cron start

#####停止

$ sudo /etc/init.d/cron stop

#####再起動

$ sudo /etc/init.d/cron restart

#####cronの設定
何もせず保存して終了

$ crontab -e

#####編集用ファイルの作成

$ crontab -l > .crontab.txt

#####pythonのパスを調べる

$ which python3
/usr/bin/python3

#####実行したいファイルのパスを調べる

$ readlink -f test.py
/home/houtarou/test.py

#####編集用ファイルの最後に、実行したいcronを書き書き

*/1 * * * * (pythonのパス) (実行したファイルのパス)
(例)*/1 * * * * /usr/bin/python3 /home/houtarou/test.py

を追加。

$ vim .crontab.txt

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

*/1 * * * * /usr/bin/python3 /home/houtarou/test.py

#####crontabにファイルを読み込ませる

$ crontab .crontab.txt

#####crontabに反映されたか確認

$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

*/1 * * * * /usr/bin/python3 /home/houtarou/test.py

時間などの詳しい説明は以下を参照
https://qiita.com/katsukii/items/d5f90a6e4592d1414f99

以上

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?