我并没有完全成功,感觉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