LoginSignup
1
0

More than 3 years have passed since last update.

shebang を記述したスクリプトが cron で動かない場合

Posted at

前提として該当スクリプトのパーミッションは 755 実行権限があること

原因

スクリプトファイルの改行コードが CRLF になっていたことが原因であった。
shebang を使用する際は改行コードを LF にする。

sample_script.php
#!/usr/bin/php
<?php
$datetime = new DateTime();
touch($datetime->format('Ymd-His') . '.txt');

改行コードが CRLF のスクリプト。

$ php /hogehoge/sample_script.php

問題なし。

crontab
0 * * * * /usr/bin/php /hogehoge/sample_script.php

問題なし。
shebang を使用していながらこの記述をする意味はないですが……

crontab
0 * * * * /hogehoge/sample_script.php

ダメです。
改行コードを CRLF にすると大丈夫です。

補足

$ cat /var/log/cron

改行コードが CRLF の場合でも cron 自体は実行されているので cronlog の確認では気づきづらいかも。

参考

https://qiita.com/mohira/items/566ca75d704072bcb26f
http://ya.maya.st/d/201005b.html

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