如何使用 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.
相关推荐
Jason_zhao_MR15 小时前
米尔RK3506核心板SDK重磅升级,解锁三核A7实时控制新架构
linux·嵌入式硬件·物联网·架构·嵌入式·嵌入式实时数据库
Shawn_CH20 小时前
Linux 休眠时内核线程冻结机制说明
嵌入式
Shawn_CH20 小时前
Linux 内核线程冻结情况分析
嵌入式
才鲸嵌入式21 小时前
香山CPU(国产开源)的 SoC SDK底层程序编写,以及其它开源SoC芯片介绍
c语言·单片机·嵌入式·arm·cpu·verilog·fpga
MounRiver_Studio1 天前
RISC-V IDE MRS2使用笔记(八):手动切换文件编码
ide·mcu·嵌入式·risc-v
加成BUFF2 天前
树莓派安装下载及远程连接(共用手机热点)(SSH)(VNC)
linux·计算机·ssh·bash·树莓派·vnc
大聪明-PLUS2 天前
硬件断点:它们在 Linux 中的用途和工作原理
linux·嵌入式·arm·smarc
一杯原谅绿茶3 天前
3位6脚数码管的单片机例程
stm32·嵌入式
大聪明-PLUS3 天前
如何修补 Linux 内核:完整指南
linux·嵌入式·arm·smarc
大聪明-PLUS3 天前
Docker 内部机制:深入剖析
linux·嵌入式·arm·smarc