如何使用 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.
相关推荐
楼台的春风2 小时前
【图像亮度、对比度调整,直方图均衡化及图像平滑】
图像处理·人工智能·opencv·算法·计算机视觉·matlab·嵌入式
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
楼台的春风10 小时前
【图像的读写与基本操作】
图像处理·人工智能·深度学习·opencv·算法·计算机视觉·嵌入式
fanged12 小时前
CMake小结2(PICO为例)
学习·嵌入式
FreakStudio1 天前
开源一款DDS信号发生扩展板-FreakStudio多米诺系列
单片机·嵌入式·电子diy·电子计算机
FreakStudio2 天前
开源一款数据转换扩展板-FreakStudio多米诺系列
python·单片机·嵌入式·电子diy
楼台的春风2 天前
图像金字塔
图像处理·人工智能·opencv·算法·计算机视觉·matlab·嵌入式
m0_715646763 天前
无名管道、有名管道、信号、信号处理
java·linux·网络·数据结构·算法·嵌入式·标准io
楼台的春风4 天前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
Freak嵌入式5 天前
开源一款I2C电机驱动扩展板-FreakStudio多米诺系列
嵌入式硬件·嵌入式·智能硬件·开源硬件·micropython·电机驱动·电子模块