如何使用 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.
相关推荐
kyle~4 小时前
嵌入式 SoC 系统
linux·c++·嵌入式·计算系统
jianqiang.xue8 小时前
1颗NPU分时复用:鸿蒙把轻AI塞进了MCU
人工智能·单片机·mcu·嵌入式·harmonyos·鸿蒙·端侧ai
蓝天居士11 小时前
JY61P认识、学习与实战(3)
机器人·嵌入式·传感器
feasibility.12 小时前
399美元桌面机器人Doly:可爱电子宠物+树莓派开源平台,但不能像ChatGPT那样聊天
机器人·开源·嵌入式·树莓派·diy·桌宠
Zwawa2 天前
陀螺仪数据为什么看起来总在动:零偏、噪声和零漂
c语言·单片机·嵌入式
LCG元3 天前
串口乱码,先算波特率误差再动代码
c语言·嵌入式
2035去旅行3 天前
ADB的底层原理
linux·adb·嵌入式·arm·soc
Zwawa5 天前
让屏幕动起来:OLED动画效果,丝滑光标、帧动画和一只贪吃蛇
c语言·单片机·嵌入式
俊俊几年的5 天前
动态规划(Dynamic Programming)详解:从入门到 LeetCode Hot 100
linux·c++·算法·leetcode·嵌入式·动态规划·c
feasibility.5 天前
LeCun团队世界模型JEPA首登无人机:9K参数模型,零样本Sim-to-Real飞入100Hz闭环
论文阅读·嵌入式·无人机·具身智能·世界模型·杨立昆·jpea