linux Linux

CronでのrequireがあるPHPファイル実行エラー

優 linux Linux

 

cronでPHP等のちょっとしたスクリプトファイルを定期的に実行したい場合に、require等の箇所で相対パスが書かれている場合失敗します。

 

# php /home/hoge/public_html/autoReport.php
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/php_fileinfo.dll' - /usr/lib64/php/modules/php_fileinfo.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  require_once(../config.php): failed to open stream: No such file or directory in /home/hoge/public_html/autoReport.php on line 3
PHP Stack trace:
PHP   1. {main}() /home/hoge/public_html/fj_live/mail/autoReport.php:0

Warning: require_once(../config.php): failed to open stream: No such file or directory in /home/hoge/public_html/autoReport.php on line 3

Call Stack:
    0.0009     323120   1. {main}() /home/hoge/public_html/autoReport.php:0

PHP Fatal error:  require_once(): Failed opening required '../config.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/hoge/public_html/autoReport.php on line 3
PHP Stack trace:
PHP   1. {main}() /home/hoge/public_html/autoReport.php:0

だからといって、単純な絶対パスにするとディレクトリを変更したいとなった時に大変な労力が必要になります。 相対パスは維持したい。

 

そういう場合の対処

該当ファイルまで移動してから、スクリプトを叩くようにCronを設定するのが簡単です。

*  *  *  *  * root cd /home/hoge/public_html/ && /usr/bin/php autoReport.php

/etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
#MAILTO=root
MAILTO=""
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

## hoge株式会社 ○○システム レポート取り込み 30分毎に実行
*/30 *  *  *  * root cd /home/hoge/public_html/ && /usr/bin/php autoReport.php

 

これで大丈夫ですね~。

 

システムの開発等お気軽にご相談下さい。

 

 

linux Linux
最新情報をチェックしよう!
>システム構築・保守に特化した会社です。

システム構築・保守に特化した会社です。

システムの構築・保守運用「システムガーディアン」 社内システム担当が欲しいが、専属で雇うほどの仕事量はない。 必要な時に必要なだけ頼りたいというお悩みを持つ企業様へ専門知識を持って対応を行っております。 サーバから各種システムまで自社・他社で構築されたシステムに対してサポートを行っております。

CTR IMG