crontab 与 shell 执行结果不一样

本帖最后由 fujiefujie 于 2011-2-28 10:38 编辑

shell
  1. #!/bin/bash  
  2. service httpd status &> /dev/null
  3. if [ $? -ne 0 ]
  4. then
  5.                day=`date "+%F%R"`
  6.                echo "service httpd is down at $day"  >> /var/log/htmon.txt
  7.                service httpd restart
  8.                service httpd status &> /dev/null
  9.                  
  10.                if  [ $? -ne 0 ]
  11.                then
  12.                           chkconfig --level 2345 httpd on  
  13.                else
  14.                           echo "httpd is running now..."
  15.                fi
  16. fi
复制代码
crontab

*/1 * * * * /opt/htmon.sh

作者: fujiefujie   发布时间: 2011-02-28

有几个问题请教一下大家:
@1
我直接在命令行执行脚本时(已经把httpd服务关掉,自启动也为off)
脚本可以执行,httpd服务启动了。
再次执行却没有显示httpd is running now...
@2
我用crond服务执行:
httpd 服务没启开
提示信息为:
/opt/htmon.sh: line 11: chkconfig: command not found
line 7: service: command not found

是怎么回事儿啊

作者: fujiefujie   发布时间: 2011-02-28

作者: fujiefujie   发布时间: 2011-02-28

是不是crond的环境变量的事儿啊   

作者: fujiefujie   发布时间: 2011-02-28

是环境变量的事儿
脚本加上  . ~/.bash_profile  和  . /etc/profile
就能执行了
可问题是 我的脚本那里出错了   执行时候把服务关了,自启动也off掉。
怎么会执行  chkconfig --level 2345 httpd on  语句
郁闷

作者: fujiefujie   发布时间: 2011-02-28