Administering the service process with the Services command and the Chkconfig command

Source: Internet
Author: User
Tags fpm

Service commands can quickly turn on and stop services in Linux, which is useful during debugging. The Chkconfig command quickly sets up a service program that needs to be started automatically when it is powered on.
The functions of both commands can be implemented in other ways, but they are more convenient and easier to maintain.

Service commands and process management scripts

In Debian Linux, if you install a service program such as Mysql,nginx with the Apt-get command , the installer will automatically create a shell script in the/etc/init.d/directory that manages this service process, such as:

/etc/init.d/mysql/etc/init.d/nginx/etc/init.d/keepalived

  

This is done using the /etc/init.d/{script file name} start or service {script file name} start to start a service, such as:

#启动mysql服务/etc/init.d/mysql start service MySQL start

  

If the process management script supports the restart command parameter, you can also restart a service with the /etc/init.d/{script file name} Restart or service {script file name} restart Such as:

#重新启动mysql服务/etc/init.d/mysql Restart service MySQL restart

  

The effect of the above two commands is the same, so when restarting MySQL or php-fpm it is not necessary to kill the process first, and then write a large segment of the path and parameters to start the service. Just remember the script file name with the service command, do not write absolute address, so it is more convenient, The default script file name is the same as the name of the service program.

if you compile and install PHP-FPM and other services , this script in the/ETC/INIT.D directory needs to be added yourself. Fortunately, many of the service program's source packages are included with this script, such as:

mysql5.6 Source of Support-files/mysql.server PHP7 source code in the SAPI/FPM/INIT.D.PHP-FPM redis2.6 source of Utils/redis_init_script

  

These are the process management scripts provided by the application, which are copied to the/ETC/INIT.D directory, and a short name can be used to manage the service process with the Services command. Don't forget to add executable permissions to scripts copied to/ETC/INIT.D.

chmod +x/etc/init.d/mysql/etc/init.d/nginx/etc/init.d/redis

  

Sometimes it is necessary to modify the parameters in the script, such as path name, to execute smoothly, need simple debugging.

we need to be aware the parameters after the service command
The first parameter after the service command must be the same as the script name in/ETC/INIT.D, or the system will error, in order to be simple, can be directly named as the service program name.
The second parameter of the service command, such as Start,stop,restart, is passed to the process management script. So whether this parameter is supported is not determined by the service command, is determined by the process management script, and uses a different process to manage the script. The command parameters and functions that can be selected are also different.
The process management script supports start and stop two command parameters, as well as other command parameters. You can generally view the script's help information with the service {script name} , which has the supported command parameters such as:

[email protected]:~# ls-l/etc/rc2.d/total 4-rw-r--r--1 root root 677 Nov readmelrwxrwxrwx 1 root Root: S01ipvsadm. /INIT.D/IPVSADMLRWXRWXRWX 1 root root 6 13:45 s16apache2. /init.d/apache2lrwxrwxrwx 1 root root: 16:31 s17keepalived. /INIT.D/KEEPALIVEDLRWXRWXRWX 1 root root June 13:54 s17memcached. /INIT.D/MEMCACHEDLRWXRWXRWX 1 root root 6 13:45 s17mysql. /init.d/mysqllrwxrwxrwx 1 root root 13:14 s17nagios-nrpe-server. /INIT.D/NAGIOS-NRPE-SERVERLRWXRWXRWX 1 root root 6 13:45 S17nginx. /INIT.D/NGINXLRWXRWXRWX 1 root root 6 13:45 s17rsync. /INIT.D/RSYNCLRWXRWXRWX 1 root root 6 13:45 s17vsftpd. /init.d/vsftpdlrwxrwxrwx 1 root root 3 14:06 S18avahi-daemon. /INIT.D/AVAHI-DAEMONLRWXRWXRWX 1 root root 6 13:45S18exim4. /INIT.D/EXIM4LRWXRWXRWX 1 root root 6 13:45 s20rc.local. /init.d/rc.local

  

There are also services, such as Apt-get-installed keepalived management scripts, which can be used to see no help information. To know what command parameters are supported in this script, only to read the script yourself, the syntax of the shell script is relatively simple, or very easy to read.

