linux 定时任务
1.设置执行脚本:
修改/var/spool/cron/root.sh,每1分钟执行/root/cron目录下的jkapache.sh
*/1 * * * * bash /root/cron/jkapache.sh
2.apache自动启动执行脚本
2.1 创建目录/root/cron
2.2 创建脚本jkapache.sh
##脚本
#!/bin/bash
#判断指定进程是否存在
result=`ps -ef | grep -w apache | grep -v grep | wc -l`
if [ $result -le 0 ]; then
#不存在, 重启
/usr/local/apache/bin/apachectl -f /usr/local/apache/conf/httpd.conf -k start
else
#存在,判断状态
#取进程状态,用来判断是否僵死
val=`ps -aux | grep apache | grep -v grep | awk '{print $8}'`
if [ "$val" == "Z" ];then
# 取进程ID,用来kill掉进程
pid = `ps -aux | grep apache | grep -v grep | awk '{print $2}'`
kill -9 $pid
fi
fi
linux自动执行脚本如果报 syntax error near unexpected token,是编码的问题。 解决办法总结两歩:
1:执行 vi -b test.sh
test.sh 即为要执行的脚本
2: 替换:shift+:冒号>输入:%s/^M//g
在末行模式下:
输入:%s/^M//g^M符,不是“^”再加上“M”,而是由“Ctrl+v”、“Ctrl+M”键生成的。

浙公网安备 33010602011771号