在crontab中运行脚本-重新启动:找不到命令 [英] Running script in crontab--reboot: command not found

查看:933
本文介绍了在crontab中运行脚本-重新启动:找不到命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在根crontab中设置了一个脚本,该脚本应该使用reboot命令重新启动计算机.

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

但是,尽管reboot在根用户的路径中,但我却得到了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.它不使用用户的常规Shell设置.有几种解决方法:

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=/usr/bin:/bin:/usr/sbin:/sbin)之前,请在脚本中设置PATH.
  • 在脚本条目之前在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中运行脚本-重新启动:找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