0.systemd概念
可以看下面几篇:
https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
https://jingyan.baidu.com/article/cbcede07193a9102f40b4d3a.html
几乎所有systemctl 相关的命令的例子(很赞的排版):
https://linux.cn/article-5926-1.html
1.常用的命令:
sudo systemctl list-units --type=service # 只显示开启状态的service?
sudo systemctl list-unit-files --type=service # 会显示各种状态的service?
sudo systemctl start foo.service
sudo systemctl stop foo.service
sudo systemctl restart foo.service
sudo systemctl enable foo.service
sudo systemctl disable foo.service
sudo systemctl daemon-reload #更改配置文件之后,会提示需要执行此命令
更多systemctl 请参考这篇
2.相关文件
环境 ubuntu 16.04LTS
配置文件存在的方式有两类:
一类是和原先的rc0-6那套差不多,
在/etc/init.d
中有可执行脚本,负责service的start
stop
另一类是标准的systemctl的service文件,
在 /lib/systemd/system
中,以service作为后缀
一个service文件的例子:
[Unit]
Description=shadowsocks server
[Service]
Type=simple
ExecStart=/usr/local/bin/ssserver -c /etc/shadowsocks/config.json
[Install]
WantedBy=multi-user.target
另一个例子:ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Service]
EnvironmentFile=-/etc/default/ssh
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
[Install]
WantedBy=multi-user.target
Alias=sshd.service
~
这两种方式配置的服务,都可以使用1中提到的start
stop
restart