Linux Service Management - System Running Level, rpm Startup and Self-Start, Source Package Startup

Posted by adi on Fri, 07 Jun 2019 20:34:10 +0200

System Running Level

  • runlevel View Run Level
  • init num sets the run level
Running level Meaning
0 Shutdown
1 Single-user mode, can be imagined as a security mode, mainly user system repair
2 Incomplete command-line mode
3 Complete command line mode, standard character interface
4 System retention
5 Graphic mode
6 Restart


[root@localhost ~]# init 3
[root@localhost ~]# runlevel
5 3

The traditional way to change the default runlevel is to change the / etc/inittab file. But in Centos7, the contents of the file are as follows:

# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

Indicates that the file is no longer working and can be modified by command:

  • multi-user.target: runlevel 3
  • graphical.target: runlevel 5

Use system CTL get-default to get the default level. Set the default level using system CTL set-default TARGET. target

Get the default runlevel

[root@localhost etc]# systemctl get-default
graphical.target

Set the default run level to character interface:

[root@localhost etc]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

View self-startup status

chkconfig –list
Looking at the service self-startup status, you can see all the services installed by RPM packages

[root@localhost rc.d]# chkconfig --list
NetworkManager  0:Close    1:Close    2:Enable    3:Enable    4:Enable    5:Enable    6:Close
abrt-ccpp       0:Close    1:Close    2:Close    3:Enable    4:Close    5:Enable    6:Close
abrtd           0:Close    1:Close    2:Close    3:Enable    4:Close    5:Enable    6:Close
acpid           0:Close    1:Close    2:Enable    3:Enable    4:Enable    5:Enable    6:Close
atd             0:Close    1:Close    2:Close    3:Enable    4:Enable    5:Enable    6:Close
auditd          0:Close    1:Close    2:Enable    3:Enable    4:Enable    5:Enable    6:Close
autofs          0:Close    1:Close    2:Close    3:Enable    4:Enable    5:Enable    6:Close

View booted software

  • Netstat-an Views All Ports, Views Connected Ports
  • Netstat-tulnp Views Opened Ports for Listening Status

How rpm starts software

  • Start through absolute path (standard startup mode)

    /etc/init.d/httpd start
    /etc/rc.d/init.d/httpd start

  • service command start (rethat system proprietary command)
    service httpd start|stop|restart|status

Set the self-starting state

  • chkconfig [- level] [independent service name] [on|off]
[root@localhost rc.d]# chkconfig --level 2345  httpd on
[root@localhost rc.d]# chkconfig --list | grep httpd
httpd           0:Close    1:Close    2:Enable    3:Enable    4:Enable    5:Enable    6:Close
  • Modify the / etc/rc.d/rc.local file so that the contents of the file can be executed at the end of system startup, and the command to start a specific service can be added. The final executed file.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

# Self-startup command
/etc/init.d/httpd start
  • ntsysv [level number] graphical tool, the same as chkconfig.

Changes made by chkconfig and ntsysv are synchronized, but changes made in the rc.local file are not visible in chkconfig

Self-startup of source package

Change the / etc/rc.d/rc.local file and append the startup command to the end of the file. The chkconfig command does not recognize the source package service.

Let the source package be identified by service

Copy the startup script of the source package into the init.d directory. To change the script, you need to be able to accept relevant parameters, such as start|stop, etc.

Let the service of source package be managed by chkconfig and ntsysv commands

  • Add # chkconfig: 3586 76 at the beginning of the script in the init.d directory

    chkconfig: Run-level startup sequence shutdown sequence (/etc/rc3.d/)

  • Add # description:... to the script.

    Explanation, free content

  • chkconfig --add script name adds the startup script under init.d to the command chkconfig

Relevant information

Teacher Tony's List of Linux Services

Topics: RPM Linux