示例:
将自己编写的一个应用程序做成开机自启动服务。
应用程序名字:candemo
整个操作需要进行如下操作:
1、cd /etc/systemd/system
sudo vi can_car.service
内容如下:
//can_car.service 文件的内容
Description=can_car Compatibility
ConditionFileIsExecutable=/usr/local/scripts/start_can_car.sh
ConditionFileIsExecutable=/usr/local/scripts/stop_can_car.sh
After=network.target multi-user.target jetson-cam-cfg.service
[Service]
Type=simple
ExecStart=/usr/local/scripts/start_can_car.sh start
ExecStop=/usr/local/scripts/stop_can_car.sh stop
TimeoutSec=60
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Alias=can_car.service
截图如下:
2、
cd /usr/local/scripts/
vi start_can_car.sh
内容如下:
#! /bin/bash
cd /home/nvidia/
./candemo &
vi stop_can_car.sh
内容如下:
#! /bin/bash
killall candemo candemo.service
截图如下:
3、注意:
使用Systemd命令配置service单元使其生效
顺序执行以下命令即可完成自启动设置sudo systemctl daemon-reload (重新加载配置)
sudo systemctl enable XXXX.service (将该单元加入开机启动列表)
注:每次修改完.service文件之后需要重新执行systemctl daemon-reload建议在配置完成之后进行手动service启动测试
sudo systemctl start XXXX(启动某service单元)
sudo systemctl status XXXX(查看某service单元的运行状态)
sudo systemctl stop XXXX(停止某service单元)