服务管理--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' 以创建一个可同时启动多个进程的服务单元文件的过程:

  1. 为你的服务创建一个 template 服务单元文件。服务单元文件通常位于 /etc/systemd/system 目录中。使用以下命令创建一个 template 服务单元文件:

    sudo touch /etc/systemd/system/your-service@.service
    

    注意 @ 符号,它表示这是一个模板服务文件。根据实际服务名称替换 your-service

  2. 编辑 template 服务单元文件。使用文本编辑器(如 nanovim)打开服务单元文件,并为该服务配置适当的设置。

    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
    

    将 DescriptionUserGroup 和 ExecStart 替换为适当的值。请特别注意,我们添加了 %i 标记。这将在启动服务时替换为实例编号。

  3. 重新加载 Systemd 配置:

    sudo systemctl daemon-reload
    
  4. 使用 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

 

posted @   lsgxeva  阅读(305)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示