运行 crontab 中的脚本--重新启动:找不到命令
Running script in crontab--reboot: command not found
我在我的根 crontab 中设置了一个脚本,它应该使用 reboot
命令重新启动我的机器。
但是,尽管 reboot
在 root 用户的路径中,但我得到了 reboot: command not found
。
$ sudo su
$ which reboot
/sbin/reboot
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
我的脚本:
#!/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:
$ 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
...是的,这只是一个临时的解决方法,我正在弄清楚为什么互联网总是掉线。
cron 作业 运行 具有非常基本的环境设置;除其他事项外,默认 PATH 只是 /usr/bin:/bin
。它不使用用户的常规shell设置。有几种方法可以解决这个问题:
- 在脚本中使用完整路径(即
/sbin/reboot
)。
- 在使用
reboot
(即 PATH=/usr/bin:/bin:/usr/sbin:/sbin
)之前在脚本中设置 PATH。
- 在您的脚本条目之前的 crontab 中设置 PATH(语法与脚本中的相同)。
我在我的根 crontab 中设置了一个脚本,它应该使用 reboot
命令重新启动我的机器。
但是,尽管 reboot
在 root 用户的路径中,但我得到了 reboot: command not found
。
$ sudo su
$ which reboot
/sbin/reboot
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
我的脚本:
#!/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:
$ 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
...是的,这只是一个临时的解决方法,我正在弄清楚为什么互联网总是掉线。
cron 作业 运行 具有非常基本的环境设置;除其他事项外,默认 PATH 只是 /usr/bin:/bin
。它不使用用户的常规shell设置。有几种方法可以解决这个问题:
- 在脚本中使用完整路径(即
/sbin/reboot
)。 - 在使用
reboot
(即PATH=/usr/bin:/bin:/usr/sbin:/sbin
)之前在脚本中设置 PATH。 - 在您的脚本条目之前的 crontab 中设置 PATH(语法与脚本中的相同)。