linux启动停止脚本模板
#命令的位置 CMD="java -jar x.jar" #参数空格隔开 ARGS="" #命令日志文件 LOG_FILE="./start.log" #pid文件 PID_FILE="./PID" PID="" setup_pid() { PID=$1 kill -0 $PID > /dev/null 2>&1 local exit_code=$? if [ "$exit_code" == "1" ]; then if [ -f $PID_FILE ]; then rm -f $PID_FILE fi PID="" fi } exit_by_error() { echo "error: $1" exit 1 } start() { if [ -n "$PID" ]; then exit_by_error "process is running, pid is $PID" exit 1 fi touch $LOG_FILE echo "starting..." nohup $CMD $ARGS > $LOG_FILE 2>&1 & local pid=$! setup_pid $pid if [ -n "$PID" ]; then echo "process started! pid is $PID" echo $PID > $PID_FILE else exit_by_error "start faild!" fi tail -f $LOG_FILE } stop() { echo "stopping..." if [ -n "$PID" ]; then kill -9 $PID fi if [ -f $PID_FILE ]; then rm -f $PID_FILE
PID="" fi echo "stopped!" } restart() { stop start } status() { if [ -n "$PID" ]; then echo "process is running, pid is $PID" else exit_by_error "process not running!" fi } if [ -f $PID_FILE ]; then PID=$(cat $PID_FILE) if [ -n "$PID" ]; then setup_pid $PID fi if [ "$PROCESS_STATUS" == "1" ]; then rm -f $PID_FILE PID="" fi fi case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage command:start|stop|restart|status" exit 1 esac exit 0