CentOS 7 Service Control and Optimized Startup Process

CentOS 7 System Service Control

In CentOS system, the control scripts of various system services are placed in the directory of / usr / lib / system by default. The system CTL command tool can control the specified system services.

  • System CTL command

    • Common formats

    System CTL [control type] [service name]

    • control type

    start: Run the specified system service program to realize the service function.

    stop: stop the specified system service program and shut down the corresponding functions.

    restart: Exit first, then run the specified system service program again. (Use cautiously)

    reload: do not exit the service program, just refresh the configuration.

    Status: View the running status of the specified system service and related information.

[root@localhost ~]# System CTL start httpd // start httpd service
[root@localhost ~]# System CTL status httpd // View httpd service status and related information
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since II 2019-08-27 14:18:52 CST; 32s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1896 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1896 /usr/sbin/httpd -DFOREGROUND
           ├─1899 /usr/sbin/httpd -DFOREGROUND
           ├─1900 /usr/sbin/httpd -DFOREGROUND
           ├─1901 /usr/sbin/httpd -DFOREGROUND
           ├─1903 /usr/sbin/httpd -DFOREGROUND
           └─1905 /usr/sbin/httpd -DFOREGROUND

8 February 2714:18:52 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
8 February 2714:18:52 localhost.localdomain httpd[1896]: AH00558: httpd: Could not ...
8 February 2714:18:52 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# System CTL stop httpd // close httpd service
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)                    //The service is closed
     Docs: man:httpd(8)
           man:apachectl(8)

8 February 2714:18:52 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
8 February 2714:18:52 localhost.localdomain httpd[1896]: AH00558: httpd: Could not ...
8 February 2714:18:52 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
8 February 2714:20:06 localhost.localdomain systemd[1]: Stopping The Apache HTTP Se...
8 February 2714:20:07 localhost.localdomain systemd[1]: Stopped The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
  • Note: For servers running in the actual production environment, do not easily perform stop or restart operations, in order to avoid interruption of client access and unnecessary losses.

Switching Run Level

The runlevel is the functional level at which the operating system is currently running. It lets some programs start at one level and not at another.

There are 10 kinds of effective login modes in Linux system from 0 to 9, but only 1 to 6 kinds of restrictions are generally valid when using UNIX system. From 1 to 6, it has different functions.

Running level Systemd's target Explain
0 target Shutdown status, the host will be shut down when using this level
1 rescue.target Single-user mode, no password validation is required to log on to the system, mostly for system maintenance.
2 multi-user.target User-defined/domain-specific runtime levels. Default equates to 3
3 multi-user.target Complete multi-user mode for character interfaces, at which most server hosts run
4 multi-user.target User-defined/domain-specific runtime levels. Default equates to 3
5 graphical.target The multi-user mode of graphical interface provides a graphical desktop operating environment
6 reboot.target Restart, the host will be restarted when using this level

1. View the current system runtime level

  • runlevel command
[root@localhost ~]# runlevel
N 5

2. Switching System Running Level

When the user needs to switch the system to other running levels, it can be done through the traditional init program, as long as the number corresponding to the running level (0-6) is used as the command parameter, or the system CTL command is used for target switching.

  • init program switching
[root@localhost ~]# init 3

  • System CTL tool switching
    • System CTL isolate graphical. target switch to graphical interface (inin 5)
    • System CTL isolate multi-user. target switch to character interface (inin 3)

  • Set boot start to enter character interface automatically.

    ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

    (Set boot to character interface)

    System CTL get-default: View the current boot level

[root@localhost ~]# System CTL get-default // / View the current startup level status
graphical.target                               //Graphical Interface
[root@localhost ~]# Ln-sf/lib/systemd/system/multi-user.target/etc/systemd/system/default.target// Set Open Start to Character Interface
[root@localhost ~]# System CTL get-default // / View the current startup level status
multi-user.target                           //Characterized interface

Optimizing startup process

Linux operating system contains a large number of service programs, which start or terminate according to the preset state when switching the running level. Many of these system services may not be required by users, but they also run by default.

