Planned at and Cron on Linux

Source: Internet
Author: User

AT command


Confirm that the ATD service has been started before using

[Email protected] tmp]# Ps-ef |        grep atdroot 35241 1 0 16:15? 00:00:00/usr/sbin/atd[[email protected] tmp]#/etc/init.d/atd status #运行状态查看atd (PID 35241) is running ...

Start command

[[email protected] tmp]#/etc/init.d/atd start

Set boot up

[Email protected] tmp]# chkconfig ATD on


The AT command is used to execute commands at a specified time. At allows you to use a fairly complex set of time-specific methods. It is able to accept the time specified in the hh:mm (hour: minute) of the day. If the time has passed, then it will be executed the next day. Of course, you can use Midnight (late night), noon (noon), Teatime (tea time, usually 4 o'clock in the afternoon) and other vague words to specify the time. The user is also able to use the 12-hour timekeeping system, which is either a AM (morning) or PM (afternoon) After the time to indicate whether it is morning or afternoon. You can also specify a specific date for the execution of the command, specifying the format for Month Day (month) or mm/dd/yy (Month/day/year) or Dd.mm.yy (day, month, year). The specified date must be followed by the specified time.

The absolute timing method is described above, and the relative timing method can be used, which is good for arranging orders 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 (day), Weeks (week). Count is the amount of time, whether it is a few days, or a few hours, and so on. A more timed method is to specify the time to complete the command directly using today, tomorrow (tomorrow).

Grammar:

at [OPTION] Time

Time:

hh:mm [YYYY-MM-DD]: In an hour-minute manner

Noon,midnight, Teatime: Specifies the time of a blur

Tomorrow: Tomorrow

now+#: For example: at now + 1 minutes execute after one minute

Unit:minutes, hours, days, OR weeks

The at job has a queue, denoted by a single letter, using a queue by default;

Common options:

-L: View job queue, equivalent to ATQ

-f/path/from/somefile: Reads the job task from the specified file without having to interactively enter it;

-D: Deletes the specified job, equivalent to ATRM;

-C: View the specific contents of the specified job;

-Q queue: indicates queues;

After the specific operation command has been entered, the Crtl+d end remains

Example:

Two days after the 3 o'clock in the afternoon execution rm-rf/tmp/a:

[Email protected] script_test]# at 3pm+2 daysat> rm-rf/tmp/a at> <eot>job 4 at 2016-10-23 15:00

Tomorrow 08:30, start the database:

[Email protected] script_test]# at tomorrowat>/etc/init.d/mysqld startat> <eot>job 5 at 2016-10-22 08:3 0

Set up tasks for the specified hours and minutes:

[Email protected] script_test]# at now + hoursat> lsat> touch/tmp/at at> <eot>job 6 at 2016-10-22 02:4 3[[email protected] script_test]# at now + 1 minutesat> cp/etc/passwd/tmpat> <eot>job 7 at 2016-10-21 16:45

After the scheduled task is set, we can use the ATQ command to see that the system does not perform a work task until it is executed:

[Email protected] tmp]# [[email protected] tmp]# atq52016-10-22 08:30 a root42016-10-23 15:00 a root62016-10-22 02:43 a R Oot

To delete a task that has already been set:

[[email protected] tmp]# atq52016-10-22 08:30 a root42016-10-23 15:00 a root62016-10-22 02:43 a root[[email protected] tmp ]# atrm 4[[email protected] tmp]# atq52016-10-22 08:30 a root62016-10-22 02:43 a root

View the contents of a task

[[email protected] tmp]# at-c 5 here omit N bytes/etc/init.d/mysqld start #倒数第二行, is the command to execute


Recurring Task Scheduler: Cron

Before you use Cron to make scheduled tasks, make sure that the Crond service is started and that the Crond service is set up to enable active startup

Ensure that the Crond Daemon (daemon) is in a running state:

CentOS 7:

systemctl Status Crond.service

active:active (running) ...

CentOS 6:

Service Crond Status

.. is running.

The way to submit a job to Crond is different from at, which requires a dedicated configuration file that has a fixed format and is not recommended to edit the file directly using a text editor; To use the crontab command;

Cron tasks fall into two categories:

System cron Task: Mainly used to realize the maintenance of the system itself;

Manual editing:/etc/crontab file

User Cron Task:

command: crontab command

configuration format for system cron:/etc/crontab

