linux添加system服务自启动,向systemd系统添加自启动服务进程

我并没有完全成功,感觉systemd系统太复杂,只是找到了一个可以成功设置自启动程序的方式而已。

$ cat /lib/systemd/system/start_all.service

[Unit]

Description=start_all -- cook

After=network.target

[Service]

ExecStart=/bin/python3.8 /home/xinlin/repos/cook/start.py

ExecReload=/bin/python3.8 /home/xinlin/repos/cook/start.py

[Install]

WantedBy=multi-user.target

这是一个service文件,或者说是systemd系统的unit。

[Service]部分,type默认是simple,ExecStart对应systemctl start命令,ExecReload对应systemctl reload命令;没有设置stop命令。

关键就是ExecStart=/bin/python3.8 /home/xinlin/repos/cook/start.py这一行,如果这一行是是一个可以执行的.sh文件,shebang(hang bang)正确,也无法执行。具体原因查了很久,网络上有人说跟SELinux有关系,以我现在的水平,就搞不懂了。

不管如何,上面这个unit file在reboot的时候,成功了。

设置reboot自启动,systemctl enable命令。取消就是 systemctl disable。

systemctl enable xxxxx.service命令,相当于在/usr/lib/system/systemd中的unit file,建立一个符号链接到/etc/systemd/system下面。。systemd在系统启动的时候,读取/etc/sysmted/system里面的符号链接。更多关于systemctl的命令。

用这种方式启动的python进程,属于root。

目前这种方式对我来说,可以自启动一个自己编写的程序,暂时够用了。Linux系统知识还要加强!

---------分割线--------

补充一个自启动apache httpd的service文件,这是Type=forking,我只是感觉这个会成功,结果真成了:

$ cat /lib/systemd/system/start_httpd.service

[Unit]

Description=start apache httpd

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/apache-2.4/bin/apachectl start

[Install]

WantedBy=multi-user.target

--------分割线--------

查看某个服务进程的状态:systemctl status xxxxxxxx.service

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值