ubuntu开机自启动服务管理

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

321 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:14 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
321 15:25:15 tx2 hello_cmake[4332]: Hello CMake!
321 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





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值