1.进入到系统服务文件夹下:cd /etc/systemd/system
2.创建服务文件:vim xxx.service
[Unit]
Description=XXX Service
After=network.target
[Service]
Type=simple
ExecStart=/home java -jar /home/xxx.jar /home/xxx_log.conf
WorkingDirectory=/home
User=root
Group=root
StandardOutput=null
StandardError=/home/error.log
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
说明:(Restart 是否开启重启,RestartSec 3秒后再次尝试启动)
3.生效配置
1.sudo systemctl daemon-reload # 重新加载 Systemd 配置
2.sudo systemctl enable xxx.service # 设置服务开机自启动
3.sudo systemctl start xxx.service # 启动服务