在 crontab 中运行脚本--reboot: command not found [英] Running script in crontab--reboot: command not found
问题描述
我在我的根 crontab 中设置了一个脚本,该脚本应该使用 reboot
命令重新启动我的机器.
I've set a script in my root crontab that is supposed to restart my machine with the reboot
command.
但是,尽管 reboot
位于 root 用户的路径中,但我收到了 reboot: command not found
.
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 用户 crontab:
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.
推荐答案
cron 作业在非常基本的环境设置下运行;除其他外,默认 PATH 只是 /usr/bin:/bin
.它不使用用户的常规外壳设置.有几种方法可以解决这个问题:
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:
- 在脚本中使用完整路径(即
/sbin/reboot
). - 在使用
reboot
之前在脚本中设置PATH(即PATH=/usr/bin:/bin:/usr/sbin:/sbin
). - 在 crontab 中的脚本条目之前设置 PATH(语法与脚本中的相同).
- 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).
这篇关于在 crontab 中运行脚本--reboot: command not found的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!