New features of systemd and common types of unit analysis and awk usage list

[TOC]

Briefly describe the new features of systemd and the analysis of common types of unit, which can be compiled and installed such as nginx\apache to be managed by systemd
  • What's New in System Boot
    1. Realize service startup when the system boots;
    2. Activate processes on demand;
    3. system state snapshot;
    4. Define service control logic based on dependencies;
  • Core concept: unit (unit)
    1. The unit is identified, identified and configured by its related configuration files; the files mainly contain system services, monitoring sockets, saved snapshots, and other init-related information; these configuration files are mainly stored in:
      /usr/lib/systemd/system
      / run/systemd/system
  • Common types of units:
    Service unit: The file extension is .service, which is used to define system services;
    Target unit: The file extension is .target, which is used to simulate the implementation of "run level";
    //centos7 does not have a startup level, only when it is accessed Start
    Device unit: the file extension is .device, which is used to define the device recognized by the kernel;
    mount unit: the file extension is .mount, which defines the mount point of the file system;
    Socket unit: the file extension is .socket, which is used to represent the process The socket file used for inter-communication;
    Snapshot unit: The file extension is .snapshot, which manages the snapshot of the system;
    Swap unit: The file extension is .swap, which is used to identify the swap device;
    Automount unit: The file extension is .automount, and the file system Automatic mount point device;
    Path unit: The file extension is .path, which is used to define the file or directory of the file system;
    /etc/systemd/system
  • Key Features
    Socket-based activation mechanism: socket and program separation;
    bus-based activation mechanism;
    device-based activation mechanism;
    Path-based activation mechanism;
    system snapshot: save the current state information of each unit in a persistent storage device;
    Backward compatibility with sysv init scripts;
  • Incompatible;
    systemctl commands are fixed;
    systemctl cannot communicate with services not started by systemd;
    /etc/init.d/Manage
    system services:
    Centos7: SErvice type unit file;
  • Compile and install Nginx
  1. install software

    [root@localhost ~]yum install -y pcre
    [root@localhost ~]yum install -y pcre-devel
    [root@localhost ~]yum install -y openssl-devel
    [root@localhost ~]useradd nginx
    [root@localhost ~]passwd nginx
    [root@localhost ~]tar -vzxf nginx-1.11.3.tar.gz -C /usr/local
    [root@localhost ~]cd nginx-1.11.3/
    [root@localhost nginx-1.11.3]# ./configure \
    > --group=nginx \
    > --user=nginx \
    > --prefix=/usr/local/nginx \
    > --sbin-path=/usr/sbin/nginx \
    > --conf-path=/etc/nginx/nginx.conf \
    > --error-log-path=/var/log/nginx/error.log \
    > --http-log-path=/var/log/nginx/access.log \
    > --http-client-body-temp-path=/tmp/nginx/client_body \
    > --http-proxy-temp-path=/tmp/nginx/proxy \
    > --http-fastcgi-temp-path=/tmp/nginx/fastcgi \
    > --pid-path=/var/run/nginx.pid \
    > --lock-path=/var/lock/nginx \
    > --with-http_stub_status_module \
    > --with-http_ssl_module \
    > --with-http_gzip_static_module \
    > --with-pcre
    [root@localhost nginx-1.11.3]# make &&make install

    2. Start, restart, shut down

    [root@localhost ~]/usr/local/nginx/sbin/nginx  //启动
    [root@localhost ~]/usr/local/nginx/sbin -s reload //重启
    [root@localhost ~]/usr/local/nginx/sbin -s stop //关闭

    3. Implement systemd to manage nginx

    [root@localhost ~]vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=Nginx Service
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    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
    Describe awk command usage and examples (at least 3 examples)
  • Introduction
    Awk is a powerful text analysis tool, equivalent to grep search, sed editing, awk is particularly powerful when it analyzes data and generates reports. Awk reads the file line by line, slices each line with a space as the default delimiter, and then performs various analysis and processing on the cut part.
  • Use the method
    awk [options] 'scripts' file1,file2, ...
    to specify options, specify the script after the report is formatted, and finally specify the source of the file content; this means that it can read text information from multiple files, and then according to The specified script is formatted into a specific format, and finally displayed; the format of the script is generally divided into two parts, as follows:
    awk [options] 'PATTERN {action}' file1,file2,...
    The script is mainly composed of PATTERN Formed with action, PATTERN is called a pattern, which means that it does not process every line in the file, but only those lines that can be matched by the pattern. After the line is matched, it will take action, that is, make corresponding processing. Generally, the most common processing mechanism is to print it out. Commonly used printing commands are print and printf, which can customize the display format, such as the width of the display, whether it is displayed as a string or a numerical value, etc.
    -Basic processing mechanism
    of awk Read a line of text from the file at a time, awk will automatically slice it, and cut each line according to the delimiter of the string. If this line is this is test, it will use blank characters as separators by default, no matter how many spaces there are, so this line will be divided into four pieces, one word is saved in one piece, and these four pieces can use a variable in awk to refer to, this variable is relative to the positional parameter in the script;
    $1,$2...$0
    - test

    [root@qingcheng-app3 lgp]# vim awk.txt 
    this is a test
    this[root@qingcheng-app3 lgp]# awk '{print $1,$2}' awk.txt
    this is
    [root@qingcheng-app3 lgp]# awk '{print $1}' awk.txt
    this
    [root@qingcheng-app3 lgp]# awk '{print $2}' awk.txt
    is
    The option to specify the delimiter is -F, such as -F:, which means that the colon is the delimiter. In addition, you can also specify the output separator, as follows:
    [ root@qingcheng-app3 lgp]# awk '{OFS=":"}{print $1,$2}' awk.txt
    this:is
    the print format
    print item1,item2,...
    if you want to insert it in the middle The text can be inserted by using double quotation marks to enclose the content to be displayed as follows:
    [ root@qingcheng-app3 lgp]# awk 'BEGIN {print "line one\nline two\nline three"}'
    line one
    line two
    line three
    //Print three lines of text, \n means newline
    [ root@qingcheng-app3 lgp]# awk -F: '{print $1,$2}' /etc/passwd
    //Use colon as separator, print passwd the first and second paragraphs of
  • awk variable
    Common built-in variable record variable
    FS:field separator, when reading text, the field separator used
    RS:Record separator, the newline used for input text information
    OFS:Output Filed Separator, the output field separator
    ORS:Output Row Separator, output row separator
  • The data variable of awk built-in variable
    NR: The number of input records, the number of records recorded by the awk command. Equivalent to the current file, how many lines he has processed. If there are multiple files, this number will be counted uniformly among the multiple files processed.
    NF: Number of field, the number of fields currently recorded, count how many fields are in the current row being processed
    FNR: Unlike NR, RNR is used to record the row being processed is the total number of rows processed in the current file
    ARGV: Array
    ARGC: Number of awk command parameters
    FILENAME: The name of the file processed by the awk command
    ENVIRON: An associative array of current shell environment variables and their values
  • Display
    [ root@qingcheng-app3 lgp]# awk '{print NF}' awk.txt
    4
    //Display a total of several fields
    [ root@qingcheng-app3 lgp]# awk '{print NR}' awk.txt
    1
    //NR is the absolute count u, showing the line number

  • User-defined variables
    gawk allows users to customize their own variables for use in program code. The naming rules for variable names are the same as most programming languages. Only letters, numbers and underscores can be used, and they cannot start with numbers. gawk variable names are case-sensitive; to define a variable, just add the variable name equal to the variable value in the BEGIN mode, or use the -v option to define the variable on the command line
    [ root@qingcheng-app3 ~]# awk -v test= "hello awk" 'BEGIN{print test}'
    hello awk
    [ root@qingcheng-app3 ~]# awk 'BEGIN{var="variable testing";print var}'
    variable testing
    //Printing variable values ​​in awk does not require adding $$, plus the $ character is the print field. There is no file behind, but BEGIN is often used when doing a simple demonstration. In addition, when defining a variable in {}, the system will recognize it as two statements, which should be separated by a semicolon.

  • Use format of the printf command
    printf format, item1,item2,...
    Key points:
    a. The biggest difference from the print command is that print needs to specify format
    b, and the format user specifies the output format of each item after that.
    c. The printf statement will not automatically print line characters. If a line break is required, add \n
    The indicators in the format format all start with %, followed by a character, as follows:
    %c: ASCII code of the display character
    %d, %i: decimal integer
    %e, %E: display value in scientific notation
    %f: display Floating point
    %g, %G: Display numerical value in scientific notation or floating point format
    %s: Display string
    %u: Unsigned integer
    %%: Display % itself
    Modifier
    N: Display width-
    : left-aligned, It is not necessary to align right
    +: display the numerical symbol
    Display column:
    awk -F: '{printf "%-15s, %-10s\n",$1 $3}' /etc/passwd
    //When $1 and $2 are displayed, $1 is left-aligned to display 15 character strings, if there is no content, it will be blank. A comma will also be displayed here, here is the comma after -15s
    awk -F:'{printf "%-5d\n",$3}' /etc/passwd
    //Display the ID number of each user in decimal for alignment

