linux task scheduler cron

10.23 linux task scheduling cron

configuration file, cat /etc/crontab

The key point is the content of the second red box in the above picture, the five "  " in front represent minutes, hours, dates, months, and weeks. user-name indicates the user name, if not, the default is root, and command to be executed indicates the command to be executed.

Enter the command crontab -e, press Enter, you will enter the configuration file

The usage is the same as vim, press the letter "i" to enter the editing mode, "Esc" to quit editing, ":wq" to save and exit, ":q" to quit, ":q!" to force quit.
Now to explain the mission plan:


"0 3": means 3 am every day. Indicates all ranges. The minute is 0-59, the hour is 0-23, the date is 1-31, the month is 1-12, and the week is 1-7.
"0 3 1-10 /2 2,5": It means 3:00 am on Tuesday and Friday on the 1st and 10th of the bimonthly period. Among them, "/2" on the side of the month means that it can be divided by 2, which means double months, and /2 on the side of the hour means every two hours.
There is no year here, because the week can be unique, and the month and week are different every year.
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log In this task plan, /bin/bash Indicates the user, /usr/local/sbin/123.sh indicates that there is a shell script in the file to execute the commands in it. >>/tmp/123.log 2>>/tmp/123.log means that all the results of the task plan are appended to the file /tmp/123.log.
After the task plan is written, save and exit, you need to enable the service again, enter the command systemctl start crond, and press Enter. Then check if the service is started.

Enter the command ps aux |grep cron to view the process

 systemctl status crond View status

If it is green, it means it is activated, if it is deactivated, there is no color display.

Note:
The commands in the shell script must use absolute paths, otherwise they will not be executed.
Commands in PTAH that are not in the configuration file need to use an absolute path. Another way is to add the command to the PTAH of the configuration file.
The safest way is to write the absolute path directly, and do not move the content in PTAH.
When writing the task plan, it is best to append all of them to a log file, and write the correct and incorrect output, so that it can be documented.
How to backup? Use the command crontab -l to see the task schedule. The crontab file is in the /var/spool/cron/ directory, and is divided into multiple files according to the user name. For example, the root user is in /var/spool/cron/root. The content in this file is the content listed in the execution result of the command crontab -l. So for backup, just copy this file directly. The option -r means delete.
Summary:
crontab -e edit
crontab -l view
crontab -r delete

10.24 The chkconfig tool

The application of this tool chkconfig is in CentOS6 and previous versions, it is not used in CentOS7 version, but in order to be compatible with previous versions, chkconfig can still be used.

Command chkconfig --list to view the chkconfig service used by the current system

You can see that there are only two services netconsole and network, so where are the other services? Pay attention to the contents of the above figure, which probably means that the services used in CentOS6 and previous versions are based on SysV services, while the services used in CentOS7 versions are based on systemd services.
The only 4 services left in the chkconfig tool exist in the file /etc/init.d/

Both netconsole and network have 7 levels, among which level 2, 3, 4, and 5 of networkd are all open. After using the command chkconfig network off, they are all closed, and after using the command chkconfig network on, they can be opened again. The 7 levels represent: 0 for shutdown, 1 for single-user mode, 2 for multi-user mode (without graphics, less nfs services), 3 for multi-user mode (without graphics), and 4 for reserved level (no graphics for the time being). with ), 5 for multi-user mode (with graphics), and 6 for reboot. There are only these levels in CentOS6 and previous versions. Which level is located at boot time, which level of mode will be started. The current CentOS7 version does not have these levels. In CentOS6 and previous versions, changing the file /etc/inittab can define the run level of the boot

chkconfig --level3 network off level 3 off

chkconfig --level45 network off 45 level off

chkconfig --level345 etwork on 345 level open

You can add scripts to the service list to customize a service. The added service must be in the /etc/init.d/ directory before it can be added successfully.

Enter the command chkconfig --del 123 to delete the custom service

Summary:
chkconfig --list List all services
chkconfig network off Turn off services
chkconfig network on Turn on services
chkconfig --level 3 network off Turn off level 3 services
chkconfig --level 45 network off Turn off both level 4 and level 5 services
chkconfig --add 123 Add the 123 service to the chkconfig tool

 10.24 systemd management services

systemctl list-unit-files //List all startup files

systemctl list-units --all --type=service //Only view service files

 systemctl list-units --type=service // List the started service files

