创建服务文件
[root@mike ~]# cd /etc/systemd/system
[root@mike system]# vim client.service
[Unit]
Description=Client Service
After=network.target
[Service]
Type=simple
ExecStart=/opt/jre/bin/java -jar /opt/agent-server-1.0-SNAPSHOT.jar /opt/client.conf
WorkingDirectory=/opt
User=root
Group=root
StandardOutput=null
StandardError=/opt/error.log
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
参数解释
Restart 是否重启
RestartSec 3秒后重启
使配置生效
sudo systemctl daemon-reload # 重新加载 Systemd 配置
sudo systemctl enable client.service # 设置服务开机自启动
sudo systemctl start client.service # 启动服务