Failed to restart **: Unit **.service not found错误,

背景

执行命令systemctl --user start **提示Failed to restart **: Unit **.service not found., 知道systemctl这个命令是启动服务, 现在提示没有找到此服务,看启动的服务名像自己定义的,然后就大致了解了一下如何使用systemd注册服务, 需要到指定目录下配置注册服务文件

注册服务文件

~/.config/systemd/user/目录下穿件文件test.service, 文件内容如下, 之后执行systemctl --user start test就能启动test.service, systemctl --user stop test停止test.service

[Unit]
Description=service description
After=network.target
Requires=network.target
Wants=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
WorkingDirectory=%h/.config/fah
ExecStart=/usr/bin/FAHClient
Environment="PATH=/usr/lib/ccache/bin:/usr/local/bin:/usr/bin:/bin"
Environment="BROWSER=chrome"
EnvironmentFile=/opt/test/.env

[Install]
WantedBy=default.target

Unit

Description: 服务的描述
Requires:A依赖B服务并且B在A之前运行,那么可以在A中写Requires=B 和 After=B
Wants:如果A和B中的依赖不是必须的,那么可以在A中写Wants=B 和 After=B
After: 如果没有指明After,两个服务并行运行

Service

Type: 默认值是simple
Restart: 自动重启策略, 可设置值有no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, or always,默认值是no
RestartSec: 设置重启的睡眠时间
WorkingDirectory: 工作目录
ExecStart: 启动命令
Environment: 环境变量设置
EnvironmentFile: 环境变量设置文件

文件路径

  • /usr/lib/systemd/user/ : 已安装软件包服务路径
  • /etc/systemd/user/ :系统用户 注册服务路径,需要有管理员权限
    where system-wide user units are placed by the system administrator.
  • ~/.config/systemd/user/ 当前用户注册服务路径

参考文档: systemd, https://www.freedesktop.org/software/systemd/man/systemd.service.html

你可能感兴趣的:(linux,学习笔记,linux)