1、安裝
yum install vixie-cron
yum install crontabs
2、啓動 service crond start
3、設置開機自啓: systemctl enable crond.service
4、添加定時任務
vim /etc/crontab
在末尾添加 */1 * * * * root /opt/demo.sh >> /opt/demo.txt
過一分鐘看demo.txt裏有記錄了
5、demo.sh示例內容
#!/bin/bash
tomcat_home=/usr/local/tomcat
SHUT_DOWN=$tomcat_home/bin/shutdown.sh
START_UP=$tomcat_home/bin/startup.sh
echo 'hello'
########################### tomcat #############################
PID=`ps -ef |grep $tomcat_home/conf |grep -v grep | awk '{print $2}'`
if [ ! "$PID" ];then
echo 'proccess dont exist'
else
echo 'kill process PID:$PID'
kill -9 $PID
fi
current_time=/opt/web_bak/$(date "+%Y%m%d")
echo $current_time
if [ ! -d "$current_time" ];then
mkdir $current_time
else
echo "file exist"
fi
#back old war
if [ -f "$tomcat_home/webapps/demo.war" ];then
cp $tomcat_home/webapps/demo.war $current_time/
fi
#use new war
if [ -f "/opt/demo.war" ];then
cp /opt/demo.war $tomcat_home/webapps/
fi
$START_UP
echo 'end'