如何使用 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.
相关推荐
想躺在地上晒成地瓜干12 小时前
树莓派超全系列文档--(14)无需交互使用raspi-config工具其一
linux·树莓派·raspberrypi·树莓派教程
相醉为友13 小时前
001 使用单片机实现的逻辑分析仪——吸收篇
笔记·单片机·嵌入式硬件·fpga开发·嵌入式
别说我什么都不会13 小时前
OpenHarmony解读之设备认证:sts协议-客户端发起start请求
物联网·嵌入式·harmonyos
别说我什么都不会15 小时前
OpenHarmony解读之设备认证:pake协议-客户端接收end响应
物联网·嵌入式·harmonyos
Ki138116 小时前
将树莓派5当做Ollama服务器,C#调用generate的API的示例
linux·树莓派·ollama
飞凌嵌入式17 小时前
从DeepSeek到Qwen,AI大模型的移植与交互实战指南
人工智能·aigc·嵌入式
别说我什么都不会19 小时前
OpenHarmony解读之设备认证:pake协议-服务端响应pake end请求
物联网·嵌入式·harmonyos
塞尔维亚大汉19 小时前
OpenHarmony之分布式软总线discovery_service.c(二)
分布式·嵌入式·harmonyos
FreakStudio1 天前
一文速通 Python 并行计算:05 Python 多线程编程-线程的定时运行
单片机·嵌入式·多线程·面向对象·并行计算·电子diy
别说我什么都不会1 天前
OpenHarmony解读之设备认证:pake协议-客户端发起end请求
物联网·嵌入式·harmonyos