如何使用 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.
相关推荐
charlie11451419118 小时前
嵌入式现代C++教程: 构造函数优化:初始化列表 vs 成员赋值
开发语言·c++·笔记·学习·嵌入式·现代c++
凉、介21 小时前
SylixOS 中的 Unix Socket
服务器·c语言·笔记·学习·嵌入式·sylixos
三万棵雪松21 小时前
【AI小智硬件程序(四)】
人工智能·嵌入式·esp32·ai小智
不脱发的程序猿2 天前
嵌入式开发中C++内存泄漏的场景与解决办法
单片机·嵌入式硬件·嵌入式
Jason_zhao_MR2 天前
V2G 实战:SECC GreenPHY 通信开发方案
嵌入式硬件·物联网·嵌入式·边缘计算
Channon_3 天前
专题六:二进制日志的暗黑魔法——结构化数据与跨平台解析
嵌入式
fanged3 天前
Pico裸机9(bootrom_func)
嵌入式
雨疏风骤12403 天前
ROM与RAM,储存地址、链接地址以及运行地址
linux·stm32·嵌入式·linux嵌入式
不脱发的程序猿3 天前
SPI、DSPI、QSPI技术对比
单片机·嵌入式硬件·嵌入式
Channon_4 天前
专题五:实时系统的生死线——中断安全与优先级管理
嵌入式·优先级·中断安全