LoginSignup
7
2

More than 5 years have passed since last update.

cronの最終行が実行されない原因と対処法

Last updated at Posted at 2018-03-29

cronの状態

ぱっと見る限り問題なさそう

$ crontab -l
0 1 * * 1-5 execute.sh
35 11 * * 1-5 execute.sh

crontabで編集したものとファイルを比べる

ファイルの末尾に改行がなかった

$ sudo cp /var/spool/cron/hoge ~/hoge-before
$ crontab -e
:wq
$ sudo cp /var/spool/cron/hoge ~/hoge-after
$ diff ~/hoge-before ~/hoge-after

原因

改行か%がないとその行は実行されない

man 5 crontab

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

意訳
6番目のフィールド(行の残りの部分)は、実行するコマンドを指定します。 行のコマンド部分全体(改行または%文字まで)は、/bin/shまたはcronfileのSHELL変数で指定されたシェルによって実行されます。 バックスラッシュ(\)でエスケープしない限り、コマンドのパーセント記号(%)は改行文字に変更され、最初の%以降のすべてのデータは標準入力としてコマンドに送られます。

解決

将来忘れそうなので、未来の自分でも気づける方法として、最終行にコメントを付与してみた。

 0 1 * * 1-5 execute.sh
 35 11 * * 1-5 execute.sh
 # Comment for the newline of crontab termination character. See man 5 crontab.
7
2
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
7
2