北京SEO

linux中service与chkconfig的替代者systemctl

2019/10/10/17:47:10  阅读:1765  来源:谷歌SEO算法  标签: SEO技术

linux中有很多命令已经存在了N多年,渐渐一些已被一些新命令所代替,不过由于习惯的原因,很多时候我们并不能一下子适应过来 ,例如ifconfig之于ip命令。

最近在玩ubuntu和opensuse时发现了systemctl命令了,后来在试玩centos7时也发现了该命令,systemctl是systemd下的一个工具。网上查了下,该命令已经存在很久了。该命令是用来替代service和chkconfig两个命令的 --- 尽管个人感觉还是后者好用。

为了顺应时间的发展,这里总结下。在目前很多linux的新发行版本里,系统对于daemon的启动管理方法不再采用SystemV形式,而是使用了sytemd的架构来管理daemon的启动。

一、runlevel 到 target的改变

在systemd的管理体系里面,以前的运行级别(runlevel)的概念被新的运行目标(target)所取代。tartget的命名类似于multi-user.target等这种形式,比如原来的运行级别3(runlevel3)就对应新的多用户目标(multi-user.target),run level 5就相当于graphical.target。

由于不再使用runlevle概念,所以/etc/inittab也不再被系统使用 --- 无怪乎在新版本ubuntu上找不到inittab文件了。

而在systemd的管理体系里面,默认的target(相当于以前的默认运行级别)是通过软链来实现。如:

ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

在/lib/systemd/system/ 下面定义runlevelX.target文件目的主要是为了能够兼容以前的运行级别level的管理方法。 事实上/lib/systemd/system/runlevel3.target,同样是被软连接到multi-user.target。

注:opensuse下是在/usr/lib/systemd/system/目录下。

二、单元控制(unit)

在systemd管理体系里,称呼需要管理的daemon为单元(unit)。对于单元(unit)的管理是通过命令systemctl来进行控制的。例如显示当前的处于运行状态的unit(即daemon),如:

  1. #systemctl
  2. #systemctl--all
  3. #systemctllist-units--type=sokect
  4. #systemctllist-units--type=service

注:type后面可以接的类型可以通过help查看:

  1. 361way:~#systemctl-thelp
  2. Availableunittypes:
  3. service
  4. socket
  5. target
  6. device
  7. mount
  8. automount
  9. snapshot
  10. timer
  11. swap
  12. path
  13. slice
  14. scope

三、systemctl用法及示例

chkconfig、service命令与systemctl命令的区别见下表:

任务 旧指令 新指令

使某服务自动启动 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 --add test systemctl load test.service

删除服务 chkconfig --del xxx 停掉应用,删除相应的配置文件

显示所有已启动的服务 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

注:systemctl后的服务名可以到/usr/lib/systemd/system目录查看(opensuse下),其他发行版是位于/lib/systemd/system/ 下。

  1. //opensuse下
  2. 361way:~#systemctlstatusnetwork.service
  3. network.service-LSB:Configurenetworkinterfacesandsetuprouting
  4. Loaded:loaded(/usr/lib/systemd/system/network.service;enabled)
  5. Active:active(exited)sinceMon2014-09-0120:05:45CST;2h14minago
  6. Process:1022ExecStart=/etc/init.d/networkstart(code=exited,status=0/SUCCESS)
  7. Sep0120:05:15linux-pnp8systemd[1]:StartingLSB:Configurenetworkinterfacesandsetuprouting...
  8. Sep0120:05:15linux-pnp8network[1022]:Settingupnetworkinterfaces:
  9. Sep0120:05:15linux-pnp8network[1022]:lo
  10. Sep0120:05:15linux-pnp8network[1022]:loIPaddress:127.0.0.1/8
  11. Sep0120:05:45linux-pnp8network[1022]:..done..done..doneSettingupservicenetwork...............done
  12. Sep0120:05:45linux-pnp8systemd[1]:StartedLSB:Configurenetworkinterfacesandsetuprouting.
  13. //centos下--phpfensi.com
  14. #systemctlstatushttpd.service
  15. httpd.service-TheApacheHTTPServer(preforkMPM)
  16. Loaded:loaded(/lib/systemd/system/httpd.service;disabled)
  17. Active:inactive(dead)<--表示未启动
  18. CGroup:name=systemd:/system/httpd.service

上面两个命令相当于/etc/init.d/network status 和 /etc/init.d/httpd status,opensuse和centos下的用法相同,只不过显示的路径不同。其他操作类似。

四、service配置文件

还以上面提到的httpd.service配置为例,httpd.service文件里可以找到如下行:

[Install]

WantedBy=multi-user.target

则表明在多用户目标(multi-user.target,相当于level3)时自动启动,如果想在runlevel 5下也自启动,则可以将配置改为如下:

[Install]

WantedBy=multi-user.target graphical.target

一旦设定了自动启动(enbale),就在/etc/systemd/system/.wants/下面建了一个httpd.service的软连接,连接到/lib/systemd/system/下的相应服务那里,所以显示自动启动状态的unit(类似于chekconfig --list命令的结果),可以通过下面的方法:

#ls /etc/systemd/system/multi-user.target.wants/

systemctl的总结先写到这里,其是systemd包内的一个工具,也是该包中最为常用的工具。回头再针对systemd做一个总结.

广告内容

linux中service与chkconfig的替代者systemctl linux中service与chkconfig的替代者systemctl linux中service与chkconfig的替代者systemctl

相关阅读

热门评论

萧红SEO 萧红SEO

SEO爱好者,分享SEO经验~

总篇数212

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法