如何使用 systemd 文件实现自启动

使用 systemd 文件实现自启动

/etc/systemd/system/目录下创建新.service文件:

bash 复制代码
$ sudo touch /etc/systemd/system/vid_tcp.service
$ sudo chmod 777  /etc/systemd/system/vid_tcp.service
$ code  /etc/systemd/system/vid_tcp.service

修改文件内容如下。ExeStart是需要执行的脚本

bash 复制代码
[Unit]
Description=auto executing vid script

[Service]
ExeStart=/home/wc/send_g/vid_scripts/cam_tcp_stream.sh
Restart=always

[Install]
WantedBy=multi-user.target

而后使服务文件生效并启用:

bash 复制代码
$ sudo systemctl daemon-reload
$ sudo systemctl enable vid_tcp.service
Created symlink /etc/systemd/system/multi-user.target.wants/vid_tcp.service → /etc/systemd/system/vid_tcp.service.

注意大小写,比如WantedBy写成wantedBy可能会在使能时报错:

bash 复制代码
$ sudo systemctl enable vid_tcp.service
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.
相关推荐
别了,李亚普诺夫10 小时前
MAX30102模块原理及代码实现
单片机·嵌入式
济61715 小时前
FreeRTOS 系统监控任务设计(上篇) ---MonitorTask的 基础框架
单片机·嵌入式·freertos
Jason_zhao_MR15 小时前
RK3576 MIPI Camera ISP调试:客观标定与环境准备(上)
人工智能·嵌入式硬件·机器人·嵌入式·接口隔离原则
济61716 小时前
MonitorTask 系统监控任务(下篇)---完善堆内存 、任务栈监控
单片机·嵌入式·freertos
2023自学中2 天前
从键盘输入 ./aaa 到程序运行,中间发生了什么?
linux·嵌入式
FreakStudio2 天前
工控开发板从开箱到点亮 LED-恩智浦MCXE31B 实测:3 路 CAN + 以太网+自带调试器
python·单片机·嵌入式·大学生·面向对象·技术栈·并行计算·电子diy·电子计算机
凉、介3 天前
Armv8-A virtualization 笔记 (一)
c语言·笔记·学习·嵌入式·虚拟化·hypervisor
不断提高4 天前
别再写 while(1) 死循环了,嵌入式开发该换个活法
c语言·嵌入式硬件·嵌入式·状态模式
济6174 天前
FreeRTOS传感器采集任务 ——SensorTask 传感器采集任务整体实现
stm32·单片机·嵌入式·freertos
济6174 天前
FreeRTOS 上报任务设计---UplinkTask 上行数据上报任务详解
stm32·嵌入式·freertos