ubuntu下定时任务

2018-11-27  本文已影响0人  a9b854aded01
//如果没安装定时器,安装定时器
#!/bin/sh
cron_pid=`pgrep cron`
if [ -z "$cron_pid" ];
then
apt-get install cron
service cron start
else
 echo "[cron]"
fi
//写入另一个脚本,查找某进程PID,杀死这个进程
Pwd=`pwd`//当前脚本路径
rm -f killRemote.sh
echo "#!/bin/sh" >>killRemote.sh
echo "Remote_pid=\`ps -ef |  grep Remote_Server.dll | grep dotnet | grep -v grep | awk '{print \$2}'\`" >>killRemote.sh
echo "if [ -z \"\$Remote_pid\" ];" >>killRemote.sh
echo "then" >>killRemote.sh
echo "echo \"[no pid]\"" >>killRemote.sh
echo "else" >>killRemote.sh
echo "cur_dateTime=\"\`date +%Y-%m-%d,%H:%m:%s\`\""  >>killRemote.sh
echo "echo \"PID: \$Remote_pid \$cur_dateTime\" >> $Pwd/killlog.txt" >>killRemote.sh
echo " kill -9 \$Remote_pid" >>killRemote.sh
echo "fi" >>killRemote.sh
chmod +x killRemote.sh

//写入任务计划 每天凌晨三点执行
#echo "0 3 * * *  sh pwd/killRemote.sh" >> /var/spool/cron/root
echo "0 3 * * *  root sh $Pwd/killRemote.sh" >> /etc/crontab
#echo "*/1 * * * * root hostname >> $Pwd/killlog.txt" >> /etc/crontab
#crontab -l > conf && echo "0 3 * * * sh $Pwd/killRemote.sh" >> conf && crontab conf && rm -f conf
service cron restart
上一篇下一篇

猜你喜欢

热点阅读