Linux scheduled restart tomcat and configuration tasks timed Detailed
1. First, write a script to restart tomcat
#!/bin/sh TOMCAT_PATH=tomcat地址路径/bin echo "TOMCAT_PATH is $TOMCAT_PATH" PID=`ps aux | grep tomcat地址路径 | grep java | awk '{print $2}'` if [ -n "$PID" ]; then echo "Will shutdown tomcat: $PID" $TOMCAT_PATH/shutdown.sh -force sleep 5 else echo "No Tomcat Process $PID" fi ps -ef|grep -v grep|grep tomcat地址路径 | grep java |awk '{print "kill -9 "$2}'|sh sleep 1 $TOMCAT_PATH/startup.sh sleep 50 for((i=1;i<5;i++)) do LASTINFO=`tail -n 1 tomcat地址路径/logs/catalina.out |grep 'INFO: Server startup in'` if [ -n "$LASTINFO" ]; then break else ps -ef|grep -v grep|grep tomcat地址路径 | grep java |awk '{print "kill -9 "$2}'|sh sleep 1 $TOMCAT_PATH/startup.sh sleep 50 fi done
2. assign permissions to the file
chmod 777 restart_tomcat.sh (file name - my name here restart_tomcat.sh)
Format created directly under 3.window system, you need to modify the file
vi restart_tomcat.sh use the following command to view the file format : set ff or: set fileformat can see the following information fileformat = = DOS or fileformat UNIX using the following commands to change the file format : set ff = UNIX or: set fileformat = UNIX save and exit : wq
4. Does the manual test files can be executed ./restart_tomcat.sh
ps: as prompt -bash: ./restart_tomcat.sh: / bin / sh ^ M: bad interpreter: No such file or directory error, please review the file format has been modified successfully!
5. Add the Crontab task scheduler crontab -e
crontab file rows of six fields, different fields splitting space or tab keys. The first five fields specify the time the command to run the minute ( 0-59 ) hour ( 0-28 ) date ( 1-31 ) month ( 1-12 ) day of the week ( 0-6 , where 0 represents Sunday) The sixth field a string to be executed at the appropriate time
example:
# 6 am every day 10 points 106 * * * Script path + script full name
ps: "crontab: installing new crontab " for success. If an error occurs, a close look crontab format is correct!
6. Review the task is successfully set
crontab - L # represents a list of all scheduled tasks crontab -r # indicates the timing task delete users, when this command is executed, all users of the following scheduled task will be deleted after execution crontab -l prompts the user: "no crontab for ADMIN "