centos7 service starts automatically at boot – systemctl

All kinds of middleware installed on the server generally need to be configured to start automatically at boot. However, the installation process of some middleware does not provide related documentation for configuring boot-up self-starting. This article summarizes the method of Centos7 configuring service self-starting through systemctl enble.

For more methods, refer to the link: Centos boot self-start configuration method summary

1. Centos7 configures service self-starting through systemctl enable

After Centos7, it is recommended to control the service through systemctl.

The Centos system service script directory /usr/lib/systemd/ is divided into system (system) and user (user). If you need to start a program that can be run without logging in, it exists in the system service (system), namely: /usr/lib/systemd/system/. Conversely, programs that can only be run after the user logs in are stored in the user (user), and the service ends with .service, namely: /usr/lib/systemd/user/
Service management is through systemd, and most configuration files of systemd are placed in the /usr/lib/systemd/ directory. However, the official Red Hat document points out (see: https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/150.html for details), the files in this directory are mainly the settings provided by the original software, and it is recommended not to modify them! The location to be modified should be placed in the /etc/systemd/system/ directory.

1.1 For the installed service, directly set boot self-start

  1. View service status: systemctl status service name
#example
systemctl status taosd
systemctl status taosadapter

As shown below:

  • disabled: Indicates that the service has not been enabled to automatically start at boot.
  • enabled: Indicates that the service is automatically started at boot.
    1
  1. If the service has not been enabled to automatically start at boot, set it to enable auto-start at boot, and execute the command:
systemctl enable service name
#example
systemctl enable taosadapter

After the setting is complete, check the status: systemctl status taosadapter
1

Service status description: systemctl status service name

  • loaded The system service has been initialized and the configuration has been loaded
  • active (running) One or more programs are being executed in the system, vsftpd is this mode
  • atcive (exited) is a service that is executed only once and ends normally, and currently there is no program executing in the system
  • atcive (waiting) is being executed, but it is still waiting for other events to continue processing
  • inactive service shutdown
  • The enbaled service starts at boot
  • disabled The service does not start automatically when booting
  • Static service startup items cannot be managed
  • failed System configuration error

1.2 For non-installed services, create a script to set boot self-start

Take the establishment of kibana service boot self-starting as an example:

  1. Create kibana service file:
#Enter the directory
cd /etc/systemd/system/
# create script file
touch kibana.service
# edit script file
vi kibana.service

Enter script content:

[Unit]
Description=kibana
After=network.target
[Service]
Type=forking
User=root
Group=root
ExecStart=/etc/init.d/kibana start
ExecReload=/etc/init.d/kibana restart
ExecStop=/etc/init.d/kibana stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target

Note:
Here the ExecStart, ExecReload, and ExecStop commands are implemented by configuring the kibana script in the /etc/init.d directory above.
Parameter Description:

  • [Unit]
    (1) Description: Describe the service
    (2) After: Describe the service category
  • [Service] Setting of service operation parameters All commands for starting, restarting and stopping services require the use of absolute paths
    (1) Type=forking is the form of running in the background
    (2) User service starts the user
    (3) Group service starts user group
    (4) ExecStart is the specific running command of the service
    (5) ExecReload is a restart command
    (6) ExecStop is a stop command
    (7) PrivateTmp=True means to allocate independent temporary space to the service
  • [Install] Related settings for service installation
    (1) WantedBy=multi-user.target is set to multi-user
  • Parameter Description:
  1. Grant execution permission and execute the command: chmod 754 kibana.service

The permission combination is the sum of the corresponding permission values, as follows:
7 = 4 + 2 + 1 read, write and run permissions
5 = 4 + 1 read and run permissions
4 = 4 read only permissions.
This command means to grant the read, write and run permissions of the filename file to the file owner, to give the group users the read and run permissions, and to give other users the read permissions.

  1. View service status
    (1) Reload the configuration file of a service. If a new service is installed, it belongs to systemctl management. If the service program configuration file of the new service takes effect, it needs to be reloaded. Execute the command: systemctl daemon-reload
    (2) To view the service status, execute the command: systemctl status kibana.service
    1
    Among them, disabled means that the service has not been enabled to start automatically.

(3) Turn on the service and start it up, execute the command: systemctl enable kibana.service
1

1.3 Common service files

  1. nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
  1. mysql.service
[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
#ExecReload=/usr/local/mysql/support-files/mysql.server restart
#ExecStop=/usr/local/mysql/support-files/mysql.server stop
#PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. redis.service
[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www

[Install]
WantedBy=multi-user.target
  1. supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99

[Install]
WantedBy=multi-user.target