如何使用 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.
相关推荐
zandy101116 小时前
嵌入式BI开发指南:如何通过衡石API将分析能力集成到业务系统?
开发语言·python·嵌入式
网易独家音乐人Mike Zhou2 天前
【Linux应用】交叉编译环境配置,以及最简单粗暴的环境移植(直接从目标板上复制)
linux·stm32·mcu·物联网·嵌入式·iot
憧憬一下2 天前
stm32之EXIT外部中断详解
stm32·单片机·嵌入式·中断
念风3 天前
移植Tensorflow Lite Micro源码到ArmClang Cortex-M
嵌入式
塞尔维亚大汉4 天前
【鸿蒙南向开发】OpenHarmony小型系统内核(LiteOS-A)【文件系统】下
物联网·嵌入式·harmonyos
塞尔维亚大汉4 天前
【鸿蒙南向开发】OpenHarmony小型系统内核(LiteOS-A)【扩展组件】下
物联网·嵌入式·harmonyos
司六米希5 天前
【操作系统】双缓冲机制(含原理、优势、实现方式、应用场景)
嵌入式
时光の尘5 天前
FreeRTOS菜鸟入门(六)·移植FreeRTOS到STM32
c语言·数据结构·stm32·单片机·嵌入式硬件·嵌入式
憧憬一下6 天前
搭建stm32工程
stm32·单片机·嵌入式
FreakStudio6 天前
一文速通Python并行计算:09 Python多进程编程-进程之间的数据同步-基于互斥锁、递归锁、信号量、条件变量、事件和屏障
单片机·嵌入式·大学生·面向对象·多进程·技术栈·并行计算·电子diy