LoginSignup
6
6

More than 5 years have passed since last update.

TS-220のcronでPythonスクリプトを実行

Last updated at Posted at 2016-01-24

この記事について

TS-220のcron機能を使って定期的にPythonスクリプトを動かしてみたくなったのでメモ。

sshでのログインはadminしかできません。スクリプトの場所は/root/python/test.pyとしておきます。
(QNAPのNASは再起動すると/root内などの余計なファイルがクリアされるので、恒久的に動かし続ける場合は別の場所にスクリプトを配置するか、autorun.shを編集しましょう。)

参考
Add items to crontab

テストスクリプト

test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
f = open("hoge.txt", "w")
f.write("hello python")

起動すると同じフォルダにあるhoge.txtに"hello python"と記入します。

crontabの編集

[~] cd /etc/config
で移動し、
[~] vi crontab
で以下のように1行追加します。このようになったのは、いろいろ試してみた結果これが一番良く動いたので。出力をlog.txtに吐くようにしています。
*/2 * * * * cd /root/python/; python test.py >> log.txt 2>&1

crondの再起動

/etc/init.d/crond.sh stop
/etc/init.d/crond.sh start

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