如何使用 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.
相关推荐
IAR Systems7 分钟前
瑞萨RH850与IAR:助力下一代“软件定义汽车”开发
汽车·嵌入式·iar
lularible3 小时前
从沙子到车辙(2.1):思想实验——在原始森林里造芯片
开源·嵌入式·汽车电子
lularible5 小时前
从沙子到车辙(2.4):CMOS——互补的智慧
开源·嵌入式·汽车电子
lularible5 小时前
从沙子到车辙(2.5):半导体制造工艺
开源·嵌入式·制造·汽车电子
一枝小雨19 小时前
RISC-V架构sp寄存器 & RISC-V架构下FreeRTOS任务上下文保存与恢复
单片机·架构·嵌入式·risc-v·rtos·内核原理
一枝小雨1 天前
RISC-V架构的中断与异常处理机制学习笔记
单片机·架构·嵌入式·risc-v·内核原理·中断与异常
兔兔怕被玩坏1 天前
LinCLI:给嵌入式设备装上一扇"对话窗口" 嵌入式cli工具实现
嵌入式
飞凌嵌入式1 天前
AIoT/储能/机器人全覆盖|FET3572-C助力行业设备快速升级
嵌入式
济6171 天前
I.MX6U Linux 驱动开发篇---设备树下 platform 驱动实验--- Ubuntu20.04
linux·嵌入式·嵌入式linux驱动开发
你疯了抱抱我1 天前
【自用】Kicad 导入嘉立创元器件封装(NLBN插件)
嵌入式硬件·嵌入式·pcb·电路·电子