1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 树莓派 使用 systemd 设置开机启动项

树莓派 使用 systemd 设置开机启动项

时间:2019-11-19 15:55:50

相关推荐

树莓派 使用 systemd 设置开机启动项

创建服务

在树莓派上创建一个 .service 文件。例如:

realtime.service

[Unit]Description=My serviceAfter=network.target[Service]Restart=on-failureRestartSec=5ExecStart=/usr/bin/python3 -u /home/pi/RealTime.pyStandardOutput=inheritStandardError=inheritUser=pi[Install]WantedBy=multi-user.target

在上面的范例中,服务会去以 Python 3 来运行 /home/pi/realtime目录下面的 main.py 脚本。用这种方法,你不仅可以配置 Python 脚本,只需要将 ExecStart 这行改为需要启动的任何程序或脚本即可。

将这个文件用 root 用户保存到 /etc/systemd/system 目录:

sudo cp realtime.service /etc/systemd/system/realtime.service

然后就可以用下面的命令尝试启动服务了:

sudo systemctl start realtime.service

停止服务:

sudo systemctl stop realtime.service

添加或修改配置文件后,需要重新加载

systemctl daemon-reload

设置开机时自动运行:

sudo systemctl enable realtime.service

查看服务状态

systemctl status realtime.service

查看进程

ps -ef |grep realtime.service

关闭进程

kill -9 567(进程ID)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。