If you need to compile and install the service program, and there is no process management script in the source code, such as memcached. You can only check the script that someone else has written. Otherwise, write a process management script is also good ~

Chkconfig Commands and/etc/rc.local scripts

The Chkconfig command can be used to configure whether a service is started automatically, and some versions of Linux need to manually install this command, such as installing the Chkconfig command in Debian Linux:

Apt-get Install Chkconfig

  

If there is no chkconfig command, want to add a start-up service automatically, you can open the service command into the/etc/rc.local, but with chconfig command more convenient
The Chkconfig command also relies on the process management scripts placed in the/ETC/INIT.D directory.
Chkconfig Command Example:

#以下脚本在debian Linux tested via Chkconfig--help #查看chkconfig命令语法chkconfig-L #查看所有服务程序在所有运行级别下的是否开机自动启动chkconfig-L MySQL # Check if MySQL service starts automatically at all operating levels chkconfig-a MySQL #在服务列表中添加新的服务mysql并设置成开机自动启动chkconfig-d MySQL # Canceling the MySQL service boot auto-start setting chkconfig-s mysql 2345 #让mysql在2345这几个运行级别下开机自动启动, if a link under a certain runlevel is not added correctly, you can add it using this command.

  

  the principle of chkconfig

The/etc directory has a set of RC start-up directories, which are used to store services that are automatically started by Linux at various operating levels:

/etc/rcs.d/  #开机后需要自动启动的一些基本服务/etc/rc0.d/  #运行模式0下需要启动的服务/etc/rc1.d/  #运行模式1下需要启动的服务/etc/rc2.d/  # Services to be started in run mode 2/etc/rc3.d/  #运行模式3下需要启动的服务/etc/rc4.d/  #运行模式4下需要启动的服务/etc/rc5.d/  #运行模式5下需要启动的服务/etc /rc6.d/  #运行模式6下需要启动的服务

  

These directories have soft links (symbolic links) In addition to the readme documentation, which point to the process management scripts for each service, and these process management scripts are placed in the/ETC/INIT.D directory . 
Debian Linux default runlevel is 2, look at the contents of/etc/rc2.d/ 

[email protected]:~# ls-l/etc/rc2.d/total 4-rw-r--r--1 root root 677 Nov readmelrwxrwxrwx 1 root Root: S01ipvsadm. /INIT.D/IPVSADMLRWXRWXRWX 1 root root 6 13:45 s16apache2. /init.d/apache2lrwxrwxrwx 1 root root: 16:31 s17keepalived. /INIT.D/KEEPALIVEDLRWXRWXRWX 1 root root June 13:54 s17memcached. /INIT.D/MEMCACHEDLRWXRWXRWX 1 root root 6 13:45 s17mysql. /init.d/mysqllrwxrwxrwx 1 root root 13:14 s17nagios-nrpe-server. /INIT.D/NAGIOS-NRPE-SERVERLRWXRWXRWX 1 root root 6 13:45 S17nginx. /INIT.D/NGINXLRWXRWXRWX 1 root root 6 13:45 s17rsync. /INIT.D/RSYNCLRWXRWXRWX 1 root root 6 13:45 s17vsftpd. /init.d/vsftpdlrwxrwxrwx 1 root root 3 14:06 S18avahi-daemon. /INIT.D/AVAHI-DAEMONLRWXRWXRWX 1 root root 6 13:45S18exim4. /INIT.D/EXIM4LRWXRWXRWX 1 root root 6 13:45 s20rc.local. /init.d/rc.local

  

Adding or removing the start-up service with the Chkconfig command is actually creating or deleting soft links in these directories.
Each soft link is named "Uppercase S+ Run order + script name", which has a link "s20rc.local." /init.d/rc.local ". Open the script that it points to/etc/init.d/rc.local look at it and find that it calls the/etc/rc.local script. The original command in/etc/rc.local was executed here.
If you put the startup command of the MySQL service into/etc/rc.local, the operating system will execute it when the list is executed, but it is executed in the order of 20, which is a bit later than the s17mysql in front of the queue.

Administering the service process with the Services command and the Chkconfig command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.