首先新建Service,名称叫做server-api
vim /lib/systemd/system/server-api.service 或者 vim /etc/systemd/system/server-api.service
[Unit]
Description=grave server
[Service]
Type=simple
Restart=always
RestartSec=3s
TimeoutStopSec=10s
ExecStart=/www/wwwroot/grave-server/app01
WorkingDirectory=/www/wwwroot/grave-server # 使用workdir的原因是如果启动指定了配置,不配置dir的话,要写绝对路径,比如 go run xx -c /xxx/xxx/绝对路径.conf
StandardOutput=append:/var/log/grave-server.log # 这里用append,不要用file,要不然服务重启,日志会出现不再写的情况
StandardError=append:/var/log/grave-server-error.log
[Install]
WantedBy=multi-user.target
ExecStart是go可执行文件的路径
WorkingDirectory要注意,如果程序中使用了相对路径来加载一些配置文件,如果在Service中没有配置WorkingDirectory,默认是根路径,所以配置文件就从根路径来寻找,会造成一些意想不到的情况
启动
service server-api start
停止
service server-api stop
查看状态
service server-api status
开机启动
service server-api enable