如何使用 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.
相关推荐
阿钱真强道1 天前
25 鸿蒙LiteOS GPIO轮询模式实战教程:电平读取与上升沿检测
嵌入式·harmonyos·liteos·开源鸿蒙·瑞芯微·rk2206
秋越1 天前
从工程角度理解嵌入式C语言关键字
c语言·开发语言·嵌入式·嵌入式软件开发·嵌入式c语言·c语言关键字
bbaydnog1 天前
STM32调试三板斧:printf重定向、HardFault定位、逻辑分析仪抓波形,从此告别瞎改代码
stm32·嵌入式·调试
凉、介2 天前
深入理解 ARMv8-A|处理器模式与寄存器
笔记·学习·嵌入式·arm
2023自学中2 天前
imx6ull 开发板,手机,MQTT 物联网通信实验。
linux·服务器·物联网·嵌入式·开发板·应用编程
lularible2 天前
从沙子到车辙(4.5):时间同步与PTP
开源·嵌入式·汽车电子
求知喻2 天前
KEIL5进行MSPM0开发
嵌入式
lularible2 天前
从沙子到车辙(4.3):板级通信——CAN / CAN-FD
开源·嵌入式·汽车电子
程序员打怪兽2 天前
嵌入式C语言
嵌入式
凉、介2 天前
深入理解 ARMv8-A|异常/中断处理
笔记·学习·嵌入式·arm