如何使用 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.
相关推荐
嵌入式分享6 小时前
嵌入式分享#38:踩坑实录!我在 RK3576 HDMI 搭错线
嵌入式
优信电子7 小时前
Raspberry Pi Pico 硬件开发 —— 第二课:MicroPython 从入门到项目实践(LED篇)
单片机·嵌入式硬件·树莓派·micropython·pico·入门教学
oku620891 天前
单片机底层系列:C 库运行时——从 libspace 到多任务与中断安全
嵌入式
Zwawa1 天前
反馈:让系统知道自己做得对不对
c语言·单片机·嵌入式
蓝天居士1 天前
PY32F系列MCU在OTA时App区概率性跑不起来的根因分析(2)
mcu·嵌入式
剑指offer.2 天前
ARM裸机开发-SPI
arm开发·嵌入式硬件·嵌入式·arm
___波子 Pro Max.2 天前
软件开发里的 prebuilt 是什么
嵌入式
7yewh3 天前
SLAM 三维空间刚体运动(2)
数据结构·人工智能·机器人·嵌入式·slam
LCG元3 天前
树莓派 Pico RP2040 PIO 逻辑分析仪实战:SM 指令周期拆解、DREQ 触发 DMA 环形缓冲与采样率参数实测
嵌入式·树莓派·rp2040
7yewh3 天前
SLAM 从视觉里程计到建图(6)
数据结构·人工智能·机器人·自动驾驶·嵌入式