crontabの中のPATHの穴
一、現象
コマンドラインでスクリプトを正常に実行し、crontabでエラーを報告します.
lsof: No such file or directory
新聞を読み間違えたのはlsofという命令が見つからないはずだ.
二、検査
lsofコマンドのパスを表示するには、次の手順に従います.
$ type -a lsof
lsof is /usr/bin/lsof
システムのPATHパスを表示するには:
$ echo $PATH
/usr/lib64/ccache:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/dell/srvadmin/bin:/bin
中には/usr/sbinが含まれています.これはなぜコマンドラインが正常に動作したのかを説明することができます.それはcrontabのPATHの問題です.
crontabでecho$PATHのスクリプトを実行します.結果は次のとおりです.
/usr/bin:/bin
果たしてcrontabの原因は
三、原因
$ man 5 crontab
man pageで見つけた
Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is running from, and may not be changed.
四、解決方法
スクリプト内でexport PATH=/usr/sbin:$PATH、またはcrontabを
* * * * * . /etc/profile; your command