如何使用 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.
相关推荐
回眸&啤酒鸭5 小时前
【回眸】香橙派zero2 阿里云机器视觉分拣系统
嵌入式
明天见~~1 天前
数据结构学习(day01)
数据结构·学习·嵌入式
酷飞飞2 天前
使用keil点亮stc8核心板的灯
51单片机·嵌入式·stc8
撬动未来的支点3 天前
简化理解I2C总线
嵌入式·i2c
DIY机器人工房3 天前
一个可以检测本机的字节顺序,并对任意数据进行字节顺序的反转操作的代码。
嵌入式硬件·算法·嵌入式·diy机器人工房
DIY机器人工房5 天前
【科普】在STM32中有哪些定时器?
c语言·嵌入式·定时器·diy机器人工房
野原鑫之祝5 天前
嵌入式开发学习———Linux环境下IO进程线程学习(一)
linux·c语言·学习·vim·嵌入式
断水客6 天前
GCC链接技术深度解析:性能与空间优化
嵌入式·链接·编译·软件
野原鑫之祝6 天前
嵌入式开发学习———Linux环境下数据结构学习(五)
linux·c语言·数据结构·学习·vim·排序算法·嵌入式
Wanderpurr7 天前
搭建imx6ull环境--tftp加载镜像,nfs挂载根文件系统
ubuntu·嵌入式·虚拟机