1,配置目录
root@tx2:/etc/systemd/system# pwd
/etc/systemd/system
2,配置文件(需要配执行权限)
[Unit]
Description=hello_MyCar
[Service]
ExecStart=/work/05-p2p/hello/build/hello_cmake
Restart=on-failure
RestartSec=3s
User=root
[Install]
WantedBy=multi-user.target
3,设置为开机自启动:
systemctl enable mycar.service
root@tx2:/home/tx2# systemctl enable mycar.service
Created symlink /etc/systemd/system/multi-user.target.wants/mycar.service → /etc/systemd/system/mycar.service.
4,查看进程
root@tx2:/home/tx2# ps aux | grep hello
5,查看服务状态
tx2@tx2:~$ systemctl status mycar.service
● mycar.service - hello_MyCar
Loaded: loaded (/etc/systemd/system/mycar.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-03-21 15:20:18 CST; 4min 56s ago
Main PID: 4332 (hello_cmake)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/mycar.service
└─4332 /work/05-p2p/hello/build/hello_cmake
3月 21 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
3月 21 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
6,重启策略:
Restart:配置在服务进程退出,终止或超时时是否应重新启动服务。
on-failure:如果设置为on-failure,则当进程以非零退出代码退出,由信号终止,当操作(例如服务重新加载)超时,以及何时触发配置的监视程序超时时,将重新启动服务。
kill pid
------此时程序不会自动重启,只能重启机器后启动程序
kill -9 pid
------此时程序会自动重启,或者重启机器后启动程序
7,参考
添加启动权限# chmod +x vsftpd.service
启动服务:systemctl start vsftpd.service
关闭服务:systemctl stop vsftpd.service
重启服务:systemctl restart vsftpd.service
显示服务的状态:systemctl status vsftpd.service
在开机时启用服务:systemctl enable vsftpd.service
在开机时禁用服务:systemctl disable vsftpd.service
查看服务是否开机启动:systemctl is-enabled vsftpd.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
使用systemctl start xxx启动自己的程序|开机启动|守护进程
https://blog.csdn.net/bandaoyu/article/details/124358513
systemd和initd添加开机自启服务
https://blog.csdn.net/weixin_44698673/article/details/128676792
ubuntu开机自启动服务管理
最新推荐文章于 2024-01-20 10:15:11 发布