Chapter VIII Linux Linux System Management Service Management

Chapter VIII Linux system administration

Linux Service Management

Outline

Linux system services, also known as daemon, refer to resident continued running in memory, processes that are required to provide the service (system or network service). Simply put, that is, after the program starts, it will continue in the background, waiting for the call to the user or other software to use.

First, the service is also a program, but it is a special program: the service is really the system running in the background, and wait for a special program called user or other software.

In the red hat7 all services must ultimately d, is the guardian daemon meaning. Such as vsftpd and so on.

8.1 system initialization process system

The initialization process is the first program to be executed when a Linux system starts, it needs to D'Souza is up and manage a variety of other services. PID Systwm process is always 1.

Pstree process tree

 

Systemd core concepts

Unit
represent different types of sytemd object identification and configuration through a configuration file, the file contains the main system services, monitor socket, the system saves snapshots and other related information init

Configuration file:
/ usr / lib / systemd / System: The main startup script for each service settings, /etc/initd.d Similar to the previous
/ run / system / system: service script execution system generated than the above directories first run
/ etc / system / system: execute the script created by administrators, similar to the function /etc/rc.d/rcN.d/Sxx class, run priority than the above directory, in the three, this directory highest priority


    init-time version of the initialization process before the red hat7, starting with red hat, officially adopted a new systemd init process. Than the previous init, there are obvious advantages of the following aspects.

When start using concurrent start mechanism advantage of a system. Init is before the order to start each of the services, but no dependencies between some services. Now the CPU is multi-core operating system can parallel processing tasks, systemd to achieve the ability to make those services do not exist dependencies in parallel start, greatly accelerate the speed of system startup.

Two advantages, systemd provides the ability to start on-demand services.

 

Unit unit

For systemd, he has a unit (unit), systemd system management functions mainly through a variety of unit to achieve a core concept. Each unit has a profile corresponding to their identification and configuration, these configuration files are stored in / usr / lib / systemd / system and / etc / system / system: in. Such unit configuration files are usually suffixed with .service.

Mainly in the / usr / lib / systemd / system directory

For example sshd.service service profile

 

root@Redhat7 system]# ll sshd.service

-rw-r--r--. 1 root root 361 9月  25 2015 sshd.service

[root@Redhat7 system]# cat sshd.s

sshd.service  sshd.socket  

[root@Redhat7 system]# cat sshd.s

sshd.service  sshd.socket  

[root@Redhat7 system]# cat sshd.service

 

 

Unit Type

systemctl -t help: Check the type of unit

service unit: the file extension .service, used to define system services

target unit: the file extension .target, used to simulate the realization of "run level"

device unit: .device, the kernel used to define the identified device

mount unit: .mount, define the file system mount points

socket unit: .socket, used to identify the inter-process communication socket files used can also be at system start, delayed start service start-demand

snapshot unit: .snapshot, system snapshot relationship

swap unit: .swap, swap device for indicating

automount unit: .automount, automatic mount point of the file system, such as: / misc directory

path unit: .path, for a file or directory using the definition file system, the file system is commonly used when the variation, the activation delay services, such as spool directory

time: .timer systemd managed by the timer

Note: When using systemctl control unit, unit often necessary to use the full name of the file, including the extension, but some units can use a shorthand way systemctl, if no extension, the default extension systemctl as .service. Are equivalent and netcfg.service e.g. netcfg mount point is automatically converted to the corresponding .mount unit, e.g. / home home.mount equivalent to the device automatically converted to the corresponding .device unit, the / dev / sd2 equivalent to dev-sda2.device

 

8.2 systemctl Management Services

In redhat5 and redhat6 service management system primarily through service and chkconfig command to complete, mainly in redhat7 are managed by the service system in systemctl tool.

Systemctl start | stop | status | restart | reload service name

         Start - closed - state - Restart - load configuration without rebooting

 

System The system of each service as a service unit service unit, the name of the service after the service is added as a suffix. When the service is managed using the systemctl command, followed by the service name is added .service suffix can be.

 

8.2.1 manage a single unit

