Closed kdmgs110 closed 6 years ago
crontabの設定
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
#MAILTO="kdmgs110@gmail.com"
#*/1 * * * * /mnt/c/workspace/pydev/tweet-analyser/python3 autoLike.py Progate >>/tmp/autoLike.log
ファイル位置
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ pwd
/mnt/c/workspace/pydev/tweet-analyser
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ tree
.
├── README.md
├── __pycache__
│ └── config.cpython-35.pyc
├── app.py
├── autoLike.log
├── autoLike.py
├── config.py
├── csv
│ ├── default
│ │ ├── followers.csv
│ │ └── test.csv
│ └── followers
├── db
│ └── database.db
├── image
│ ├── index.PNG
│ ├── keyword.PNG
│ ├── rateLimit.PNG
│ ├── show.PNG
│ └── stats.PNG
├── result.csv
├── sql.py
├── templates
│ ├── addQuery.html
│ ├── followings.html
│ ├── home.html
│ ├── includes
│ │ └── _navbar.html
│ ├── layout.html
│ ├── query.html
│ ├── queryList.html
│ └── yourProfile.html
└── test.sh
8 directories, 25 files
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ cat test.sh
echo "HELLO
普通にパスをまちがえとるやんけwwww
* * * * * sh /mnt/c/workspace/pyenv/tweet-analyser/test.sh > /mnt/workspace/pyenv/tweet-analyser/autoLike.log
正しくは
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh > /mnt/workspace/pydev/tweet-analyser/autoLike.log
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ sh /mnt/c/workspace/pydev/twe
et-analyser/test.sh
HELLO
crontab修正
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
#MAILTO="kdmgs110@gmail.com"
#*/1 * * * * /mnt/c/workspace/pydev/tweet-analyser/python3 autoLike.py Progate >>/tmp/autoLike.log
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
めっちゃありがたいアドバイスきた
シェルスクリプトの実行権限と、ログの書き出し先のパーミッションはいかがでしょう
— たっきー (@budehuc) April 8, 2018
そうなのか
あ、あと sh 自体もフルパスではどうですか /bin/sh それか bash でもよいかと思います /bin/bash
— たっきー (@budehuc) April 8, 2018
ありがたすぎか
こんな感じでやっているのですが、パスはあってるっぽいんですよね。https://t.co/0adoTddWlA https://t.co/osKocwTSrS
— Dai (@never_be_a_pm) April 8, 2018
https://qiita.com/shishamo_dev/items/558b116fda95e4f4f808
○○.shを./○○.shで実行できるようにする
cronでシェルスクリプトが動作しない時に確認すべきこと
たぶんこの辺のはなしかな
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ps -aef | grep cron
kdmgs110 517 2 0 16:23 tty1 00:00:00 grep --color=auto cron
プロセスけしてsuで入ってもう一度cronリスタートしてみたらどうじゃろか。
あ、でも起動するとき大体ルート権限ではないよな ユーザーに権限を与えたほうがよい?
パーミッションを755にするということは、そのファイル(ディレクトリ)の制限を以下のようにするということです。その所有者は読み出せて、書き込めて、実行できるそのグループは、読み出せて、実行できる その他のユーザは、読み出せて、実行できる
chmod 755 /mnt/c/workspace/pydev/tweet-analyser/test.sh
chmod 755 /mnt/c/workspace/pydev/tweet-analyser/autoLike.log
進捗だめです汗
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ chmod 755 /mnt/c/workspace/pydev/tweet-analyser/test.sh
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ chmod 755 /mnt/c/workspace/pydev/tweet-analyser/autoLike.log
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ls
README.md __pycache__ app.py autoLike.log autoLike.py config.py csv db image result.csv sql.py templates test.sh
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ tailf autoLike.log
なんで出ねえんだ
サーバの処理を自動実行するcronの仕組みと応用法
ログを出力するところは削って、最低限の動作で試してみてはどうでしょう。touchでファイル出力だけ、など。
— たっきー (@budehuc) April 8, 2018
確かに!
touchだけしてみる。
だめだった
gitとこちらのどちらが良いかわからないですが、とりあえずこちらで。
— 偽りの食欲に抗え (@Royec0619) April 8, 2018
cronのプロセスは起動しているんですよね?(psコマンドの結果に/usr/sbin/cronが出力される)
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ps -aef | grep cron
kdmgs110 598 2 0 17:30 tty1 00:00:00 grep --color=auto cron
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ whoami
kdmgs110
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ps
PID TTY TIME CMD
2 tty1 00:00:01 bash
600 tty1 00:00:00 ps
sudo service cron start
上記を実行していただけますか? cronのプロセスが起動していないように見受けられます。 プロセスが動いて入れば、以下のようにそのコマンドであれば2行出力されるはずです。
ps -aef | grep cron root 975 1 0 17:24 ? 00:00:00 /usr/sbin/cron USER 992 2 0 17:26 tty1 00:00:00 grep --color=auto cron
ありがとうございます!実行してみました!
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ sudo service cron start
[sudo] kdmgs110 のパスワード:
* Starting periodic command scheduler cron [ OK ]
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ps -aef | grep cron
root 618 1 0 17:38 ? 00:00:00 /usr/sbin/cron
kdmgs110 622 2 0 17:38 tty1 00:00:00 grep --color=auto cron
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$```
でてますね。これで、スクリプトは動きます。 結果をお待ちください。
うまくいきました!ありがとうございます!!!
$ crontab -e
# 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
#MAILTO="kdmgs110@gmail.com"
* * * * * touch /mnt/c/workspace/pydev/tweet-analyser/test.log
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ls
README.md app.py autoLike.py csv image sql.py test.log
__pycache__ autoLike.log config.py db result.csv templates test.sh
ということで、実行結果は確認できたので、今度はログ出力を1秒おきにできるか試してみたが、今度は起動しない。
echo "Hello"
crontab -e
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
#MAILTO="kdmgs110@gmail.com"
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
tailf autoLike.log
を実行するtailf autolike.log
をしても何も表示されない
test.shを実行したら、出力はだたしいみたいなので、実行しているパスがおかしいわけではなさそう。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ sh /mnt/c/workspace/pydev/tweet-analyser/test.sh
HELLO
同一ディレクトリには、autoLike.logが存在するので、ファイルが存在しないわけではなさそう。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ls
README.md app.py autoLike.py csv image sql.py test.log
__pycache__ autoLike.log config.py db result.csv templates test.sh
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ps -aef | grep cron
root 618 1 0 17:38 ? 00:00:00 /usr/sbin/cron
kdmgs110 741 2 0 18:02 tty1 00:00:00 grep --color=auto cron
権限を確認してみる。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ls -al
合計 28
drwxrwxrwx 0 root root 512 4月 8 17:39 .
drwxrwxrwx 0 root root 512 4月 3 08:40 ..
drwxrwxrwx 0 root root 512 4月 8 10:24 .git
-rwxrwxrwx 1 root root 11 4月 7 11:02 .gitignore
-rwxrwxrwx 1 root root 1400 4月 8 10:36 README.md
drwxrwxrwx 0 root root 512 4月 5 07:38 __pycache__
-rwxrwxrwx 1 root root 12521 4月 8 15:53 app.py
-rwxrwxrwx 1 root root 0 4月 8 16:00 autoLike.log
-rwxrwxrwx 1 root root 2054 4月 8 14:19 autoLike.py
-rwxrwxrwx 1 root root 348 4月 5 07:38 config.py
drwxrwxrwx 0 root root 512 4月 7 10:38 csv
drwxrwxrwx 0 root root 512 4月 8 10:34 db
drwxrwxrwx 0 root root 512 4月 7 07:50 image
-rwxrwxrwx 1 root root 16 4月 7 13:36 result.csv
-rwxrwxrwx 1 root root 314 4月 3 21:23 sql.py
drwxrwxrwx 0 root root 512 4月 7 08:49 templates
-rwxrwxrwx 1 root root 0 4月 8 17:50 test.log
-rwxrwxrwx 1 root root 13 4月 8 15:56 test.sh
それだと sh (bash) に -c オプション必要そうです
— たっきー (@budehuc) April 8, 2018
sh -cを使うとプロセスが1つ作られてこのプロセスの中で引数に与えられたコマンドが走る。便利だと思われる良い例が見つからない。例えばcronではsh -cのテクニックが使われている。 http://za.toypark.in/html/2009/12-12.html
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
#MAILTO="kdmgs110@gmail.com"
* * * * * sh -c /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
crontab -eで/bin/bashから起動しようとさせたけどだめだった。権限かな。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
* * * * * /bin/bash /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
typoでした。一応うまくいっている。 ただ上書きされないのが謎。
* * * * * /bin/bash /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
これで実行結果が
$ cat autoLike.log
> HELLO
ファイルを上書き追加するためには >> を使うらしい。 https://www.adminweb.jp/command/redirect/index2.html
で修正した。起動するたびにファイルは追加されているみたいだけど、一秒ごとに実行されているわけではないようだ。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ cat autoLike.log
HELLO
HELLO
HELLO
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
* * * * * /bin/bash /mnt/c/workspace/pydev/tweet-analyser/test.sh >> /mnt/c/workspace/pydev/tweet-analyser/autoLike.log
cronは最短で1分ごとですね。 ただ、魔術を使えば秒単位でも可能です。
* * * * *
は1分おきだったのか。
無事起動していることを確認。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ cat autoLike.log
HELLO
HELLO
HELLO
HELLO
HELLO
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ ^C
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ cat autoLike.log
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
今日いろんな人に助けられてて超絶ありがたい本当
お風呂わず。 次はPythonで定期処理できるか確認。 おそらく権限周りで死にそうな気がするけど、試してみる。
どうなるか。
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ 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
* * * * * python3 /mnt/c/workspace/pydev/tweet-analyser/autoLike.py progate >> /mnt/c/workspace/pydev/tweet-analyser/autoLike.log
最近翔泳社からこんなのとか出たんで、一冊持っておくと楽かもです。 https://www.shoeisha.co.jp/book/detail/9784798155968
CUI上でできたので閉じる。
環境
Windows Subsystem For Linux Windows 10
やろうとしていること
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh
を実行して、そのログをautoLike.logに保存したい(詳細は下記crontabの設定)やったこと
期待値
結果