1、在部署目录下新建 startup.sh 脚本(注意java路径要改为绝对路径)。然后检查该脚本是否可以执行。
- #!/bin/sh
- WORKDIR=/home/
- TARGET=test.jar
-
- start(){
- pid=`get_pid`
- now=`date "+%Y-%m-%d %H:%M:%S"`
- if [ -z "$pid" ]; then
- new_target
- pid=`get_pid`
- printf '%s start %s done, with pid %s.\n' "$now" "$TARGET" "$pid"
- else
- printf '%s %s is running, with pid %s.\n' "$now" "$TARGET" "$pid"
- fi
- }
-
- new_target(){
- if [ -f "$WORKDIR"/"$TARGET" ]; then
- exec /usr/local/jdk1.8.0_381/bin/java -Xms2048m -Xmx4096m -jar "$WORKDIR"/"$TARGET" >/dev/null 2>&1 &
- else
- now=`date "+%Y-%m-%d %H:%M:%S"`
- printf '%s %s file not exist.\n' "$now" "$WORKDIR"/"$TARGET"
- fi
- }
-
- stop(){
- pid=`get_pid`
- now=`date "+%Y-%m-%d %H:%M:%S"`
- if [ -z "$pid" ]; then
- printf '%s %s is NOT running, ignored.\n' "$now" "$TARGET"
- else
- printf '%s try to kill %s, with pid %s.\n' "$now" "$TARGET" "$pid"
- kill "$pid"
- wait_till_killed
- fi
- }
-
- wait_till_killed(){
- wait_time=60
- while [ $wait_time -ge 0 ]
- do
- sleep 1
- pid=`get_pid`
- now=`date "+%Y-%m-%d %H:%M:%S"`
- if [ -z "$pid" ]; then
- printf '%s stop %s done.\n' "$now" "$TARGET"
- return
- else
- printf '%s wait till killed, with pid %s.\n' "$now" "$pid"
- fi
- let wait_time--
- done
-
- kill -9 "$pid"
- printf '%s FORCE stop %s done.\n' "$now" "$TARGET"
- }
-
- status(){
- pid=`get_pid`
- now=`date "+%Y-%m-%d %H:%M:%S"`
- if [ -z "$pid" ]; then
- printf '%s %s is NOT running.\n' "$now" "$TARGET"
- else
- printf '%s %s is running, with pid %s.\n' "$now" "$TARGET" "$pid"
- fi
- }
-
- get_pid() {
- pid=`ps -aux | grep "$TARGET" | grep -v grep | awk '{print $2}'`
- echo "$pid"
- }
-
- check(){
- pid=`get_pid`
- now=`date "+%Y-%m-%d %H:%M:%S"`
- if [ -z "$pid" ]; then
- printf '%s %s is NOT running, call by watch dog.\n' "$now" "$TARGET"
- start
- fi
- }
-
- monitor(){
- now=`date "+%Y-%m-%d %H:%M:%S"`
- printf '%s %s start to monitor.\n' "$now" "$TARGET"
- while [ 1 ]
- do
- sleep 10
- check
- done
- now=`date "+%Y-%m-%d %H:%M:%S"`
- printf '%s %s stop to monitor.\n' "$now" "$TARGET"
- }
-
- echo "---------------------------------------------------"
- echo " Start/Stop/Status script"
- echo " Work dir: $WORKDIR "
- echo " APP: $TARGET "
- echo "---------------------------------------------------"
-
-
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status
- ;;
- monitor)
- monitor
- ;;
- check)
- check
- ;;
- restart)
- stop
- start
- ;;
- *)
- printf 'Usage: %s {start|stop|status|restart|monitor|check}\n' "$0"
- exit 1
- ;;
- esac
-
2、在 /etc/systemctl/system 目录下,新建test.service 文件。内容如下
- [Unit]
- After=network-online.target
- Wants=network-online.target
-
- [Service]
- WorkingDirectory=/home/
-
- Type=forking
- ExecStart=/bin/bash ./startup.sh start
- ExecStop=/bin/bash ./startup.sh stop
-
- TimeoutSec=65
-
- RestartSec=10
-
- Restart=always
-
- StartLimitInterval=0
-
- LimitNOFILE=65535
-
- LimitNPROC=65535
-
- LimitCORE=65535
-
- KillMode=process
-
- SuccessExitStatus=143
-
-
- [Install]
- WantedBy=multi-user.target
-
3、修改了service文件,需要执行以下命令更新一下。
systemctl daemon-reload
4、设置开机自启动
systemctl enable test.service
5、执行以下命令,启动/关闭/查看状态。
- systemctl start test.service
-
- systemctl stop test.service
-
- systemctl status test.service