Shell=/bin/bash

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root

# for details see Mans 4 Crontabs

# Example of Job definition:

#.----------------minute (0-59)

# | .-------------Hour (0-23)

# |  | .----------Day of Month (1-31)

# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...

# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat

# |  |  |  | |

# * * * * * * user-name command to be executed

Attention:

(1) Each row defines a recurring task, a total of 7 fields;

* * * * * : define periodic time

user-name: User identity for running a task

command to be executed: task

(2) The environment variable here differs from the environment that is obtained after the user logs on, so it is recommended that the command use an absolute path or a custom path environment variable;

(3) Execution result message sent to mailto specified user



Configuration format for user cron:/var/spool/cron/username

Shell=/bin/bash

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root


# for details see Mans 4 Crontabs


# Example of job definition:

#.----------------minute (0-59)

# | .-------------Hour (0-23)

# |  | .----------Day of Month (1-31)

# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...

# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat

# |  |  |  | |

# * * * * * command to be executed

Attention:

(1) Each row defines a cron task, a total of 6 fields;

(2) The environment variable here differs from the environment that is obtained after the user logs on, so it is recommended that the command use an absolute path or a custom path environment variable;

(3) The mail is sent to the current user;

Time notation:

(1) a specific value;

A value in the range of valid values for a given point in time;

Note: Day of week and day of month are not used at the same time;

(2) *

All values in the range of valid values at a given point in time; table "per.."

(3) Discrete value:,

Use a comma-separated number of values at a point in time;

#,#,#

(4) Continuous value:-

Use at point in time-connection start and end

#-#

(5) at a specified point in time, define the step size:

/#: #即步长;

Attention:

(1) When the specified time point cannot be divisible by stride length, its meaning will not exist;

(2) The minimum time unit is "minutes", want to complete the "second" level of the task, you need to use additional mechanisms;

Defined as per-minute tasks: And in the use of script implementation within a minute, the loop executes multiple times;

Example:

(1) 3 * * *: hourly, 3 minutes per hour;

(2) 3 4 * * 5: Weekly, 5 4:3 per week;

(3) 5 6 7 * *: Once a month, the number of 7th in the month 6:5;

(4) 7 8 9 10 *: Every year, October 9 8:7;

(5) 9 8 * * 3, 7: Every Wednesday and Sunday;

(6) 0 8,20 * * 3, 7:8 points and 20 points per Wednesday and Sunday

(7) 0 9-18 * * 1-5: from Monday to Friday from 9 to 18

(8) */5 * * *: Perform a task once every 5 minutes;

(9) */7

crontab command:

crontab [-u user] [-l |-r |-e] [-I.]

-E: Editing tasks;

-L: Lists all tasks;

-R: Remove all Tasks; Delete/var/spool/cron/username files;

-I: Prompt the user for confirmation when removing all tasks with the-r option;

-U user:root users can manage cron tasks for designated users;

Note: The results of the run are notified to the current user by mail, or if you refuse to receive messages:

(1) COMMAND >/dev/null

(2) COMMAND &>/dev/null

Note: When a command is defined, it needs to be escaped if it needs to be used, but the% placed in single quotes is not escaped;

Thinking: When a task fails to execute at a specified time due to shutdown, the next opportunity is not automatically executed.

No!.

If it is expected that a certain time can not be executed on schedule, the next time after the boot, whether or not to the corresponding point in time to execute once, the use of anacron implementation;

Practice:

1, every 12 hours to back up once/etc directory to the/backups directory, save the file name format "Etc-yyyy-mm-dd-hh.tar.gz", and save the log to/backups

0 */12 * * */bin/tar-zcvf/backups/etc. ' Date +\%y-\%m-\%d-\%h '. tar.gz/etc/&>/backups/etcbak.log

2, weekly 2, 4, 7 backup/var/log/secure file to/logs directory, the file name format is "SECURE-YYYYMMDD";

0 * * * 2,4,7/bin/tar-zcvf/logs/secure-' date +\%y\%m\%d '. Tar.gz/var/log/secure &>/logs/etcbak.log

3, every two hours to remove the current system/proc/meminfo file in the beginning of S or m line information appended to the/tmp/meminfo.txt file;

0 */2 * * */bin/grep "^[SM]"/proc/meminfo >>/tmp/meminfo.txt





This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1864365

Planned at and Cron on Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.