一、systemctl

systemctl daemon-reload
systemctl enable app && systemctl start app
systemctl status app

编写app.service

vi /etc/systemd/system/app
[Unit]
Description=app

#After=network.target

[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
DynamicUser=true
ExecStart=/opt/app/run

[Install]
WantedBy=multi-user.target

二、服务脚本

常用命令

/etc/init.d/app
Available commands:
        start           Start the service
        stop            Stop the service
        restart         Restart the service
        reload          Reload configuration files (or restart if service does not implement reload)
        enable          Enable service autostart
        disable         Disable service autostart
        enabled         Check if service is started on boot

编写脚本(需要调整权限)

#!/bin/sh /etc/rc.common

START=99
STOP=10

PROG="/opt/app/run"
CONFIG="/opt/app/config.json"
PIDFILE="/var/run/app.pid"

start() {
    if [ -e "$PIDFILE" ]; then
        return 1
    fi

    $PROG -c $CONFIG

    echo $! > $PIDFILE
}

stop() {
    if [ -e "$PIDFILE" ]; then
        kill "$(cat $PIDFILE)" 2>/dev/null
        rm -f "$PIDFILE"
    fi
}

restart() {
    stop
    start
}