Detailed systemd service

Brief introduction

Centos5 generated in the user space and process management as well as the completion of initialization using the system are init, and is in turn starts. In centos6 the upstart is used to achieve a certain degree of parallelism start, but there are still dependencies to centos7 start using systemd, real implementation started in parallel, the delay started on demand.

systemd features

1 system start process is implemented in parallel to start

2 systemd managed by the service can be activated on demand

3 Support System Snapshot Service Status

4 may define dependencies between program

unit Introduction

The configuration file is the core unit uit, mainly contained in the configuration file

1. The need to manage the service or program

2. Service or program needs to listen sockets

3. Save a snapshot of the system (that is, when the snapshot of the state of the system in each service)

4. Other related configuration and init

Location unit profile

/ Lib / systemd / system configuration of the local system unit

The runtime configuration / run / systemd / system system unit

/ Usr / lib / systemd / system of the third-party software system unit (sshd, nginx ..)

Note: unit (unit) and the init service script is similar, but not identical

unit in multiple categories

Type 1: service unit, which is the type of service unit, the class unit suffix is .service, such as nginx.service, nuit can be achieved by this startup, shutdown, view the status 
of such unit is located in / usr / lib / systemd / System 
Note: the unit is essentially not a script, but only systemd a configuration file, so this unit file does not have execute permissions, but can not execute 
the way to start a service: to read the contents of the configuration file by making systemd, complete nginx start the process of 
type 2: device unit, which is the device unit, the class unit suffix is .device, the role of this type of unit is used to identify the device file 
type 3: Mount unit, suffix. Mount , mount the file used to implement system to the specified mount point 
type 4: socket unit, suffix .socket, used to represent inter-process communication socket file 
type 5: snapshot nuit: snapshots unit, the suffix .snapshot, used to manage snapshots 
type 6: target unit: goal unit, the suffix is .target, run level on the analog centos5,6 (mainly for backward compatibility) 
type 7: swap unit 
type 8: path unit

systemd features

Features 1. socket-based activation mechanism of 
    the system boot time, systemd will all have to start service mark has been launched, marking the way to the service of all sockets are listening to start up, but by their own systemd to listen, rather than specific services to monitor themselves, this would resolve the dependencies between processes. 
    The first time a user is accessed socket, systemd will immediately start the process corresponding to the socket, then the socket and the start of the service to do the binding 
properties of 2: activation mechanisms based bus 
characteristics 3: based device activation mechanism 
characteristics 4: based on path activation mechanism 
    that allows a systemd monitoring a directory, when the directory files are changed, and immediately activate a service 
characteristic 5: support snapshot mechanism 
6 features: support for compatible domain sysV style service management script

Application Example systemctl

1      view all current system has been activated unit: List systemctl-Units - type-Service
 2      All of the current system unit (including the inactive): systemctl list-units --type service --all

 2. boot from the start to close

Set a service boot from the start: systemctl enable nginx.service 
    disable a service boot from the start: systemctl disable nginx.service 
    [Setup boot from the start, in fact, in /etc/systemd/system/multi-user.target.wants/ created under the implementation of / usr / lib / systemd / system / soft connection profile] under the 

    View automatically starts the current system of all the unit: systemctl List -unit- files 
    State 
        Disabled: prohibits boot automatically 
        enabled: at startup 
        static: the unit can not operate independently, rely on other unit
[root@localhost ~]# cd /etc/systemd/system/multi-user.target.wants/
[root@localhost multi-user.target.wants]# ls -l nginx.service 
lrwxrwxrwx. 1 root root 37 3月  25 15:22 nginx.service -> /usr/lib/systemd/system/nginx.service

 3. Check whether the self-starting

1      to see whether the specified service at startup
 2          systemctl List-unit-Files | grep nginx.service
 3          systemctl-unit- List Files nginx.service
 4          systemctl IS- Enabled nginx.service
 5      to view the dependencies of a unit: systemctl list- nginx.service the Dependencies
 6      priority with reload, if you can not make changes to take effect before execution restart: systemctl reload-or-restart nginx.service

4. Prohibition of the Use enable

1      prohibited enable to make a service boot from the start: systemctl mask nginx.service
 2      unban mask function: systemctl unmask nginx.service
 3  
4      Note: All .service can be omitted

5. Switch Level

Application Example 1 systemctl -2 
second switching level: systemctl isolate name.target 
. 3 
. 4 is switched to rescue mode (Level): systemctl rescure.tartet <<< do not need to switch to isolate rescue mode

 6. Check the default level

1 Check the system default run level: systemctl default GET- 
2 target system can be used to view the current: systemctl List --type target-Units 
3 modify the default operating system level: systemctl set-default name.target

The basic structure of the file unit

First part: unit, used to record some generic information of the file unit

The second part: service, service record specific information (how to start, profile location ...)

Part III: install, install recording information (run level)

First part: Unit portion
     . 1 ) the Description: a brief description of the service
     2 ) the Before: define a startup sequence, examples: the Before = nginx.service, indicates that the service needs to be started before starting nginx
     . 3 ) the After: definition of start-up sequence, supra
     4 ) documentation: documentation help
     5 ) Wants: also start a service, such as: Wants = nginx.service representation system is best to start nginx service, but even if nginx does not start, the service also lacks the impact of the current
     6 ) the Require: current services which need a specific service, such as: the require = nginx.service, which indicates that the current service is needed nginx service, then when starting the current service, nginx will also automatically start up; and if it is found nginx service stops , then the current will automatically stop the service also 
    [require not specify the start order, you need to combine Before After use, otherwise frequent problems] 
    7 ) Conflicis: when a service starts, it will immediately stop the conflict with it service 