systemctl enable crond.service //Let the service start up, the .service here can be omitted.
systemctl disable crond //Disable service startup
systemctl status crond //View service status

systemctl stop crond //Stop the service
systemctl start crond //Start the service
systemctl restart crond //Restart the service
systemctl is-enabled crond //Check whether the service is started

10.25 Unit introduction

The original file pointed to by the soft link file is /usr/lib/systemd/system/crond.service,

Use the command ls /usr/lib/systemd/system/ 

There are many types of files and directories under this path, and these files are called unit.
There are mainly the following types:
service system service
target group composed of multiple units
device hardware device
mount file system mount point
automount automatic mount point
path file or path scope external process slice
not started by systemd process group snapshot systemd snapshot socket Interprocess communication socket swap swap file timer timer Then enter the /usr/lib/systemd/system/ directory to view the runlevel, see the figure below,





There are also 7 run levels on the entOS7 side, which are the target soft link files of 0-6, which point to the following paths. These 7 run levels can basically correspond to CenOS6 and previous versions.
Next, let's take a look at several related commands of unit,

systemctl list-units //View running units

systemctl list-unit --all //View all units

systemctl list-units --all --state=inactive //Specify the state to view the unit

systemctl list-units --type=service //List all running Units of type service 

 systemctl list-units --all --type=service //List all units of type service

 systemctl is-active crond.service //Check whether a service is active

10.27 Introduction to target

 systemctl list-unit-files --type=target //List all targets in the system

.

systemctl list-dependencies multi-user.target //View which units are in the specified target

systemctl get-default // View the default target

Change the target type, you can change the startup level of the boot, similar to the configuration file of CentOS6 and previous versions to change the run level. 

systemctl set-default multi-user.target //Set the default target

In the above figure, the file /etc/systemd/system/default.target is a soft link file
. The relationship between unit and service is as follows:
A service belongs to one type of unit, and
multiple units form a target
. A target contains multiple services.

Summarize:

cat /etc/crontab View the configuration file of the task plan
user-name command to be executed " Five "  " represent minutes, hours, days, months, weeks, user-name represents the user name, if not, the default is root, command to be executed indicates the command to be executed.
crontab -e Enter the configuration file, enter the command line to save and exit
0 3 1-10 stars/2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp /123.log In this task plan, /bin/bash indicates the user, and /usr/local/sbin/123.sh indicates that there is a shell script in the file to execute the commands in it. >>/tmp/123.log 2>>/tmp/123.log means that all the results of the task plan are appended to the file /tmp/123.log.
systemctl start crond restart service
ps aux |grep cron check process
systemctl status crond check status
crontab -e edit
crontab -l check
crontab -r delete

chkconfig --list View the services of the current system using the chkconfig tool
chkconfig network off Turn off the service
chkconfig network on Turn on the service
chkconfig --level 3 (or 345) network off Turn off one or more specified level services
chkconfig --level 3 (or 345 ) network on Start the specified one or more level services
cd /etc/init.d/
cp network 123 These two steps prepare for the following adding or deleting custom services
chkconfig --add 123 Add custom services
chkconfig -- del 123 delete custom service

systemctl list-unit-files View all unit services
systemctl list-units --all --type=servic View only services of type service
systemctl list-units --type=service List only active files
systemctl enable crond.service Let the service start up, the .service here can be omitted.
systemctl disable crond Disable service startup
systemctl status crond View service status
systemctl stop crond Stop service
systemctl start crond Start service
systemctl restart crond Restart service
systemctl is-enabled crond Check whether service is started

systemctl list-units Lists running units
systemctl list-units --all Lists all, including failed or inactive
systemctl list-units --all --state=inactive Lists units in the specified state inactive
systemctl list-units --type=service lists the units of the service in the specified state, and the failed state will also appear
systemctl list-units --type=service --state=active only lists the units of the service in the specified state
systemctl list-units --all --type =service lists all services in status
systemctl list-units --type=service lists services whose status is active
systemctl is-active crond.service to see if a service is active

systemctl list-unit-files --type=target List all targets in the system
systemctl list-dependencies multi-user.target View which units are under the specified target
systemctl get-default View the system default target 
systemctl set-default multi-user .target sets the default target 
cat /usr/lib/systemd/system/sshd.service to see which target
unit ssh.service belongs to. The relationship between service and service is as follows: a service belongs to a
type of unit and
multiple units form a target
target contains multiple services

Guess you like

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