Linux common commands: at command


  In the Windows system, Windows provides the function of scheduling tasks. In Control Panel -> Performance and Maintenance -> Task Scheduler, its function is to schedule tasks to run automatically. Through the step-by-step guide of 'Add a task plan', you can create a scheduled task.

  In a linux system you may have discovered why the system often performs some tasks automatically? Who is directing their work on these tasks? In the Linux system, if you want to make the backup program you designed can automatically start running under the system at a certain point of time, without having to start it manually, what should you do? These routine jobs may be further divided into one-time scheduled jobs and recurring scheduled jobs. Which services are responsible in the system? Also, if you want to send a letter every year before your wife's birthday to remind yourself not to forget, what should you do under linux? 

  Today we mainly learn the usage of the at command of one-time scheduled task!

1. Command format:

  at[parameter][time]

2. Command function:

  To execute a specified task at a specified time, it can only be executed once, and the atd process needs to be started (

  ps -ef | grep atd to view, use /etc/init.d/atd start or restart to open; to start when booting, you need to run chkconfig --level 2345 atd on).

3. Command parameters:

-m will send an email to the user when the specified task is completed, even if there is no standard output

-I alias for atq

-d alias for atrm

-v show when the task will be executed

-c print the contents of the task to standard output

-V show version information

-q <queue> use the specified queue

-f <file> Read task from specified file instead of stdin

-t <time parameter> Submit the task to be run as a time parameter 

  at allows a fairly complex set of methods for specifying times. He is able to accept time designations in the hh:mm (hours:minutes) style of the day. If the time has passed, it will be executed on the next day. Of course, vague words such as midnight (late night), noon (noon), teatime (tea time, usually 4 pm) can also be used to specify the time. Users can also use a 12-hour clock, which means AM (morning) or PM (afternoon) after the time to indicate whether it is AM or PM. It is also possible to specify the specific date of the command execution in the format of month day (month day) or mm/dd/yy (month/day/year) or dd.mm.yy (day.month.year). The specified date must follow the specified time. The above are all absolute timing methods. In fact, relative timing methods can also be used, which is very beneficial for arranging commands that will be executed soon. The specified format is: now + count time-units, now is the current time, time-units is the time unit, here can be minutes (minutes), hours (hours), days (days), weeks (weeks). count is the amount of time, whether it is days, hours, etc. Another timing method is to directly use today (today), tomorrow (tomorrow) to specify the time to complete the command.

  TIME: Time format, here you can define when the at task is to be performed. The format is:

  HH:MM

  ex> 04:00

  Do it at today's HH:MM, and if that time has passed, do it tomorrow at HH:MM.

  HH:MM YYYY-MM-DD

  ex> 04:00 2009-03-17

  Mandating that the task be carried out at a special time on a certain day of a certain year in a certain month

  HH:MM[am|pm] [Month] [Date]

  ex> 04pm March 17

  The same is true, forcing the task to be performed at a certain time of a certain year, a certain month, and a certain day

  HH:MM[am|pm] + number [minutes|hours|days|weeks]

  ex> now + 5 minutes

  ex> 04pm + 3 days

  That is, at a certain point in time plus a few more hours before doing the task.

4. Example of use:

Example 1: Execute /bin/ls at 5 pm after three days

Order:

at 5pm+3 days

output:

[root@localhost ~]# at 5pm+3 days

at> /bin/ls

at> <EOT>

job 7 at 2013-01-08 17:00

[root@localhost ~]#

illustrate:

Example 2: At 17:00 tomorrow, output the time to the specified file

Order:

  at 17:20 tomorrow

output:

[root@localhost ~]# at 17:20 tomorrow

at> date >/root/2013.log         

at> <EOT>

job 8 at 2013-01-06 17:20

[root@localhost ~]#

illustrate:

 

Example 3: After the scheduled task is set, we can use the atq command to check that the system has not executed work tasks before it is executed.

Order:

  and

output:

[root@localhost ~ ]#

8       2013-01-06 17:20 a root

7       2013-01-08 17:00 a root

[root@localhost ~]#

illustrate:,

 

Example 4: Delete a task that has been set

Order:

  atrm 7

output:

[root@localhost ~ ]#

8       2013-01-06 17:20 a root

7       2013-01-08 17:00 a root

[root@localhost ~]# atrm 7

[root@localhost ~ ]#

8       2013-01-06 17:20 a root

[root@localhost ~]#

illustrate:

 

Example 5: Display the task content that has been set

Order:

  at -c 8

output:

[root@localhost ~]# at -c 8

#!/bin/sh

# atrun uid = 0 gid = 0

# mail     root 0

umask 22 omit n characters here

date >/root/2013.log

[root@localhost ~]#

illustrate:

 

Example 6:

Order:

output:

illustrate:

 

5. The way atd starts and at runs:

5.1 Start of atd

  To use a one-time scheduled task, our Linux system must have a service responsible for this scheduled task, that is, the atd service. However, not all Linux distributions will turn it on by default, so at some point we need to manually activate the atd service. The activation method is very simple, it is like this:

Order:

/etc/init.d/atd start 

/etc/init.d/atd restart 

output:

[root@localhost /]# /etc/init.d/atd start

[root@localhost /]# /etc/init.d/atd 

用法:/etc/init.d/atd {start|stop|restart|condrestart|status}

[root@localhost /]# /etc/init.d/atd stop

stop atd: [OK]

[root@localhost /]# ps -ef|grep atd

root      25062  24951   0  14:53 pts / 0 00:00:00 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  14:53 pts / 0 00:00:00 grep atd _ _ _ _ _     

[root@localhost /]# /etc/init.d/atd restart

stop atd: [OK]

[OK] td: [OK]

[root@localhost /]#

illustrate:

When /etc/init.d/atd start is not started, start the atd service directly

/etc/init.d/atd restart After the service has been started, restart the atd service

Note: Start this service when you configure it to start, so as not to have to do it again every time you restart

Order:

chkconfig atd on

output:

[root@localhost /]# chkconfig atd on

[root@localhost /]#

5.2 How at works

  Since it is a scheduled task, there should be a way for the task to be executed, and these tasks are scheduled in the schedule. So how does the method of generating the scheduled task work? In fact, we use the at command to generate the scheduled task to be run, and write the scheduled task into the /var/spool/at/ directory as a text file. The job can wait for the atd service to be available and running. It's that simple.

  However, not everyone can perform the at scheduled task. Why? For system security reasons. After many hosts are cracked by so-called attacks, the most common finding is that there are many hacker programs in their systems. These programs are very likely to use some scheduled tasks to run or collect your system operation information, and send them to hackers regularly. So, unless it's an account you approve of, don't let them use the at command yet. How to achieve the controllability of using at?

  We can use the two files /etc/at.allow and /etc/at.deny to restrict the use of at. After adding these two files, at works like this:

  First find the file /etc/at.allow  , users written in this file can use at , users who are not in this file cannot use at (even if it is not written in at.deny);

  If /etc/at.allow does not exist, look for the file /etc/at.deny. If the user written in this at.deny cannot use at, and there is no user in this at.deny file, then The at command can now be used.

  If neither file exists, only root can use the at command.

  From this description, we know that /etc/at.allow is the more strictly managed way, while /etc/at.deny is looser (because the account is not in that file, it can run at). In general distributions, since all users on the system are assumed to be trusted, the system usually keeps an empty /etc/at.deny file, which means that everyone is allowed to use the at command (you can check it yourself the file). However, in case you don't want some user to use at, just write that user's account into /etc/at.deny! Write one line for an account number.

Guess you like

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