systemctl provides a set of subcommands single unit, the command format is:
systemctl [Command] [Unit]
Command mainly:
Start: start immediately behind the contact unit.
stop: take off immediately behind the unit.
restart: start immediately followed by the closing of the unit, i.e., execute the stop means re-start.
reload: without shutting down the unit, and reload the configuration file so that the settings take effect.
enable: When you set the next boot, the unit will be followed by the start.
disable: When you set the next boot, back then the unit will not be started.

Is-enable: Check whether the service start-up.
status: followed by the current status of this unit, there are no lists are performing, whether to start at boot time and other information.

mask: write-off unit, you can not start after the cancellation of this unit.
unmask: Cancel the write-off of the unit.
 

[root@Redhat7 system]# systemctl status sshd.service

● sshd.service - OpenSSH server daemon

   Loaded: loaded (/usr/lib/systemd/system/sshd.service (location profile); enabled (at startup); vendor preset: enabled)

   Active: active (running) since 二 2019-08-27 20:55:37 CST; 2h 31min ago

8.2.2 See all service on the system by systemctl

  See all units

systemctl list-units

View services running service

systemctl list-units --type=service

See all service service

systemctl list-units --type=service --all
 

8.4 redhat7 program startup

8.4.1 /etc/rc.d/rc.local way

 

 

/etc/rc.d/rc.local final step is the beginning of system startup

 

 

In order to see what folder above

rc.local

[root@jsmongodb ~]# which mongod

/app/mongodb3.0/bin/mongod

 

In centos7, the privileges / etc / rc.d / rc.local file is reduced, boot time to perform in your own scripts of some services can not start, execute the following command file can be marked as executable file

chmod +x /etc/rc.d/rc.local

 

[root@jsmongodb ~]# vim /etc/rc.d/rc.local   

 

touch /var/lock/subsys/local

/app/mongodb3.0/bin/mongod -f /root/mongodbconf/mongodb3.0_primary.conf

 

8.4.2   centos7中 chkconfig

First, add the boot from Kai Services

Add in centos7 boot from Kai service is very convenient, only two commands (to Jenkins for example):

systemctl enable jenkins.service # Set jenkins services since the launch of the service

sysstemctl start jenkins.service # services start jenkins

Second, add the boot from Kai Script

Added a script in centos7 There are two common ways to script autostart.sh example:

#!/bin/bash

#description: boot from Kai Script

/usr/local/tomcat/bin/startup.sh # start tomcat

A method chkconfig

1, giving the script executable permissions (/opt/script/autostart.sh your script path)

chmod +x /opt/script/autostart.sh

2, open /etc/rc.d/rc/local file, add the following at the end of

/opt/script/autostart.sh

3, in the centos7, / etc / rc.d / rc.local permissions are reduced, it is necessary to execute the following commands impart executable permissions

chmod +x /etc/rc.d/rc.local

Key Method Two

1, the script moves to the next directory /etc/rc.d/init.d

mv  /opt/script/autostart.sh /etc/rc.d/init.d

2, increase the executable permissions script

chmod +x  /etc/rc.d/init.d/autostart.sh

3, add a script to automatically boot project

cd /etc/rc.d/init.d

chkconfig --add autostart.sh

chkconfig autostart.sh on

8.5 Linux Management Scheduled Tasks

In the Linux operating system, in addition to the immediate execution of the user operation command, you can configure the specified time, the date specified to perform system administration tasks previously planned (such as regular backups, periodic inspection data collection). CentOS system default is already at, cronie package installation, to achieve a one-time, periodic scheduled tasks function by atd and crond two systems service, were set by the scheduled tasks at, crontab command. at a one-time task settings, crontab periodic task settings.

According to a preset period of time (minutes, hours, date, month, week) the user specified command repeatedly performed operation.
The main settings files:
the global configuration file, located in "/ etc / crontab";
the default setting, located in the directory "/ etc / cron. /";
User-defined settings, located in the file "/ var / spool / cron / username " .
Use crontab commands to manage scheduled tasks users commonly used options are:
-e : edit the list of scheduled tasks;
the -l : displays a list of scheduled tasks;
-r : delete the list of scheduled tasks.

8.5.1 at a one-time configuration of scheduled tasks

