Linux Essentials Mission Plan (AT,CRONTAB)

Source: Internet
Author: User

In a Linux system you may have found out why the system often does some tasks automatically. Who are those who are at the mercy of their work? In a Linux system, what if you want your own backup program to start running under the system automatically at some point in time, without having to start it manually? These routine work may be divided into one-time timing work and cycle time work, in the system is also what services in charge?

command at and Crontab can solve the above problems


At: Perform a task at some point in the future

Crontab: Used to run a task periodically

The results of the execution of the two or sent to the user by mail.


AT command

Common formats: at [option] ... Time

At allows you to use a fairly complex set of time-specific methods. He was 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, today 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).


Time: Date format, where you can define when to do at this task, in the following format:

hh:mm

Ex> 04:00

At today's hh:mm time, if that moment is over, tomorrow's hh:mm will do the job.

hh:mm YYYY-MM-DD

Ex> 04:00 2016-09-19

To mandate the task at a particular time of day of the month of the year.

HH:MM[AM|PM] [Month] [Date]

ex> 04pm September 17

Also, forcing a certain day of the year to perform the task at some point in the month

HH:MM[AM|PM] + number [Minutes|hours|days|weeks]

Ex> now + 5 minutes

Ex> 04pm + 3 days

That is, it takes a few more minutes to do that task at a certain point in time.


Common options:

-L: View job queue, equivalent to ATQ

-F: Reads the job task from the specified file without having to enter interactively

-D: Deletes the specified job, equivalent to ATRM

-C: View the specific contents of the specified job

-Q: Indicates the queue

Note: Job execution results are sent to the user who submitted the job by mail

Before using the AT command, we need to first confirm that the ATD process on which the at depends is enabled, using the command: PS-EF | grep ATD View, open with/ETC/INIT.D/ATD start or restart if not enabled.


Execution mode:

1) Interactive, after the command input is completed use ctrl+d save exit; 2) input redirection; 3) at-f file


Example 1: Use the LS command to expand the/tmp directory after 2 minutes (after the interactive command input is finished, use Ctrl+d to save the exit)

[[email protected] ~]# at now + 2 minutes at> ls/tmpat> <eot>job 7 at Sun Sep 18 09:17:00 2016

Command execution results look in mail

[[email protected] ~]# mailheirloom mail version 12.5 7/5/10.   Type ? for help. " /var/spool/mail/root ": 9 messages 1 unread    1 [email  protected]  tue jul 19 16:46  90/2915   "[abrt] full  Crash report "    2 [email protected]  thu sep  8  11:00  16/568    "output from your job         1 "    3 [email protected]  Mon Sep  12 10:31 353/15306  "[Abrt] full crash report"     4 [ email protected]  mon sep 12 10:31 443/17535  "[abrt] full  Crash report "&NBSP;&NBSP;&NBSP;&NBSP;5&NBSP;ROOT&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;        sun sep 18 09:04  23/998     "Output from your job        3"      6 root                   Sun Sep 18 09:08  19/827    "Output from  your job        4 "    7 root                   sun  Sep 18 09:13 275/2986   "output from your job         5 "    8 root                   Sun Sep 18 09:15   26/552    "OuTput from your job        6 ">U  9  root                   Sun Sep 18 09:17  18/776    "Output from your job         7 ">U  9 root                   sun sep 18 09:17   18/776    "output from your job         7 "& 9Message  9:From [email protected]  Sun Sep  18 09:17:01 2016return-path: <[email protected]>x-original-to: rootdelivered-to : [email protected]subject: output from your job         7to:&nbsP [Email protected] date: sun, 18 sep 2016 09:17:01 +0800  (CST) from: [email protected ]  (Root) status:  rosystemd-private-2c8c4ebab1524b5c9f463bd2ec8ffea7-colord.service-gyaadpsystemd-private-2c8c4ebab1524b5c9f463bd2ec8ffea7- Cups.service-6gb4mmsystemd-private-2c8c4ebab1524b5c9f463bd2ec8ffea7-rtkit-daemon.service-25opvwsystemd-private-2c8c4ebab1 524b5c9f463bd2ec8ffea7-vmtoolsd.service-sw2cqm

Example 2: Tomorrow at 14, restart the system. (implemented using input redirection)

[[email protected] ~]# at $ tomorrow >> time1 << eof> reboot> eofjob 8 at Mon Sep 19 14:00:00 2016[[ Email protected] ~]# Atq8mon Sep 14:00:00 a root

Example 3: Delete a scheduled task for example 2

[Email protected] ~]# at-d 8[[email protected] ~]# ATQ


Execute permissions for at command:

Configuration file/etc/at. {Allow,deny} determines which users are allowed to use the AT command and which ones are not.


After adding these two files, at is working like this:

First find/etc/at.allow This file, written in this file users can use at, no user in this file can not use at (even if not written in At.deny);

If the/etc/at.allow does not exist, look for/etc/at.deny this file, if the user written in this at.deny can not use at, but not in this At.deny file users, it is possible to use the AT command.

If none of the two files exist, then only root can use the AT command.

