使用方法:设置service服务
整体逻辑:创建一个service服务并将它设置为自启动,这个service会调用桌面的一个sh文件,通过修改这个sh文件的内容,可以方便的修改要设置自启动的东西。sh文件可以放在任何方便使用的位置。
一、编辑.service文件
在/etc/systemd/system/目录下创建一个myautostart.service文件,里面内容如下:
[Unit]
Description=My autostart Service
After=network.target
[Service]
User=firefly
ExecStart=/home/firefly/dep.sh
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target
其中,[Service]下的User代表以哪个用户来执行程序,ExecStart代表所要执行的程序的位置。
二、编辑.sh文件
在/home/firefly/目录下创建一个dep.sh文件,内容如下:
#! /bin/bash
python3 /home/firefly/udpsend.py
三、常用的service命令
# *替换成自己的服务的名字
systemctl is-enabled *.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl daemon-reload
sudo journalctl -u *.service #查看service的输出日志