second part: service part 
    1 ) Type: Specifies the service Type 
        Type 1: oneshot: the end of the program execution is completed automatically, no background processes, such as the implementation of a shell 
        Type 2: forking: need a background daemon has been in operation, such as running nginx, apache, sshd
    2 ) ExecStart: Specifies how to start the service (Command designated to start the service to be performed) 
        If the service type is a oneshot type, you can write directly on his command to be executed, usually can not add parameters and options 
        if the service type is not a oneshot type, in the write command to be executed, you can add some parameters, such as specifying options, configuration files, users 
        [If you want to execute multiple commands, then use between multiple commands; split, if required wrap can be used \ to continue the line] 
    3 ) ExecStartPre, ExecStartPost: specified before launching the service, after what command to execute
     4 ) ExecStop: stop the service
     5 ) the Restart:
     6 ) the PidFile: Specifies the pid file path 
part III: install part 
    WantedBy: Specifies the run level, WantedBy = multi-user.target

Case: file write unit, and registered to systemd services

Step one: Prepare a shell script 
[root @ localhost ~] # CAT name. SH  
# / bin /! Bash
 echo ` hostname `> / tmp / name.log 
Step 2: Create unit files 
[root @ localhost ~] # CAT my.service 
[Unit] 
the Description = the this IS My First Unit File 
 
[Service] 
Type = OneShot 
ExecStart = / bin / bash / root / name. SH 
 
[the Install] 
WantedBy = Multi- user.target 

[root @ localhost ~] # mv my.service / usr / lib / systemd / System
The third step: to register my systemd unit files to the 
[root @ localhost ~ ] # systemctl enable my.service 
the Created symlink from /etc/systemd/system/multi-user.target.wants/my.service to / usr / lib / systemd / System / my.service. 
step 4: Check the status of the service 
# systemctl status my.service

Spread

1.grub2 configuration file - / etc / default / GRUB 

Copy the code
# 1 specifies the user select menu timeout 
 2 GRUB_TIMEOUT =. 5       
 . 3 
 described in Title # 4 specified menu     
 . 5 GRUB_DISTRIBUTOR = "$ (Sed 'S, Release. * $ ,, G' / etc /-Release System)" 
 . 6 
 . 7 # GRUB specify which is the default title 
 . 8 # save indicates saves the current configuration, requires a combination of an internal variable is used 
 . 9 GRUB_DEFAULT = saved 
10 
. 11 # disable a submenu 
12 is GRUB_DISABLE_SUBMENU = to true 
13 is 
14 # specify the output information during the boot terminal 
15 GRUB_TERMINAL_OUTPUT = "Console" 
16 
. 17 # start the system, as to the command line parameters passed to the kernel 
18 is GRUB_CMDLINE_LINUX = "Auto rhgb crashkernel =" 
. 19 
20 is # Specifies if the startup procedure information 
21 GRUB_DISABLE_RECOVERY = "true"
Copy the code

2. Customize grub2 profile

The first step: the original backup the grub.cfg 
        # CP / Boot / grub2 / the grub.cfg {, BAK.} 

    Step: Modify / etc / default / GRUB 

    third step: to regenerate the grub.cfg 
        # grub2 -mkconfig > /boot/grub2/grub.cfg

Enter rescue mode method

1 Overview

To make linux into rescue mode, in principle, in fact, one CD-ROM into the startup mode, so finding ways to let the machine boot from the CD.

In this paper, the simulation operation in Vmware. If it is true the machine, insert the disc into operation before the machine can be

2 CentOS6

First press esc connected when the following screen appears on reboot into Boot Menu. (Ps: if a virtual machine is to press ahead click of the mouse, press the mouse mode is operating within a virtual machine, the local machine is not operating, then, to be fast hand speed, before the progress bar is full press, only click, press more have no access)

Note: Here If you press F2 to enter the BIOS and then set the compact disc as the first entry is also possible, but not recommended, because the need to change back, or else every time CD first start, a lot of trouble

 

1240

The third key, select CD-ROM Drive, CD-ROM boot

1240

And then select the third rescue mode

1240

Select the language, the default English on the line

1240

1240

No skipping detecting network port selected

1240

Click Continue

1240

Here reminds us that the root directory of our previous rescue mode is not the root directory, /mnt.sysimage this directory is the root directory before I did, if you want to change back to the previous root directory need to perform chroot / mnt / sysimage command

1240

Once again, we question the root directory

1240

This time choose to open the shell process, so that we can use the commands.

1240

1240

 

3 CentOS7

FIG centOS7 enter a troubleshooting method has two

method one

First press esc connected when the following screen appears on reboot into Boot Menu. (Ps: if a virtual machine is to press ahead click of the mouse, press the mouse mode is operating within a virtual machine, the local machine is not operating, then, to be fast hand speed, before the progress bar is full press, only click, press more have no access)

1240

The third key, select CD-ROM Drive, CD-ROM boot

1240

Method Two

Virtual machine boot by F12 - "c -> Enter exit -"

The above two methods can enter the troubleshooting page, the same as the next step

 Figure I choose troubleshooting -> Figure II select Rescue, waiting to load - "Select Option 1, wait for a while, press enter, you can enter Rescue Mode

 

1240

Select a troubleshooting chart

 

1240

Figure II Select Rescue

 

1240

 

Guess you like

Origin www.cnblogs.com/liujunjun/p/12566666.html