With this explanation, we know that/etc/at.allow is a more rigorous way of managing, while/etc/at.deny is more loosely (because the account is not in the file, it is able to run at). In general distributions, because it is assumed that all users on the system are trustworthy, an empty/etc/at.deny file is usually retained, meaning that all people are allowed to use the AT command (you can check the file yourself). However, in case you don't want some users to use at, write the user's account to/etc/at.deny! One account to write a line.


Cron comes from the Greek word chronos (meaning "time"), which is the next periodic program for the Linux system to perform the specified tasks automatically.

Before using cron, you need to confirm that the Crond daemon is running in order to ensure that Cron is running properly.

Available by command: Systemctl status Crond and Ps-ef | grep Crond to see if the service cron is in a running state.


The way to submit a job to Crond is different from the 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; The crontab command is required;

Cron tasks fall into two categories:

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

Manually edit the/etc/crontab file

User Cron Task: Using the command crontab


/etc/crontab file

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/87/58/wKiom1fd8KHRembWAAAv68Igy1w180.gif "title=" Cron1.gif "width=" 680 "height=" 281 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:680px;height:281px; "alt=" Wkiom1fd8khrembwaaav68igy1w180.gif "/>

Note:

1) Define a recurring task for each row; Total 7 fields

* * * * *: Define Periodic time

Username: User identity for running a task

command to be executed: the task to perform

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 define the PATH environment variable

3) Execution result message sent to mailto specified user (sent to user running task)


Time notation:

(1) a specific value;

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

(2) *

All values in the range of valid values at a given point in time;

means "every ...";

Example:

1> 3 * * *: first three minutes per hour

2> 3 4 * 5: Friday 4:03 per week

3> 5 6 7 * *: 7th # 6:05 per month executed once

4> 7 8 9 10 *: once every October 9 8:7

(3) Discrete value: Multiple values separated by commas in time: #,#,#

Example:

5> 9 8 * 3,7:8:9 every Wednesday Sunday

6> 0 8,20 * * 3,7: Every Wednesday Sunday 8:00 followed by 20:00

(4) Continuous value: Used at point-in-time-link start and end: #-#

Example:

7> 0 9-18 * 1-5: from Monday to Friday from 9 to 18 every hour.

(5) in the specified time range, define the step size:

/#: #即为步长

Note: 1) When a specified point in time cannot be divisible by stride length, its meaning ceases to exist:

2) The minimum time unit is "minutes", want to complete the "second" level task, need to rely on other mechanisms: defined as a per-minute task, and in the use of script implementation of the loop multiple times per minute


User cron

crontab command definition, each user has a dedicated cron task file:/var/spool/cron/username

Only the user has defined a cron task to see the appropriate information through the file.

Note: 1) Each row defines a cron task with a total of 6 fields (no user required); 2) The message is sent to the current user

crontab command

Use format:

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

-L: Lists all tasks;

-E: Editing tasks;

-R: Remove All Tasks;

-I: Used in conjunction with-R to allow users to selectively remove assigned tasks in interactive mode;


-U User: Only root can run, on behalf of the designated users to manage cron tasks;


Note: The results of the operation are notified to the relevant users by mail, and if you do not want to receive mail, use the following command

(1) COMMAND >/dev/null

(2) COMMAND &>/dev/null

Thinking:

(1) How do I run a task at the second level?

* * * * * for min in 0 1 2; Do echo "HI"; Sleep 20; Done

(2) How do I run a task every 7 minutes?


Sleep command:

Sleep Number[suffix] ...


SUFFIX:

S: seconds, default

M: minutes

H: Hours

D: Day


Practice:

1, every 4 hours to back up once/etc directory to/backup directory, save the file name format is "ETC-YYYY-MM-DD-HH.TAR.XZ";

Add the following in the/etc/crontab file:

0 4 * * *./test/bak_etc

Cat Bak_etc

#!/bin/bash

#bakup/etc Diratory

#auther Chawan date:20160908

cp/etc/*/bakup/etc$ (Date +%f-%h)

xz/bakup/etc$ (Date +%f-%h)


2, weekly 2, 4, 7 backup/var/log/messages file to/logs directory, file name is like "MESSAGES-YYYYMMDD";

* * * * * 2,4,7./test/logs

Cat/test/logs

#!/bin/bash

#bakup/var/log/messages To/logs

cp/var/log/messages/logs/messages$ (Date +%f)

xz/logs/messages$ (Date +%f)


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

0 2 * * * cat/proc/meminfo | grep "^[SM" >>/tmp/meminfo.txt


4, weekday time, the user Docker every small execution of "IP addr Show" command;

Switch to Docker user using command Crontab-e to enter the editing interface

Save exit after adding "0 9-18 * * 1-5 docker IP addr Show" message

[Email protected] ~]$ crontab-l

0 9-18 * * 1-5 IP addr Show



This article is from "Zhang Fan-it's fantasy drifting" blog, please be sure to keep this source http://chawan.blog.51cto.com/9179874/1853509

Linux Essentials Mission Plan (AT,CRONTAB)

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.