Linux启动服务的方式

本文介绍了Linux系统中两种启动服务的方法:service命令,它通过执行/etc/init.d目录下的程序;以及systemctl命令,它是针对systemd初始化系统的进程管理工具,旨在提升系统启动速度并优化进程并发启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、service命令

service命令其实是去/etc/init.d目录下,去执行相关程序

查看/etc/init.d目录下有哪些文件
[root@VM_0_11_centos init.d]# ll /etc/init.d/
total 40
-rw-r--r-- 1 root root 18281 Mar 29  2019 functions
-rwxr-xr-x 1 root root  4569 Mar 29  2019 netconsole
-rwxr-xr-x 1 root root  7923 Mar 29  2019 network
-rw-r--r-- 1 root root  1160 Oct 19 00:48 README
[root@VM_0_11_centos init.d]# 
查看脚本文件都有哪些命令
# See how we were called.
case "$1" in
start)
    [ "$EUID" != "0" ] && exit 4
    rc=0
    # IPv6 hook (pre IPv4 start)
    if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
        /etc/sysconfig/network-scripts/init.ipv6-global start pre
    fi

    apply_sysctl

    #tell NM to reload its configuration
[root@VM_0_11_centos ~]# /etc/init.d/network start
Starting network (via systemctl):                          [  OK  ]
[root@VM_0_11_centos ~]# service restart network
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[root@VM_0_11_centos ~]# 

2、systemctl命令

 systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl

systemctl命令管理systemd的资源Unit

相关的服务文件放置在/usr/lib/systemd/system目录下
[root@VM_0_11_centos ~]# ll /usr/lib/systemd/system
total 1084
-rw-r--r--  1 root root  275 Aug 13 05:18 abrt-ccpp.service
-rw-r--r--  1 root root  380 Aug 13 05:18 abrtd.service
-rw-r--r--  1 root root  361 Aug 13 05:18 abrt-oops.service
-rw-r--r--  1 root root  266 Aug 13 05:18 abrt-pstoreoops.service
-rw-r--r--  1 root root  262 Aug 13 05:18 abrt-vmcore.service
-rw-r--r--  1 root root  311 Aug 13 05:18 abrt-xorg.service
-rw-r--r--. 1 root root  590 Apr 11  2018 acpid.service
-rw-r--r--  1 root root  275 Aug  9 09:10 arp-ethers.service
-rw-r--r--. 1 root root  222 Oct 31  2018 atd.service
-rw-r--r--  1 root root 1384 Aug  8 20:06 auditd.service
lrwxrwxrwx  1 root root   14 Dec  8 23:31 autovt@.service -> getty@.service
-rw-r--r--  1 root root  517 Oct 19 00:48 basic.target
drwxr-xr-x. 2 root root 4096 Dec  8 23:31 basic.target.wants
-r--r--r--  1 root root  429 Oct 19 00:05 blk-availability.service
-rw-r--r--  1 root root  379 Oct 19 00:48 bluetooth.target
-rw-r--r--  1 root root  160 Mar 29  2019 brandbot.path
-rw-r--r--  1 root root  116 Mar 29  2019 brandbot.service
-rw-r--r--  1 root root  209 Aug  6 21:44 chrony-dnssrv@.service
-rw-r--r--  1 root root  138 Aug  6 21:44 chrony-dnssrv@.timer
-rw-r--r--  1 root root  495 Aug  8 19:40 chronyd.service
-rw-r--r--  1 root root  472 Sep 19  2018 chrony-wait.service
-rw-r--r--  1 root root  391 Mar 12  2019 cloud-config.service
-rwxrwxrwx  1 root root  536 Jan 15  2019 cloud-config.target
-rw-r--r--  1 root root  439 Mar 12  2019 cloud-final.service
-rw-r--r--  1 root root  535 Mar 12  2019 cloud-init-local.service
-rw-r--r--  1 root root  626 Mar 12  2019 cloud-init.service
-rwxrwxrwx  1 root root  256 Jan 15  2019 cloud-init.target
-rw-r--r--  1 root root  386 Oct 19 00:02 conntrackd.service
-rw-r--r--  1 root root  787 Oct 19 00:48 console-getty.service
-rw-r--r--  1 root root  749 Oct 19 00:48 console-shell.service
-rw-r--r--  1 root root  808 Oct 19 00:48 container-getty@.service
-rw-r--r--  1 root root  294 Dec  7 00:00 cpupower.service
分析每个服务的启动时间
[root@VM_0_11_centos ~]# systemd-analyze blame
          4.635s kdump.service
          3.396s docker.service
          2.498s network.service
          1.842s systemd-journal-flush.service
          1.748s cloud-init-local.service
列出所有可用的units
[root@VM_0_11_centos ~]# systemctl list-units
  UNIT                                                          LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                             loaded active waiting   Arbitrary Executable File Formats File System Automount Point
  sys-devices-pci0000:00-0000:00:01.1-ata1-host0-target0:0:1-0:0:1:0-block-sr0.device loaded active plugged   QEMU_DVD-ROM config-2
  sys-devices-pci0000:00-0000:00:03.0-virtio0-net-eth0.device   loaded active plugged   Virtio network device
  sys-devices-pci0000:00-0000:00:04.0-virtio1-block-vda-vda1.device loaded active plugged   /sys/devices/pci0000:00/0000:00:04.0/virtio1/block/vda/vd
  sys-devices-pci0000:00-0000:00:04.0-virtio1-block-vda.device  loaded active plugged   /sys/devices/pci0000:00/0000:00:04.0/virtio1/block/vda
  sys-devices-platform-serial8250-tty-ttyS1.device              loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
  sys-devices-platform-serial8250-tty-ttyS2.device              loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device              loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:04-tty-ttyS0.device                       loaded active plugged   /sys/devices/pnp0/00:04/tty/ttyS0
  sys-devices-virtual-net-docker0.device                     
检查服务是否开启
[root@VM_0_11_centos ~]# systemctl is-enabled crond.service
enabled
[root@VM_0_11_centos ~]# systemctl start crond.service
[root@VM_0_11_centos ~]# systemctl stop crond.service
[root@VM_0_11_centos ~]# systemctl restart crond.service
[root@VM_0_11_centos ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-12-13 23:05:59 CST; 8s ago
 Main PID: 24871 (crond)
   Memory: 624.0K
   CGroup: /system.slice/crond.service
           └─24871 /usr/sbin/crond -n

Dec 13 23:05:59 VM_0_11_centos systemd[1]: Started Command Scheduler.
Dec 13 23:05:59 VM_0_11_centos crond[24871]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 71% if used.)
Dec 13 23:05:59 VM_0_11_centos crond[24871]: (CRON) INFO (running with inotify support)
Dec 13 23:05:59 VM_0_11_centos crond[24871]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
在引导时激活服务并启用或禁用服务(系统引导时自动启动服务)
[root@VM_0_11_centos ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@VM_0_11_centos ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@VM_0_11_centos ~]# systemctl is-active crond.service
active
[root@VM_0_11_centos ~]# systemctl stop crond.service
[root@VM_0_11_centos ~]# systemctl is-active crond.service
inactive
[root@VM_0_11_centos ~]# systemctl restart crond.service
[root@VM_0_11_centos ~]# systemctl is-active crond.service
active
[root@VM_0_11_centos ~]# 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值