ubuntu20.04设置文件开机自启动

作者 : admin 本文共1170个字,预计阅读时间需要3分钟 发布时间: 2024-06-8 共3人阅读

硬件:树霉派4B
系统:ubuntu20.04

在ubuntu20.04上经常需要运行 ./BluetoothServerParse_L.c ,比较繁琐,想要设置开机自启动,让树霉派4B在接上电源之后就自动运行该程序。使用systemd服务,设置步骤如下:

(1)在/etc/systemd/system/下创建一个systemd服务文件,例如BluetoothServerParse_L.service。
ubuntu20.04设置文件开机自启动插图当遇到权限不够时,就用root用户,即使用sudo。

(2)打开BluetoothServerParse_L.service,并编辑该文件,内容大致如下:

[Unit]  
Description=Your Service Description  
After=multi-user.target  
  
[Service]  
ExecStart=/path/to/your/BluetoothServerParse_L  
Restart=on-failure  
  
[Install]  
WantedBy=multi-user.target

将/path/to/your/BluetoothServerParse_L替换为你的脚本或程序的完整路径。

根据BluetoothServerParse_L实际路径,我在BluetoothServerParse_L.service中添加的内容如下:

[Unit]  
Description=Bluetooth Server Parse Service  
After=network.target  
  
[Service]  
ExecStart=/home/ubuntu/ccy/glove_car_bt/ros_udp_udp-main/src/udp_udp/C/BluetoothServerParse_L  
Restart=on-failure  
  
[Install]  
WantedBy=multi-user.target

保存、关闭文件。

(3)重新加载systemd守护进程以应用更改

sudo systemctl daemon-reload

(4)启用并启动服务

sudo systemctl enable BluetoothServerParse_L.service  
sudo systemctl start BluetoothServerParse_L.service

ubuntu20.04设置文件开机自启动插图(1)(5)查看服务的状态

sudo systemctl status BluetoothServerParse_L.service

ubuntu20.04设置文件开机自启动插图(2)服务已经成功启动,并且状态为 active (running)。
主进程 ID(Main PID)是 9249,并且它正在执行 /home/ubuntu/ccy/glove_car_bt/ros_udp_udp-main/src/udp_udp/C/BluetoothServerParse_L

经过以上步骤,自启动文件就设置成功了。

本站无任何商业行为
个人在线分享 » ubuntu20.04设置文件开机自启动
E-->