Running script in crontab--reboot: command not found

穿精又带淫゛_ 提交于 2020-01-10 05:32:20

问题


I've set a script in my root crontab that is supposed to restart my machine with the reboot command.

However, I am getting a reboot: command not found despite the fact that reboot is in the root user's path.

$ sudo su
$ which reboot
/sbin/reboot
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin

My script:

#!/bin/bash

ping 8.8.8.8 -c 1 > /dev/null 2>&1; exit_code=$?
time_stamp=$(date +"%Y%m%d-%H%M")

if [ $exit_code -ne 0 ]; then
    (1>&2 echo "$time_stamp: failed with exit code $exit_code; restarting now")
    reboot
else
    echo "$time_stamp: ok"
fi

root user crontab:

$ sudo crontab -l
58 * * * * /home/pi/github/ping-restart/ping-restart.sh >> /home/pi/github/ping-restart/cron.log 2>&1
$ sudo su
58 * * * * /home/pi/github/ping-restart/ping-restart.sh >> /home/pi/github/ping-restart/cron.log 2>&1

...yes, this is only a temporary workaround while I figure out why the internet keeps dropping.


回答1:


cron jobs run with a very basic environment setup; among other things, the default PATH is just /usr/bin:/bin. It does not use the user's regular shell setup. There are several ways to solve this:

  • Use the full path in the script (i.e. /sbin/reboot).
  • Set PATH in the script before using reboot (i.e. PATH=/usr/bin:/bin:/usr/sbin:/sbin).
  • Set PATH in the crontab before the entry for your script (syntax is the same as in the script).


来源:https://stackoverflow.com/questions/44731613/running-script-in-crontab-reboot-command-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!