如何使用 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.
相关推荐
高旭的旭3 小时前
Linux V4L2框架详解:Camera软件架构与驱动实现
linux·嵌入式·camera·v4l2
大聪明-PLUS6 小时前
TCP/IP 协议族—理论与实践(二)
linux·嵌入式·arm·smarc
北京迅为1 天前
【北京迅为】iTOP-4412精英版使用手册-第三十五章 WEB控制LED
linux·嵌入式硬件·嵌入式·4412
文火冰糖的硅基工坊1 天前
[嵌入式系统-78]:嵌入式系统的硬件组成
嵌入式
爱思德学术2 天前
中国计算机学会(CCF)推荐学术会议-B(计算机网络):SenSys 2026
人工智能·计算机网络·嵌入式·传感系统
嵌入式分享2 天前
嵌入式分享#41:RK3576改UART波特率【精简版】
linux·嵌入式硬件·ubuntu·嵌入式
嵌入式爱好者hsw3 天前
记录gitee的使用
嵌入式
SundayBear3 天前
Qt 开发修炼指南:从入门到通透的实战心法
开发语言·qt·嵌入式
牛马大师兄3 天前
STM32独立看门狗IWDG与窗口看门狗WWDG知识梳理笔记
笔记·stm32·单片机·嵌入式硬件·嵌入式·看门狗
大聪明-PLUS4 天前
如何从 USB 闪存驱动器安装 Debian Linux
linux·嵌入式·arm·smarc