/lib/systemd/system目录下创建service文件

cd /lib/systemd/system

vim test.service

[Unit]
Description=test
After=network.target

[Service]
Type=forking
ExecStart=/opt/test/test.sh
ExecReload=/opt/test/reload.sh
ExecStop=/opt/test/stop.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

刷新配置

systemctl daemon-reload

设置开启自启

systemctl enable test.service

查看是否设置成功

systemctl is-enabled test.service

如果怕有问题,可以提前进行验证
验证方式就是自己手动试一下:systemctl start test.service