kdmgs110 / Tweet-Analyser

Flaskでつくれるツイート分析アプリです。
4 stars 0 forks source link

CRONTABを実行する #23

Closed kdmgs110 closed 6 years ago

kdmgs110 commented 6 years ago

環境

Windows Subsystem For Linux Windows 10

やろうとしていること

やったこと

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

期待値

結果

kdmgs110 commented 6 years ago

環境

やったこと

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
kdmgs110 commented 6 years ago

普通にパスをまちがえとるやんけ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 commented 6 years ago
kdmgs110@DESKTOP-4HHEO95:/mnt/c/workspace/pydev/tweet-analyser$ sh /mnt/c/workspace/pydev/twe
et-analyser/test.sh
HELLO
kdmgs110 commented 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
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log
kdmgs110 commented 6 years ago

めっちゃありがたいアドバイスきた

kdmgs110 commented 6 years ago

そうなのか

kdmgs110 commented 6 years ago

ありがたすぎか

kdmgs110 commented 6 years ago

!/bin/sh は ただのコメントじゃないよ! Shebangだよ!

https://qiita.com/shishamo_dev/items/558b116fda95e4f4f808

○○.shを./○○.shで実行できるようにする

https://qiita.com/shishamo_dev/items/558b116fda95e4f4f808

kdmgs110 commented 6 years ago

cronでシェルスクリプトが動作しない時に確認すべきこと

https://qiita.com/mohira/items/566ca75d704072bcb26f

kdmgs110 commented 6 years ago

たぶんこの辺のはなしかな

https://teratail.com/questions/89391

kdmgs110 commented 6 years ago
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
kdmgs110 commented 6 years ago

プロセスけしてsuで入ってもう一度cronリスタートしてみたらどうじゃろか。

kdmgs110 commented 6 years ago

あ、でも起動するとき大体ルート権限ではないよな ユーザーに権限を与えたほうがよい?

kdmgs110 commented 6 years ago

パーミッションを755にするということは、そのファイル(ディレクトリ)の制限を以下のようにするということです。その所有者は読み出せて、書き込めて、実行できるそのグループは、読み出せて、実行できる その他のユーザは、読み出せて、実行できる

http://kouritool.com/what-linux/%E3%83%91%E3%83%BC%E3%83%9F%E3%83%83%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%92755%E3%81%ABchmod/

chmod 755 /mnt/c/workspace/pydev/tweet-analyser/test.sh
chmod 755 /mnt/c/workspace/pydev/tweet-analyser/autoLike.log
kdmgs110 commented 6 years ago

進捗だめです汗

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
kdmgs110 commented 6 years ago

なんで出ねえんだ

kdmgs110 commented 6 years ago

サーバの処理を自動実行するcronの仕組みと応用法

http://oxynotes.com/?p=6912

kdmgs110 commented 6 years ago
kdmgs110 commented 6 years ago

確かに!

kdmgs110 commented 6 years ago

touchだけしてみる。

kdmgs110 commented 6 years ago

だめだった

kdmgs110 commented 6 years ago
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 commented 6 years ago
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
miyamiyab commented 6 years ago

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 commented 6 years ago

ありがとうございます!実行してみました!


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$```
miyamiyab commented 6 years ago

でてますね。これで、スクリプトは動きます。 結果をお待ちください。

kdmgs110 commented 6 years ago

うまくいきました!ありがとうございます!!!

期待値

結果

手順

$ 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
kdmgs110 commented 6 years ago

ということで、実行結果は確認できたので、今度はログ出力を1秒おきにできるか試してみたが、今度は起動しない。

手順

echo "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
#MAILTO="kdmgs110@gmail.com"
* * * * * sh /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/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 commented 6 years ago

権限を確認してみる。

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
kdmgs110 commented 6 years ago

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

期待値:

結果:

kdmgs110 commented 6 years ago

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
kdmgs110 commented 6 years ago

typoでした。一応うまくいっている。 ただ上書きされないのが謎。

* * * * * /bin/bash /mnt/c/workspace/pydev/tweet-analyser/test.sh 1> /mnt/workspace/pydev/tweet-analyser/autoLike.log

これで実行結果が

$ cat autoLike.log
> HELLO
kdmgs110 commented 6 years ago

ファイルを上書き追加するためには >> を使うらしい。 https://www.adminweb.jp/command/redirect/index2.html

kdmgs110 commented 6 years ago

で修正した。起動するたびにファイルは追加されているみたいだけど、一秒ごとに実行されているわけではないようだ。

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
nikukyugamer commented 6 years ago

cronは最短で1分ごとですね。 ただ、魔術を使えば秒単位でも可能です。

kdmgs110 commented 6 years ago

* * * * * は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
kdmgs110 commented 6 years ago

今日いろんな人に助けられてて超絶ありがたい本当

kdmgs110 commented 6 years ago

お風呂わず。 次はPythonで定期処理できるか確認。 おそらく権限周りで死にそうな気がするけど、試してみる。

kdmgs110 commented 6 years ago

どうなるか。

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
nikukyugamer commented 6 years ago

最近翔泳社からこんなのとか出たんで、一冊持っておくと楽かもです。 https://www.shoeisha.co.jp/book/detail/9784798155968

dsc09359_200

kdmgs110 commented 6 years ago

CUI上でできたので閉じる。