如何使用 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.
相关推荐
取啥都被占用24 分钟前
两个网络供应商靠树莓派跨个域
网络·树莓派
Raspberry_Pi_官方账号1 天前
Raspberry Pi+TinyML:基于边缘计算的低成本离线皮肤癌AI诊断系统
人工智能·边缘计算·树莓派·raspberry pi·tinyml
慧都小项1 天前
从 Figma 到 Qt:Qtitan 能减少哪些界面还原工作?
前端·qt·嵌入式·figma·图形界面·工业界面·qtitan
程序员打怪兽1 天前
完整的开发板使用流程
嵌入式
youcans_2 天前
【嵌入式软件AI编程】01. 基于 STM32/Claude Code的AI编程
stm32·mcu·嵌入式·ai编程·claude code
小智学长 | 嵌入式2 天前
实测GPT-6 Astra绘制硬件原理图和PCB效果分享
嵌入式·嵌入式开发·ai工具·单片机开发
一路往蓝-Anbo2 天前
第七篇:以太网设计 —— 从 RMII 时序到变压器隔离的深度拆解
stm32·单片机·嵌入式硬件·嵌入式·stm32硬件设计
玄奕子3 天前
三相逆变器系列(一):看懂三相全桥拓扑
嵌入式·dsp开发·电力电子·ti c2000·三相全桥逆变
the sun343 天前
嵌入式易错点:ARM指令架构、IP核、MCU厂商关系+BOOTLoader刷机原理
stm32·嵌入式
嵌入式阿蔡4 天前
CoAP / LwM2M 轻量协议:受限设备为什么不用 MQTT
单片机·物联网·嵌入式