服务管理--systemctl命令
服务管理--systemctl命令
来源 http://my.oschina.net/5lei/blog/191370
| 任务 | 旧指令 | 新指令 |
| 使某服务自动启动 | chkconfig --level 3 httpd on | systemctl enable httpd.service |
| 使某服务不自动启动 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
| 检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active) |
| 显示所有已启动的服务 | chkconfig --list | systemctl list-units --type=service |
| 启动某服务 | service httpd start | systemctl start httpd.service |
| 停止某服务 | service httpd stop | systemctl stop httpd.service |
| 重启某服务 | service httpd restart | systemctl restart httpd.service |
下面以nfs服务为例:
1.启动nfs服务
systemctl start nfs-server.service
2.设置开机自启动
systemctl enable nfs-server.service
3.停止开机自启动
systemctl disable nfs-server.service
4.查看服务当前状态
systemctl status nfs-server.service
5.重新启动某服务
systemctl restart nfs-server.service
6.查看所有已启动的服务
systemctl list -units --type=service
开启防火墙22端口
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
如果仍然有问题,就可能是SELinux导致的
关闭SElinux:
修改/etc/selinux/config文件中的SELINUX=”” 为 disabled,然后重启
彻底关闭防火墙:
sudo systemctl status firewalld.service sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
要使用 systemctl 启动单个服务,其中包含多个进程,你可以使用 Systemd 的 template 机制。以下是使用 Systemd 'template' 以创建一个可同时启动多个进程的服务单元文件的过程:
-
为你的服务创建一个 template 服务单元文件。服务单元文件通常位于
/etc/systemd/system目录中。使用以下命令创建一个 template 服务单元文件:sudo touch /etc/systemd/system/your-service@.service注意
@符号,它表示这是一个模板服务文件。根据实际服务名称替换your-service。 -
编辑 template 服务单元文件。使用文本编辑器(如
nano、vim)打开服务单元文件,并为该服务配置适当的设置。sudo nano /etc/systemd/system/your-service@.service添加以下内容,根据您的实际需求进行修改:
[Unit] Description=Description of your service instance %i After=network.target [Service] User=user-to-run-the-service-instance Group=group-to-run-the-service-instance ExecStart=/path/to/your/program --program-options instance-%i Restart=always [Install] WantedBy=multi-user.target将
Description、User、Group和ExecStart替换为适当的值。请特别注意,我们添加了%i标记。这将在启动服务时替换为实例编号。 -
重新加载 Systemd 配置:
sudo systemctl daemon-reload -
使用
systemctl启动具有多个进程的单个服务:sudo systemctl start your-service@1 your-service@2 your-service@3在上述命令中,将
your-service替换为您在步骤 1 中创建的模板服务文件的名称,并使用所需的实例数量。这将启动多个进程,每个进程都具有不同的实例编号。
现在,你已经配置了一个包含多个进程的服务,并使用 systemctl 将它们同时启动。使用此方法,你可以根据需要动态地启动、停止或重启各个进程实例。
要在系统启动时自动运行这些服务实例,请分别为每个指定实例使用如下命令:
sudo systemctl enable your-service@1
sudo systemctl enable your-service@2
sudo systemctl enable your-service@3
替换 your-service 并指定所需实例号。
============ End

浙公网安备 33010602011771号