如何使用 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.
相关推荐
en.en..8 小时前
第一篇:位域打包:看懂 I2C 地址左移的底层逻辑
stm32·嵌入式·位运算·i2c·底层驱动
youcans_8 小时前
【嵌入式软件AI编程】21. 整理通用提示词模板
stm32·单片机·嵌入式·ai编程
番茄灭世神11 小时前
PN学堂GD32课程第15篇——HardFault异常
c语言·单片机·嵌入式
LCG米1 天前
STM32串口接收卡死根因排查:ORE溢出标志陷阱、错误标志清除顺序与中断恢复实测
stm32·单片机·嵌入式硬件·串口·嵌入式
阿源-1 天前
从 INF、DSC、DEC 到 FDF:理解 EDK II 的 UEFI 编译与固件生成流程
嵌入式·uefi·edk2·固件
2035去旅行1 天前
SOC 里的 CPU 处理器 和 L1/L2 Cache
嵌入式·arm·soc
优信电子1 天前
STM32 驱动 SHT35 温湿度传感器:从 I²C 通信到 CRC 校验的完整实战
单片机·嵌入式
Hotpot1 天前
3 线程管理和调度
嵌入式
一直C1 天前
嵌入式 Linux 驱动开发学习笔记:设备树、pinctrl 与 GPIO 驱动开发
驱动开发·嵌入式·arm·dt
blueSatchel1 天前
framebuffer注册源码解读
linux·嵌入式