Systemctl--Linux新的自启动方式
2021-11-15 04:48:21

启动示例

1
2
3
4
5
6
7
8
systemctl start $name
# 启动
systemctl stop $name
# 停止
systemctl enable $name
# 开启开机自启
systemctl disable $name
# 关闭开机自启

配置文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]           # 这个项目与此 unit 的解释、执行服务相依性有关
Description=OpenSSH server daemon
# 描述性内容
After=network.target
# 在网络启动后
Wants=sshd-keygen.service

[Service] # 这个项目与实际执行的指令参数有关
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install] # 这个项目说明此 unit 要挂载哪个 target 下面
WantedBy=multi-user.target

摘自这里的介绍

不想整理了,放在excel里了

链接

rssbot的配置文件示例

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=RSSBOT
After=network.target

[Service]
Type=simple
ExecStart=/root/rssbot/run.sh
Restart=always

[Install]
WantedBy=multi-user.target

systemd有系统和用户区分;系统(/user/lib/systemd/system/)、用户(/etc/lib/systemd/user/).

一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。

注意事项:

  1. [Service] 里面用绝对路径,如 sh换成 /usr/bin/sh
  2. systemctl status rssbot.service
    查看服务状态
  3. systemctl daemon-reload
    刷新服务状态

就这样了,做个笔记。

Prev
2021-11-15 04:48:21
Next