Describe awk function examples (at least 3 examples)
  • The available function rand()
    is listed:
    awk 'BEGIN{print rand()}
    //Using the rand() function can only generate a number randomly, this number will not change
    awk 'BEGIN{strand();print rand()}' //配合使用strand()函数,才生成一个大于0小于1的随机数awk 'BEGIN{strand();print int(100*rand())}'
    // Generate a random integer less than 100. Use strand() and rand() to multiply by 100 to get a random integer random number. The int is used to truncate the integer part. If there is no int, the generated number will have a fractional part.
  • String functions
    Replace some text with gsub or sub functions
    [ root@qingcheng-db ~]# vim test.txt
    Allen phillips
    Green Lee
    William Lee
    william ken Allen
    [ root@qingcheng-db ~]# awk '{gsub("l ","L",$1);print $0}' test.txt
    ALLen phillips
    Green Lee
    WiLLiam Lee
    wiLLiam ken Allen
    //Use gsub function to replace lowercase l with uppercase L, but the replacement range is limited to the first column
    [ root@qingcheng -db ~]# awk '{gsub("[az]","6",$1);print $0}' test.txt
    A6666 phillips
    G6666 Lee
    W666666 Lee
    6666666 ken Allen
    //Replace lowercase letters in the first paragraph is 6, and displays all
    [ root@qingcheng-db ~]# awk '{sub("l","L",$1);print $0}' test.txt
    ALlen phillips
    Green Lee
    WiLliam Lee
    wiLliam ken Allen
    //sub command replaces the character of the first matching symbol condition within the specified range, that is, the first lowercase l of the first paragraph

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324830644&siteId=291194637