at (Option) (parameters)

ctrl + D at the end of the command input

at the delete key can not be used in interactive mode, hold down the tab can only add delete key can be used.

1 at a specified time method:

● it is acceptable day hh: Specifies the time of formula: mm (minutes to hours). If the time has elapsed, then on the next day execution. For example: 04: 00
    ● can use midnight (late at night), noon (noon), teatime (tea time, usually 16:00) and other more obscure words to specify the time.
    ● can be a 12 hour clock, i.e. after the time plus AM (AM) or PM (PM) AM or PM will be described. For example: 12pm
    commonly able to specify a date command, specifying the format month day (May day) or mm / dd / yy (month / day / year) or DD.MM.YY (Day Month Year.), Designated dates must follow the specified time. For example: 04: 002009-03-1
    common to use relative timing method. Specify the format: now + count time-units, now is the current time, time-units are units of time, there can be minutes (min), hours (h), days (days), weeks (weeks). count the number of times, days, hours. For example: now + 5 minutes 04pm + 3 Days
    ● can directly use today (today), tomorrow (tomorrow) to specify the complete command of time.

 

2 Related command

    1) at: perform a single task at a specific time;

    2) atq: Lists the user's scheduled tasks, if it is the superuser will list all the user's task, the result of output format is: Job number, date, hour, queues, and user name; AT the -l

    3) atrm: Number The Delete at tasks based on the Job; at -m

4) batch: perform at tasks when the system load is permitted, it is executed at the task when the system is idle.

 

at parameters

-m When the specified task is completed, the user will send a message, even if there is no standard output

-I atq alias. Atq command to view the operating system waits

-d atrm alias. atrm command to delete a job waiting in the system, followed by the job id to be deleted

Common  -v displays the time the task to be performed

Common content -c print jobs to the standard output

-V Display version information

-q <Queue> specified queue

-f <file> read tasks from the specified file instead of from standard input

-t <time parameter> submit tasks to run as a time parameter

3 Case

12019-09-01 23:11 to output time syj.txt

[root@Redhat7 ~]# at 23:11 2019-09-01

at> date>/syj222.txt

at> <EOT>

job 6 at Sun Sep  1 23:11:00 2019

2 to see if there are tasks

[Root @ Redhat7 ~] # atq

6       Sun Sep  1 23:11:00 2019 a root

3 Delete Task

[root@Redhat7 ~]# atrm 6

4 View Task content

[root@Redhat7 ~]# at -c 6

${SHELL:-/bin/sh} << 'marcinDELIMITER7f5b2a11'

date>/syj222.txt

5 successful execution

[root@Redhat7 ~]# cat /syj222.txt

September 1, 2019 Sunday 23:11:00 CST

 

8.5.2 cron periodically scheduled tasks

cron-- service name; cron - under linux to periodically perform some task or a daemon pending certain events, and scheduled tasks under windows similar; crontab-- good plan is customized task list

1 cron command parameters

