如何使用 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.
相关推荐
FreakStudio10 小时前
不用费劲编译ulab了!纯Mpy矩阵micronumpy库,单片机直接跑
python·嵌入式·边缘计算·电子diy
Zevalin爱灰灰1 天前
零基础入门学用物联网(ESP8266) 第一部分 基础知识篇(三)
单片机·物联网·嵌入式·esp8266
优信电子2 天前
ESP32开发板单向点对点ESP-NOW无线通信
单片机·嵌入式·arduino
国产化创客2 天前
OpenClaw在树莓派DHT11数据采集任务过程全记录
ai·树莓派·智能硬件·openclaw
FreakStudio2 天前
保姆级 uPyPi 教程|从 0 到 1:MicroPython 驱动包一键安装 + 分享全攻略
python·嵌入式·电子diy
dys_Codemonkey2 天前
如何在树莓派上用 VS Code 优雅直连内部的 Ubuntu 子系统/容器用来访问容器内的文件和代码?
linux·运维·ubuntu·树莓派
GetcharZp2 天前
只有代码是不够的:手搓手机第一课,看懂那条“看不见的河流”
嵌入式
busideyang3 天前
数据手册和参考手册区别
stm32·单片机·嵌入式硬件·嵌入式
vockydesign3 天前
SUBDEV驱动的set_fmt和get_fmt实现
嵌入式
网易独家音乐人Mike Zhou3 天前
【嵌入式基础】Keil自动编译脚本及环境变量配置
c语言·stm32·单片机·51单片机·嵌入式·keil