A linux command per day (49): the 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:

Execute a specified task at a specified time, which 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 start; Requires running chkconfig --level 2345 atd on).

3. Command parameters:

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

#p#page header #e#-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 tasks from the specified file instead of

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



at allows a rather complex set of methods for specifying time. 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 to perform at this task, the format is:

HH:MM

ex> 04:00#p#page title#e#

is performed at today's HH:MM time, if this time If it has been exceeded, do this task at HH:MM tomorrow.

HH:MM YYYY-MM-DD

ex> 04:00 2009-03-17

It is mandatory to perform this task at a special moment on a certain day in a certain year and a certain month

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

ex> 04pm March 17

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

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

ex> now + 5 minutes

ex> 04pm + 3 days

means that the task will be performed after a few hours at a certain point in time.



4. Example of use:

Example 1: Execute /bin/ls

#p#page title#e# command at 5:00 pm after three days:

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 ~]#

Description:



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

Command :

at 17:20 tomorrow

Output: #p#Pagination title#e#

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

at> date >/root/2013.log        

at> <EOT>

job 8 at 2013-01-06 17:20

[root@localhost ~]#



Description:



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

Command :

atq

output:

#p#Page title#e#[root@localhost ~]# atq

8 2013-01-06 17:20 a root

7 2013-01-08 17:00 a root

[root@localhost ~]#



Description:



Example 4: Delete Tasks already set

Command :

atrm 7

Output:

[root@localhost ~]# atq

8 2013-01-06 17:20 a root

#p#page title#e#7 2013-01-08 17:00 a root

[root @localhost ~]# atrm 7

[root@localhost ~]# atq

8 2013-01-06 17:20 a root

[root@localhost ~]#

Description:



Example 5: Display the task content that has been set

Command :

at -c 8

output:

[root@localhost ~]# at -c 8

#p#page title#e##!/bin/sh

# atrun uid=0 gid=0

# mail root 0

umask 22 n characters are omitted here

date >/root/2013.log

[root@localhost ~]#

Description:



Example 6:

Command:

Output:

Description:

5. How to start atd and how at runs:

#p#Page title#e#5.1 Startup 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 this:

Command:

/etc/init.d/atd start

/etc/init.d/atd restart

Output:

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

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

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

[root@localhost /]#p#page title#e## /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#p#page title#e#@localhost /]# /etc/init.d/atd restart

stop atd: [OK]

[OK] td: [OK]

[root@localhost /]#

Description:

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

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



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

Command :

chkconfig atd on

output:

[root@localhost /]# chkconfig atd on#p#page title#e#

[root@localhost /] #



5.2 The operation mode of at

Since it is a scheduled task, there should be a way to execute the task and schedule these tasks 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 at scheduled tasks. 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 #p#page title#e#system operation information, And regularly sent to the hacker. So, unless it's an account you approve of, don't let them use the at command yet. How to achieve the controllable use of at?

We can use the two files /etc/at.allow and /etc/at.deny to limit the use of at. After adding these two files, the working situation of at is as follows:

first find the file /etc/at.allow, users written in this file can use at, and users who are not in this file cannot use it at (even if 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 more strictly managed, while /etc/at.deny is looser (because the account is not in this 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 users to use at, write that user's account to /etc/at.deny! Write one line for an account number.

Reprinted in: http://www.itxuexiwang.com/a/liunxjishu/2016/0306/245.html?1457358449

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326735012&siteId=291194637