So what system services are included by default in the Linux operating system? What are their respective roles? How to control the system services automatically running after boot-up in order to reduce resource occupancy and improve system operation efficiency?

1. Common System Services

In CentOS system, there are more than 100 kinds of system services installed by default, which provide users with rich application services. Only by understanding the purpose of each system service can we selectively optimize the operation and enable Linux services on demand.

Some common system services in CentOS:

Service Name Brief introduction to use Remarks
atd Delayed and scheduled execution of tasks Suggested closure
bluetooth Discover and authenticate Bluetooth-related devices Suggested closure
crond Execute planned tasks according to scheduled cycle Suggested Opening
irqbalance Scheduling support for multi-core CPU processors Suggested Opening
kdump Recording memory information when the kernel crashes Suggested closure
lvm2-monitor LVM Management and Monitoring Suggested Opening
netfs Accessing network file systems such as shared folders Suggested Opening
network Configuration and Use of Network Card and Network Address Suggested Opening
restorecond SELinux File Monitoring and Recovery Function of Security Mechanism Suggested closure
rhnsd Access Red Hat Network, get notifications, submit subscriptions, etc. Suggested closure
rpcgssd Client Program Context in Managing NFS Access Suggested closure
saslauthd Text-based Identity Authentication Suggested closure
smartd Monitor the status of local hard disk and send failure report Suggested Opening
smb File Sharing Service Suggested closure
sshd Provide the function of remote login and management of Linux host Suggested Opening
rsyslog Logging messages for the kernel and system Suggested Opening
vsftpd Provide file upload and download function through FTP Suggested closure
  • Note: It must be emphasized that whether these services are open or closed should be determined according to the actual functional requirements of the host, and not rigidly copied.

2. Optimizing Boot-Up Automatic Loading Services

The Linux operating system enters the default system d running target (such as character mode or graphics mode) after each boot, and runs various system services that are set to start by default in the target. To prohibit some system services from running automatically, you can use ntsysv or system CTL tools for optimization.

  • ntsysv tool

    Ntsysv tool can run in character mode, providing users with an interactive graphical interface for centrally configuring the startup status of various system services. It is very convenient to use ntsysv tools when multiple services need to be started at the same time.

[root@localhost ~]# ntsysv // Input Command Enter Imitated Graphic Interactive Operating Interface

Press the_and_direction keys to select different system services, press the Space (space) key to set the default startup status of services ([*] means startup, [] means shutdown). If you want to view the description information of the selected service, press F1 to get help.

  • System CTL tool

    The system CTL tool is similar to ntsysv in function, but it does not provide an interactive operation interface. It is used to query or set the default startup status of system services. systemctl tools are more efficient when you need to set the default startup status of a service in the current running target.

    • Command format

    • System CTL control option service name
    • Common Options
      • enable: Start automatically.
      • Disabled: Turn on and turn off automatically.
      • is-enable: Check boot status.
[root@localhost ~]# System CTL enable httpd. service // Set up boot to automatically start httpd service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# System CTL IS-enabled httpd.service//View httpd boot status
enabled
[root@localhost ~]# System CTL disable httpd. service // / Set boot not to start httpd service
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@localhost ~]# System CTL IS-enabled httpd.service////View httpd boot status
disabled

Supplementary Contents

Executing the system CTL list-units -- type = service command under the current system allows you to view all active system services in the current system.

[root@localhost ~]# System CTL list-units -- type = service // / view all active system services in the current system
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
abrt-ccpp.service                  loaded active exited  Install ABRT coredump hook
abrt-oops.service                  loaded active running ABRT kernel log watcher
abrt-xorg.service                  loaded active running ABRT Xorg log watcher
abrtd.service                      loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service            loaded active running Accounts Service
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service           loaded active exited  Availability of block devices
...//Eliminate part of the content.

Thank you for watching, I hope it will be helpful to you!!!

Keywords: Linux Apache network CentOS

Added by php_tom on Tue, 27 Aug 2019 13:03:32 +0300