crontab [-u user] file

        crontab [-u user] [ -e | -l | -r ]

                (default operation is replace, per 1003.2)

        -e      (edit user's crontab)

        -l      (list user's crontab)

        -r      (delete user's crontab)

        -i      (prompt before deleting user's crontab)

        -s      (selinux context)

   Common -e modify and create tasks

         -l View Task

         -r delete a task

2 cron format

 

 

  * * * * * Command
  minute (0-59) hour (0-23) (1-31) (1-12) week (0-6, 0 for Sunday) command
  first column represents 1 minute to 59 per min * or * / 1 indicates
  the second column shows the hours 1 to 23 (0 represents 0:00)
  the third column indicates 1 to 31
  fourth column represents the month 1 to 12
  of five identification number of weeks 0 to 7 (0 and 7 for Sunday)
  the first six command to run

An asterisk (*): On behalf of all possible values, such as the month field is an asterisk, it indicates that the command is executed operation after a month constraints to meet other fields.

Comma (,): a comma-separated values ​​can be used to specify a list of ranges, e.g., "1,2,5,7,8,9"

The bar (-): can represent an integer in the range of a bar between the integer, e.g., "2-6" indicates "2,3,4,5,6"

Forward slash (/): n frequency intervals can slash specified time, for example, "0-23 / 2" is performed once every two hours.

 

3 cron examples

1 cron and redirected

error

11 21 * * * / usr / bin / date> /syj2222.txt 21:11 every day execute the command, / usr / bin / date command must be an absolute path

[root@Redhat7 ~]# cat /syj2222.txt (空)

[Root @ Redhat7 ~] # (empty)

 

 

normal

[root@Redhat7 ~]# cat /sh/date.sh

#!/bin/bash

/usr/bin/date >/syj2222.txt

 

[root@Redhat7 ~]# chmod +x /sh/date.sh         

[root@Redhat7 ~]# ll /sh/date.sh      

-rwxr-xr-x 1 root root 40 9月   2 21:35 /sh/date.sh

 

49 21 * * * /usr/bin/bash /sh/date.sh  OK

03 22 * * * /sh/date.sh               OK

 

03 22 * ​​* 1 /sh/date.sh OK 03 copies a week, 22-point execution

10-23 30/2 * * * /sh/date.sh the OK from 10:30 beginning performed once every two hours.

10-23 30/2 * * 1-5 /sh/date.sh the OK Monday to Friday, 10:30 start to 23:00, performed once every 2 hours.

* * / 2 * * 1-5 /sh/date.sh the OK Monday to Friday, performed once every 2 hours.

 

  1. cron error-prone areas

The minimum one can not be *

* 22 * ​​* * /sh/date.sh 22 o'clock every wrong executed once per minute

0 22 * ​​* * /sh/date.sh 22 o'clock every once executed correctly

0 * / 3 * * * /sh/date.sh 0:00 start day, once every 3 hours executed correctly

 

 

View 5 cron task

[root@Redhat7 ~]# crontab -l

03 22 * * * /sh/date.sh

8.5.3 cron Case

 

Example 1 sshd service open daily 23:05, 23:07 shut down sshd service.

1 [root@Redhat7 ~]# crontab -l

05 23 * * * /usr/bin/systemctl start sshd

07 23 * * * /usr/bin/systemctl stop sshd

 

[root@Redhat7 ~]# systemctl status sshd

Active: inactive (dead)

1 success

[root@Redhat7 ~]# systemctl status sshd

  Active: active (running)

2 Success

[root@Redhat7 ~]# systemctl status sshd

Active: inactive (dead)

 

 

Example 2 23 points every 5 days a clear data var / / ftp / pub in.

14 23 */5 * * /usr/bin/rm  -rf /var/ftp/pub/*

 

Example 3 is closed every Monday at 23:20 sshd service

20 23 * * 1 /usr/bin/systemctl stop sshd

 

Example 17.30 4 weeks 1,3,5, use tar automatic backup / etc / http directory

26  23  *  *  1,3,5  /usr/bin/tar  -zcf  /http.tar.gz  /etc/http

 

8.5.4 cron +% time

 

% Had planned cron tasks for special purposes, if you want to use in the command% of scheduled tasks, you need to use \% in the form of escape, or is placed in single quotes%.

Examples 1 week 1,3,5 23:40, using the tar command automatic backup / etc / http date - date directory plus

[root@Redhat7 ~]# date +%F

2019-09-02

 

26  23  *  *  1,3,5  /usr/bin/tar  -zcf  /http-$(date +%F).tar.gz  /etc/http

Empty file

 

 

40  23  *  *  1,3,5  /usr/bin/tar  -zcf  /http-$(date +\%F).tar.gz  /etc/http

[root@Redhat7 ~]# crontab -l

40  23  *  *  1,3,5  /usr/bin/tar  -zcf  /http-$(date +\%F).tar.gz  /etc/http

 

Success OK

[root@Redhat7 ~]# ll /http-2019-09-02.tar.gz

-rw-r--r-- 1 root root 45 9月   2 23:42 /http-2019-09-02.tar.gz

 

8.5.5 system-level schedule settings

[root@Redhat7 ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

 

# For details see man 4 crontabs

 

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed

Published 37 original articles · won praise 0 · Views 2406

Guess you like

Origin blog.csdn.net/syjhct/article/details/100164559