如何使用 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.
相关推荐
fanged20 小时前
Yocto1--环境搭建和验证
大数据·搜索引擎·嵌入式
捷瑞电子工坊1 天前
FreeRTOS中断管理:从原理到实践(标准库版)
嵌入式·freertos·实时操作系统·任务通知·临界区·中断管理·二值信号量
dozenyaoyida1 天前
LittleFS 嵌入式文件系统机制拆解与某 RISC-V MCU 实测配置
单片机·嵌入式·文件系统·risc-v·littlefs·flash存储
江洋之道2 天前
嵌入式LVGL UI架构实践:MVP分层 + 双链表路由,构建低耦合、易维护的嵌入式界面
c语言·架构·嵌入式·lvgl
小柯博客2 天前
01 · 点亮 STM32MP25 的硬件视频编解码:VPU 与那些“安静“的坑
c语言·笔记·stm32·单片机·嵌入式硬件·嵌入式·视频编解码
捷瑞电子工坊3 天前
FreeRTOS事件组:多任务同步的利器
stm32·嵌入式·freertos·实时操作系统·标准外设库·事件组·任务同步
大熊背4 天前
树莓派相机自动白平衡详解(四)
算法·树莓派·白平衡·isppipeline
小嵌同学4 天前
Linux 内核中的 Per-CPU 机制详解
linux·运维·arm开发·嵌入式
jufeng13074 天前
【系列:uC/OS-II 内核源码精读:从 6736 行代码看懂一个 RTOS · 第 3 篇】
嵌入式·实时操作系统·rtos·uc/os-ii
捷瑞电子工坊5 天前
FreeRTOS任务管理:从入门到实践(标准库版)
stm32·嵌入式·freertos·实时操作系统·任务管理·标准外设库·任务挂起与恢复