Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the at and crontab commands in Linux to schedule a task server at a specified time

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The content of this article mainly focuses on how to use at and crontab commands in Linux to plan tasks at a specified time. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Experienced system operation engineers can enable Linux to automatically enable or stop certain services or commands in a specified period of time without human intervention, so as to realize the automation of operation and maintenance. Although we now have a powerful script to perform some batch work, it's too painful if you still need to hit the enter key at 2: 00 a. M. to execute the script (of course, you can also train your kitten to press enter in the middle of the night). Next, we will explain how to set up the scheduled task service of the server and leave the periodic and regular work to the system to be completed automatically.

Planned tasks are divided into one-time planned tasks and long-term planned tasks, which can be understood in the following ways.

As the name implies, one-time scheduled tasks are performed only once and are generally used to meet temporary work needs. We can do this with the at command, just write it in the form of "at time". If you want to see an one-time scheduled task that has been set up but not yet executed, you can use the "at-l" command; to delete it, you can use the "atrm task serial number". When you use the at command to set up one-time scheduled tasks, the default is an interactive approach. For example, use the following command to set the system to automatically restart the Web service at 23:30 tonight.

[root@liangxu ~] # at 23:30at > systemctl restart httpdat > here please press Ctrl+d at the same time to finish writing the planning task job 3 at Mon Apr 27 23:30:00 2015 [root@liangxu ~] # at-L3 Mon Apr 27 23:30:00 2016 a root

If you want to challenge a more difficult but simpler approach, you can place the pipe character (any door) between the two commands and let the at command receive the output of the previous echo command to achieve the goal of creating scheduled one-time tasks in a non-interactive way.

[root@liangxu ~] # echo "systemctl restart httpd" | at 23:30job 4 at Mon Apr 27 23:30:00 2015 [root@liangxu ~] # at-L3 Mon Apr 27 23:30:00 2016 a root4 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@liangxu ~] # atrm 3 [root@liangxu ~] # at-L4 Mon Apr 27 23:30:00 2016 a root

If we want the Linux system to perform certain specific tasks periodically and regularly, then the crond service enabled by default in the Linux system is simply appropriate. The 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 logged on to the system as an administrator, you can add the-u parameter to the crontab command to edit other people's scheduled tasks.

Please read the formula "order of minute, hour, day, month and week" before you formally deploy the planned task. This is the parameter format for the task using the crond service. It is important to note that if some fields are not set, you need to use an asterisk (*) placeholder.

Use crond to format the parameters of a task

Use crond to set the parameter field description for a task

Field description any integer day with a value of 059. any integer day with a value of 1q31, an integer month with a value of 1q12, an arbitrary integer week with a value of 07. where 0 and 7 are commands or program scripts to be executed by Sunday commands.

Suppose that at 03:25 every Monday, Wednesday, and Friday, you need to use the tar command to package the data directory of a Web site as a backup file. We can use the crontab-e command to create scheduled tasks. You do not need to use the-u parameter to create a scheduled task for yourself. The specific parameters to achieve the effect are shown in the result of the crontab-l command:

[root@liangxu ~] # crontab-eno crontab for root-using an empty onecrontab: installing new crontab [root@liangxu ~] # crontab-L25 3 * * 3Jing 5 / usr/bin/tar-czvf backup.tar.gz / home/wwwroot

It is important to note that in addition to using a comma (,) to represent multiple time periods, for example, "8pr 9jue 12" means August, September and December. You can also use a minus sign (-) to indicate a continuous period of time (for example, if the value of the field "day" is "12-15", it means the 12th-15th of each month). And a division sign (/) is used to indicate the interval between the execution of the task (for example, "* / 2" indicates that the task is executed every 2 minutes).

If you need to include multiple command statements for scheduled tasks at the same time in the crond service, only one should be written per line. For example, let's add another scheduled task that automatically empties all files in the / tmp directory at 1 a.m. from Monday to Friday. In particular, it is important to note that in the scheduled task parameters of the crond service, all commands must be written in an absolute path. If you do not know the absolute path, use the whereis command to query. The path of the rm command is the bold part of the output information below.

[root@liangxu ~] # whereis rmrm: / usr/bin/rm / usr/share/man/man1/rm.1.gz / usr/share/man/man1p/rm.1p.gz [root@liangxu ~] # crontab-ecrontab: installing new crontab [root@liangxu ~] # crontab-L25 3 * * 3Meiko 5 / usr/bin/tar-czvf backup.tar.gz / home/wwwroot0 1 * * 1-5 / usr/bin/rm-rf / tmp/*

Finally, let's say a few more points for attention on the use of planning services at work.

In the configuration parameters of the crond service, you can write comments starting with the # sign like a Shell script, so that you can quickly learn about its functions, requirements, and writers when you review the command code later.

The "minutes" field in a scheduled task must have a numeric value, must not be empty or a * sign, and the "day" and "week" fields cannot be used at the same time, otherwise conflicts will occur.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

Thank you for your reading. I believe you have some understanding of "how to use at and crontab commands in Linux to plan task service programs at a specified time". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report