In windows, windows provides the task scheduling function. Today, we mainly learn how to use the at command for one-time scheduled tasks in linux.
In windows, windows provides the task scheduling function. in the control panel-> performance and maintenance-> task scheduling, the function is to schedule tasks that run automatically. You can create a scheduled task through step-by-step guidance of 'add Task Scheduler.
In linux, you may have discovered why the system often performs some tasks automatically? Who are responsible for these tasks? In linux, if you want to enable a self-designed backup program to automatically start running under the system at a certain point in time without manually starting it, what should you do? These routine work may be divided into one-time scheduled work and cyclic scheduled work. what services are responsible for in the system? Also, if you want to send a letter one day before your wife's birthday every year to remind you not to forget, what should you do in linux? Today, we will mainly learn how to use the at command for one-time scheduled task!
1. command format:
At [parameter] [TIME]
2. command functions:
A specified task can be executed only once at a specified time, and the atd process must be enabled (
Ps-ef | grep atd view, enable/etc/init. d/atd start or restart, and run chkconfig -- level 2345 atd on when started ).
3. command parameters:
-M after the specified task is completed, an email is sent to the user even if no standard output is provided.
-I atq alias
-D atrm alias
-V: display the time when the task will be executed
-C: print the task content to the standard output
-V: Display version information
-Q <列队> Use the specified queue
-F <文件> Reads a task from a specified file instead of a standard input.
-T <时间参数> Submit the task to run as a time parameter
At allows a quite complex set of time-based methods. He can accept the time specified in the hh: mm (hour: minute) format of the day. If the time has passed, it will be executed on the next day. Of course, you can also use vague words such as midnight (late night), noon (noon), teatime (Tea Time, usually four o'clock P.M.) to specify the time. The user can also use a 12-hour hourly system, that is, add AM (morning) or PM (afternoon) after the time to indicate whether it is morning or afternoon. You can also specify the specific date of command execution, in the format of month day (month day), mm/dd/yy (month/day/year) or dd. mm. yy (day. month. year ). The specified date must be later than the specified time. The above is the absolute timing method. In fact, the relative timing method can be used, which is very good for the command to be executed soon. The specified format is: now + count time-units, now is the current time, and time-units is the time unit. here it can be minutes (minutes), hours (hours), days (days) and weeks (week ). Count indicates the number of times, days, hours, and so on. Another method of timing is to use today (today) and tomorrow (tomorrow) to specify the time for command completion.
TIME: TIME Format. you can define the TIME when the at task is to be executed. the formats include:
HH: MM
Ex>
This task is performed at HH: MM today. if the time has exceeded, HH: MM tomorrow will perform this task.
HH: MM YYYY-MM-DD
Ex>
Forces the task to be performed at a special time of a certain day of a certain year or month.
HH: MM [am | pm] [Month] [Date]
Ex> 04 pm March 17
The same is true. this task is forced to be executed at a certain time point in a certain year, month, or day.
HH: MM [am | pm] + number [minutes | hours | days | weeks]
Ex> now + 5 minutes
Ex> 04 pm + 3 days
That is to say, the task will be executed only a few more times at a certain time point.
4. example:
Example 1: Run/bin/ls at five o'clock P.M. after three days
Command: at 5 + 3 days
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # At 5 + 3 days
At>/bin/ls
At>
Job 7
[Root @ localhost ~] #
Note:
Instance 2: tomorrow, and the output time is within the specified file
Command: at 17:20 tomorrow
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # At 17:20 tomorrow
At> date>/root/2013.log
At>
Job 8
[Root @ localhost ~] #
Note:
Instance 3: After the task is scheduled, you can run the atq command to check whether the task is executed.
Command: atq
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Atq
8 a root
7 a root
[Root @ localhost ~] #
Note:
Instance 4: Delete a set task
Command: atrm 7
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Atq
8 a root
7 a root
[Root @ localhost ~] # Atrm 7
[Root @ localhost ~] # Atq
8 a root
[Root @ localhost ~] #
Note:
Instance 5: displays the configured task content.
Command: at-c 8
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # At-c 8
#! /Bin/sh
# Atrun uid = 0 gid = 0
# Mail root 0
Umask 22 n characters are omitted here
Date>/root/2013.log
[Root @ localhost ~] #
Note:
5. how to start and run atd:
5.1 atd startup
To use a one-time scheduled task, the atd service must be provided on the Linux system. However, not all Linux distributions will open it by default. Therefore, you must manually activate the atd service at some time points. The activation method is simple:
Command:
Copy codeThe code is as follows:
/Etc/init. d/atd start
/Etc/init. d/atd restart
Output:
Copy codeThe code is as follows:
[Root @ localhost/] #/etc/init. d/atd start
[Root @ localhost/] #/etc/init. d/atd
Usage:/etc/init. d/atd {start | stop | restart | condrestart | status}
Copy codeThe code is as follows:
[Root @ localhost/] #/etc/init. d/atd stop
Stop atd: [OK]
[Root @ localhost/] # ps-ef | grep atd
Root 25062 24951 0 00:00:00 pts/0 grep atd
[Root @ localhost/] #/etc/init. d/atd start
[OK] td: [OK]
[Root @ localhost/] # ps-ef | grep atd
Root 25068 1 0 14: 53? 00:00:00/usr/sbin/atd
Root 25071 24951 0 00:00:00 pts/0 grep atd
[Root @ localhost/] #/etc/init. d/atd restart
Stop atd: [OK]
[OK] td: [OK]
[Root @ localhost/] #
Note:
When/etc/init. d/atd start is not started, start the atd service directly.
Restart the atd service after the/etc/init. d/atd restart service is started.
Note: configure to start the service at startup, so that you do not have to start the service again at every restart.
Command: chkconfig atd on
Output:
Copy codeThe code is as follows:
[Root @ localhost/] # chkconfig atd on
[Root @ localhost/] #
5.2 at running mode
Since the task is scheduled, there should be a way to execute the task, and these tasks are arranged in the table. So how does one plan tasks? In fact, we use the at command to generate the scheduler task to be run and write the scheduler task to the/var/spool/at/directory in the form of text files, this work will be able to wait for the access and operation of the atd service. That's simple.
However, not all people can schedule at tasks. Why? Because of system security. After many hosts are cracked by so-called attacks, the most common cause is that many hacker programs are added to their systems, these programs are very likely to use some scheduled tasks to run or collect your system running information, and regularly send it to hackers. Therefore, do not use the at command unless it is an account that you acknowledge. So how can we achieve the controllable use of?
We can use the/etc/at. allow and/etc/at. deny files to restrict at usage. After these two files are added, the at operation is like this:
Search for the/etc/at. allow file first. only users in the file can use at. users in this file cannot use at (even if they are not written in at. deny );
If/etc/. if allow does not exist, search for/etc/. if the file deny is written in. deny users cannot use at instead of. the user in the deny file can use the at command.
If neither file exists, only root can use the at command.
Through this instruction, we know/etc/. allow is a strict management method, while/etc/. deny is loose (because the account is not in the file, it can run ). In general distributions, since all users on the system are trustworthy, the system usually keeps an empty/etc/. deny file, which means allowing everyone to use the at command (you can check the file by yourself ). However, if you do not want some users to use at, write the user's account to/etc/at. deny! One account writes one line.