我测试了好几篇文章,终于有个成功了


local.service

vim /etc/systemd/system/rc-local.service

输入以下内容:

[Unit] 
Description=/etc/rc.local Compatibility 
ConditionPathExists=/etc/rc.local 

[Service] 
Type=forking 
ExecStart=/etc/rc.local start 
TimeoutSec=0 
StandardOutput=tty 
RemainAfterExit=yes 
SysVStartPriority=99 

[Install]
WantedBy=multi-user.target

rc.local

vim /etc/rc.local

#!/bin/sh -e 
## rc.local

sh /root/startup.sh

exit 0

收尾

  1. 授权 chmod +x /etc/rc.local

  2. 自启动 systemctl enable rc-local

  3. 运行 systemctl start rc-local.service

  4. 查看状态 systemctl status rc-local.service

先别重启, ps -x配合grep搜索关键字看看进程启动没有

之后保存好文件重启进行测试 再次查看是否启动


举个例子:

startup.sh

#!/bin/bash -e
cd /root/News-D/
nohup python3 main.py &
cd /root/News-D/Finance/
nohup python3 -m http.server 20777 &
rm nohup.a.out
exit 0

Re:

https://www.cnblogs.com/Deskew/p/13798595.html