美文网首页linuxTECHlinux
service和systemctl的区别

service和systemctl的区别

作者: virtual灬zzZ | 来源:发表于2022-06-19 14:10 被阅读0次

    systemctl基本上是的更强大的版本service

    Linux服务管理的两种方式service和systemctl。

    service

    service命令其实是去/etc/init.d目录下,去执行相关程序,init.d目录包含许多系统各种服务的启动和停止脚本。当Linux启动时,会寻找这些目录中的服务脚本,并根据脚本的run level确定不同的启动级别。参考这篇文章,了解系统启动的过程及centos和ubuntu的区别。

    service的常用方式:

    1.格式:service <service>
    打印指定服务<service>的命令行使用帮助。
    2.格式:service <service> start
    启动指定的系统服务<service>
    3.格式:service <service> stop
    停止指定的系统服务<service>
    4.格式:service <service> restart
    重新启动指定的系统服务<service>,即先停止(stop),然后再启动(start)。
    5.格式:chkconfig --list
    查看系统服务列表,以及每个服务的运行级别。
    6.格式:chkconfig <service> on
    设置指定服务<service>开机时自动启动。
    7.格式:chkconfig <service> off
    设置指定服务<service>开机时不自动启动。
    8.格式:ntsysv

    systemctl

    systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

    在systemd管理体系中,被管理的deamon(守护进程)称作unit(单元),对于单元的管理是通过命令systemctl来进行控制的。unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息。

    用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止;

    在systemctl中,也类似,文件目录有所不同,在/lib/systemd/system目录下创建一个脚本文件redis.service,里面的内容如下


    创建软链接
    创建软链接是为了下一步系统初始化时自动启动服务
    ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
    刷新配置
    刚刚配置的服务需要让systemctl能识别,就必须刷新配置
    sudo systemctl daemon-reload

    启动、重启、停止
    启动redis:systemctl start redis
    重启redis:systemctl restart redis
    停止redis: systemctl stop redis

    开机自启动
    redis服务加入开机启动:systemctl enable redis
    禁止开机启动:systemctl disable redis
    查看状态:systemctl status redis

    相关文章

      网友评论

        本文标题:service和systemctl的区别

        本文链接:https://www.haomeiwen.com/subject/xzsrvrtx.html