systemd使用教程

systemd使用教程

常用指令

  • 运行一个服务: systemctl start <服务名>
  • 关闭一个服务: systemctl stop <服务名>
  • 重启一个服务: systemctl restart <服务名>
  • 显示一个服务(无论运行与否)的状态: systemctl status <服务名>
  • 在开机时启用一个服务: systemctl enable <服务名>
  • 在开机时禁用一个服务: systemctl disable <服务名>
  • 更新服务列表:systemctl daemon-reload
  • 查看已激活的服务:systemctl list-units -t service

加载自己的service到列表

将自己写好的service放入以下路径

cp mytest.service /lib/systemd/system/

测试自己的service

sudo systemctl start <服务名>
systemctl status <服务名>

开启成功:

1月 08 00:54:42 ubuntu systemd1: Started windtest.
1月 08 00:54:42 ubuntu test[2816]: Hello!!!

开启失败:

Failed to start wind-test.service1.service: Unit wind-test.service1.service not found.

● wind-test.service1.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)

开机自启动service

sudo systemctl enable <服务名>

检查一个服务是否是开机启用:

systemctl is-enabled <服务名>; echo $?

0 表示已开机启用, 1 表示没有开机启用

修改service

1.更新列表

systemctl daemon-reload

2.执行相关操作,例如打开服务

systemctl start test.service

最后附上一段systemd的代码

wind-base.service 内容如下:
1.这个代码的意思是开启网络后开启SSH-Service

[Unit]
Description=Wind-BaseService like SSH Server
After=network.target

[Service]
ExecStart=/etc/init.d/ssh start
Type=simple  
RemainAfterExit=yes
KillMode=process

[Install]
WantedBy=multi-user.target

wind-test.service 内容如下:
1.退出后继续执行改程序
2.启动的进程不管如何关闭,5s后都会重启(这里有问题,我用kill -9后貌似不能自动重启进程,求解!)

[Unit]
Description=windtest
After=network.target

[Service]
ExecStart=/wind-test/test
Type=simple  
RemainAfterExit=yes
KillMode=process
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

具体的格式及关键字意义请移步 这里


随笔:

关于systemd学习过程:

学习这玩意的过程有点郁闷,网上一堆他的教程。全是关键字的含义,和大篇幅搬运官方的帮助说明文档,看半天也不知道怎么动手去写。只有上面推荐的连接里稍微好点,写的不错。但是!!!我最终写好了service,你得告诉我怎么用啊·····
也就是怎么去加载这个节点啊?找了半天都没有,都讲得很含糊。于是乎我搜索了下wpa_supplicant.service

find / -name wpa_supplicant.service
find: `/run/user/1000/gvfs': 权限不够
/lib/systemd/system/wpa_supplicant.service

于是就知道怎么做了吧。哈哈

关于ssh-service的自启动

貌似Open-SSH本身就支持systemd,所以你可以直接设置成开机启动。
(这句指令表示启动整个ssh组。)

systemctl enable ssh@

简单介绍下ssh sshd ssh-agent

ssh 是客户端,即ssh-client
sshd 是服务器端,即ssh-server
ssh-agent 是一种控制,用来保存公钥身份验证所使用的私钥的程序


2018年01月08日 01时59分28秒 Wind

posted @ 2018-01-08 02:35  WittXie  阅读(1608)  评论(0编辑  收藏  举报