Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4.4 Schedule task service programs


May 23, 2021 That's what Linux should learn



Experienced system operations engineers can automate operations by enabling or stopping certain services or commands automatically for a specified period of time without human intervention. A lthough we now have a powerful script to do some batch work, it's painful to still need to tap the keyboard return key at 2 a.m. every day (and, of course, train your kitten to press the return key in the middle of the night). Next, Mr. Liu will explain how to set up the server's planned task service, the periodic, regular work to the system to complete automatically.

Planned tasks are divided into one-time planning tasks and long-term planning tasks, which can be understood as follows.

One-time scheduled mission: Start the website service at 11:30 tonight.

Long-term planning tasks: Pack the home/wwwroot directory back up as a backup at 3:25 a.m. .tar.gz.

As the name implies, a one-time scheduled task is performed only once and is typically used to meet temporary job requirements. W e can do this with at commands, just in the form of "at time". I f you want to see a one-time scheduled task that is set up but not yet executed, you can use the at-l command; W hen you use the at command to set up a one-time scheduled task, the interactive method is used by default. For example, use the following command to set the system to automatically restart the website service at 23:30 tonight.

(root@linuxprobe. , at 23:30 at . Systemctl restart http://www.systemctl restarts http://www.systemctl restarts.com/news-at-gt; click on Ctrl-d at the same time to end the scheduled task job 3 at Mon Apr 27 23:30:00 root@linuxprobe 2016 a root

If the reader wants to challenge a more difficult but simpler way, he can put the previously learned pipe character (any door) between the two commands and have the at command receive the output of the previous echo command for the purpose of creating a scheduled one-time task in a non-interactive manner.

[root@linuxprobe ~]# echo "systemctl restart httpd" | at 23:30 job 4 at Mon Apr 27 23:30:00 2015 [root@linuxprobe ~]# at -l 3 Mon Apr 27 23:30:00 2016 a root 4 Mon Apr 27 23:30:00 2016 a root

If we accidentally set up two one-time scheduled tasks, we can easily delete one of them using the following command:

[root@linuxprobe ~]# atrm 3 [root@linuxprobe ~]# at -l 4 Mon Apr 27 23:30:00 2016 a root

If we want Linux systems to perform specific tasks periodically and regularly, the crond service that is enabled by default in Linux systems is simply perfect. T he command to create and edit a scheduled task is "crontab -e," the command to view the current scheduled task is "crontab -l," and the command to delete a scheduled task is "crontab -r." In addition, if you are signed in as an administrator on a system, you can also add the -u parameter to the crontab command to edit someone else's planned tasks.

Before you formally deploy your planned tasks, please read the "Time, Time, Day, Month, Week Command" with Mr. Liu. T his is the use of the cross service to format the parameters of the task (see Table 4-6). I t is important to note that if some fields are not set, you will need to use an asterisk (*) placeholder, as shown in Figure 4-23. C hapter 4 Vim Editor with Shell Command Script. Chapter 4 Vim Editor with Shell Command Script.

Figure 4-23 formats the parameters of a task using cross

Table 4-6 uses cross to set the parameter field description for the task

Field Description Minutes Any integer with a value of 0 to 59 Any integer with a value of 0 to 23 Any integer with a value of 1 to 31 Any integer month with a value of 1 to 12 Any integer week with a value of 0 to 7, where 0 and 7 are commands or program scripts to be executed by the Sunday command

Suppose that every Monday, Wednesday, and Friday at 3:25 a.m., you need to use the tar command to package a site's data catalog as a backup file. W e can use the crontab -e command to create scheduled tasks. You don't need to use the -u parameter to create a scheduled task for yourself, as shown in the results of the crontab -l command:

[root@linuxprobe ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@linuxprobe ~]# crontab -l 25 3 1,3,5 /usr/bin/tar -czvf backup.tar.gz /home/wwwroot

It should be noted that, in addition to using commas (,), multiple time periods are represented, such as "8, 9, 12" for August, September, and December. Y ou can also use a minus sign (-) to represent a continuous period of time (for example, the field "Day" is valued "12-15", which means 12-15 days per month). and the interval between tasks with a divide sign (/) (for example, "

If you need command statements that contain multiple scheduled tasks at the same time in the cross service, you should write only one line per line. F or example, let's add a scheduled task that automatically emptys all files in the /tmp directory every Monday through Friday at 1 a.m. In particular, in the planned task parameters of the crond service, all commands must be written in an absolute path, if you do not know the absolute path, please query with the whereis command, the rm command path is the bold part of the output information below.

[root@linuxprobe ~]# whereis rm rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz /usr/share/man/man1p/rm.1p.gz [root@linuxprobe ~]# crontab -e crontab: installing new crontab [root@linuxprobe ~]# crontab -l 25 3 1,3,5 /usr/bin/tar -czvf backup.tar.gz /home/wwwroot 0 1 1-5 /usr/bin/rm -rf /tmp/*

At the end of this section, Mr. Liu Wei would like to make a few more words about using the planning service at work.

In the configuration parameters of the crond service, you can write comments at the beginning of the sign, as the Shell script does, so that you can quickly understand important information such as its functionality, requirements, and writers when reviewing the command code later.

The Score field in the scheduled task must have a numeric value, must never be empty or a number, and the Day and Week fields cannot be used at the same time, otherwise conflicts will occur.

Finally, I think readers have found that many services, such as cross, call the Vim editor by default, I believe you can now further appreciate the benefits of mastering the Vim text editor in Linux systems. S o please be sure to learn the next chapter after thoroughly mastering the Vim encoder. https://www.linuxprobe.com/chapter-05.html