Linux Task Scheduler

Source: Internet
Author: User
Tags echo command month name syslog

I. Configuring CRON for Automation

Using Cron to automate task automation can be accomplished by modifying the/etc/crontab file and using the crontab command, which results in the same result.

1./etc/crontab files for Automation

The cron daemon can schedule a daemon that performs repetitive tasks in combination with time, date, month, and week, without human intervention.

To use the Cron service, you must have the VIXIE-CRON-4.2-4.FC8 package installed, and you must run the Crond service to query the package installation and service operation using the following command.

[Email protected] ~]# Rpm-qa |grep Vixie-cron

Check to see if the Vixie-cron package is installed.

[[Email protected] ~]# service Crond status

Crond (PID 2179) is running ...

[Email protected] ~]#

View the Crond service health status.

The main configuration file for Cron is/etc/crontab, which reads as follows.

[Email protected] ~]# Cat/etc/crontab

Shell=/bin/bash

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

Mailto=root

home=/

# 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

The first 4 rows are variables that you can use to configure the Cron task runtime environment. The value of the shell variable tells the system which shell environment to use (in this case, bash). The path variable defines the paths used to execute the command. The output of the cron task is mailed to the user name defined by the mailto variable. If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the home directory to use when executing a command or script.

Each line in the/etc/crontab file represents a task, which is in the following format:

Minute Hour day Month DayOfWeek command

minute: minutes, any integer between 0~59.

Hour: An hour, any integer between 0~23.

Day: Date, any integer from 1~31 (if the month is specified, it must be a valid date for that month).

Month: Any integer between 1~12 (or the English shorthand for using the month, such as Jan,feb, etc.).

DayOfWeek: Week, any integer between 0~7, here 0 or 7 for Sunday (or use the English shorthand of the week such as Sun,mon, etc.).

Command: The commands to execute.

In any of these values, an asterisk (*) can be used to represent all valid values. An asterisk in a month value means that the command is executed monthly after other constraints have been met.

The short line between integers "-" specifies an integer range. For example, 1-4 means an integer 1,2,3,4.

Specify a list with a series of values separated by a comma ",". For example, 3,4,6,8 marks these 4 specified integers.

The forward slash "/" can be used to specify the interval frequency. Adding/<integer> after a range means that integers can be skipped within the range. For example, "0-59/2" can be used to define a time interval of two minutes on a minute field. The interval frequency value can also be used with asterisks. For example, the value of "*/3" can be used in the month field to indicate that the task runs every 3 months.

Two. crontab Command automation

(1). crontab Command Introduction

Users other than root can configure cron tasks using the crontab command. All user-defined crontab are saved in the/var/spool/cron directory and executed using the user who created them.

Create a crontab project as a user, log on as the user, and then type the "crontab-e" command to edit the user's crontab using the editor specified by the visual or editor environment variable. The file uses the same format as the/etc/crontab. When a change to crontab is saved, the crontab file is saved in the file/var/spool/cron/<username> based on the user name.

The cron daemon examines changes in the/etc/crontab file, the/etc/cron.d/directory, and the/var/spool/cron directory every minute. If a change is found, they will be loaded into memory. When a crontab file changes, you do not have to restart the daemon.

The syntax of the crontab command is:

Crontab [-u user Name]-e-l-R

The meanings of the parameters in the command are as follows:

-U: User name, if you log in with your own name, you do not have to use the "-u" option.

-e: Edit the crontab file.

-L: Lists the crontab files.

-R: Delete the crontab file.

(2). Controlling the use of cron

/etc/cron.allow and/etc/cron.deny files are used to limit the use of cron. The two formats used by the control file are one user per line. None of the two files allow spaces. If the control file is modified, the Crond daemon does not have to be restarted. Using control files is read every time a user adds or deletes a cron task.

The root user can use Cron regardless of the rules used in the control file.

If the Cron.allow file exists, only the users listed in it are allowed to use cron, and the Cron.deny file is ignored.

(3). Start and stop services

To start the Cron service, you need to use the service crond Start command. To stop the service, you need to use the "Service crond Stop" command, as follows:

[[Email protected] ~]# service Crond stop

stopping Crond: [OK]

[[Email protected] ~]# service Crond start

Starting Crond: [OK]

[Email protected] ~]#

(3). Create a crontab file

The first thing to do before considering submitting a crontab file to the cron process is to set the environment variable editor.

The cron process is based on its determination to edit the crontab file using that editor. Edit the. bash_profile file under the user host directory, adding the following code to it.

Editor=vi;export EDITOR

Then save the exit.

Next, create a new crontab file that is submitted to the Crond process, which runs every 15 minutes. At the same time, a copy of the newly created file has been placed in the/var/spool/cron directory, and the file name is the user name.

Example 1:

[email protected] ~]# cat a.sh

#!/bin/bash

mkdir Yangtab

[Email protected] ~]# CRONTAB-E

* * * * * *./a.sh

[[email protected] ~]# ls

Anaconda-ks.cfg Install.log Root

a.sh Install.log.syslog sqldeveloper-4.0.1.14.48-1.noarch.rpm

Desktop Music Templates

Documents Pictures Videos

Downloads Public Yangtab

[Email protected] ~]#

As shown above, the Yangtab directory will be created automatically in a minute. (After the directory Yangtab is created, it is no longer created unless the directory Yangtab directory is deleted).

Three. Using at for automation

The cron command is used to dispatch repetitive tasks, while the AT command is used to dispatch a one-time task within a specified timeframe.

1.at Introduction

Once a job is committed, the AT command retains all current environment variables, including the path, and all output of the job is sent to the user as an e-mail message.

As with crontab, the root user can control which users can use the AT command through the At.allow and At.deny files in the/etc directory, and the RPM package must be installed before using the AT command, and the ATD service must be running with the following command:

[Email protected] ~]# Rpm-qa at

At-3.1.10-43.el6_2.1.x86_64

You have new mail in/var/spool/mail/root

[Email protected] ~]#

[[Email protected] ~]# Service ATD Status

ATD (PID 2151) is running ...

[Email protected] ~]#

See if the ATD service is running

2. Configure the At job

To dispatch a one-time job within a specified time, you can enter the "at[Times" command, you must specify a time, and then specify a date.

Command syntax:

At [-F script] [-m-l-R] [TIME] [date]

The parameters in this command have the following meanings.

-F script: The scripts or commands that you want to commit.

-L: Lists all currently running jobs that have the same effect as the ATQ command.

-R: Clears a job and provides the corresponding job ID (ID).

-M: sends a message to the user when the job is completed.

The time format of the Time:at command.

Date: The datetime format can be a month or a number of dates.

The time parameter can be any of the following formats.

HH:MM format, for example, 04:00 represents 4:00am. If the time is past, it will be executed at the next day.

Midnight: Represents 12:00am.

Noon: Represents 12:00pm.

Teatime: Represents 4:00pm.

English month name Date year format: for example, January 15 2002 represents January 15, 2002, the year is optional.

Mmddyy,mm/dd/yy or MM.DD.YY format: For example, 011502 represents January 15, 2002.

now+ time: The time in minutes,hours,days or weeks, for example, "Now+5 days" on behalf of the command in 5 days after the moment of execution.

After you enter the AT command and its time parameters, the "at>" prompt appears. Enter the command you want to execute, press the ENTER key, and then press the [Ctrl+d] key. Here you can specify more than one command by typing each command and then pressing the "enter" key. After entering all the commands, press the "enter" key to go to a blank line, and then enter [Ctrl+d].

Example: Copy the/root/a file to the/home directory at this point in 5 minutes.

[Email protected] ~]# at now +5 minutes

At>./a.sh

You can also enter a shell script at the prompt, press the "enter" key after each line of the script, and then press the [Ctrl+d] key at the blank line to exit.

3. Using the AT command to submit a command or script

There are several different forms of submitting jobs using the AT command, either by command line or at command prompt. In general, when you submit a system command for several rows, use the AT command prompt, and when you submit a shell script, use the command line method.

If you want to submit a command for several lines, you can follow the date/time followed by the AT command and enter. Then you enter the AT command prompt, just enter the appropriate command one by one, and then press [Ctrl+d] key to exit.

If you want to submit a shell script to the AT command, use its command-line method. Use the "-f" option when committing the script.

Example: Using the echo command to submit a job to the AT command

[Email protected] ~]# echo hello>/root/a |at now

Job 5 at 2015-01-25 05:59

You have new mail in/var/spool/mail/root

[[email protected] ~]# ls

A Downloads root

Anaconda-ks.cfg Install.log sqldeveloper-4.0.1.14.48-1.noarch.rpm

a.sh Install.log.syslog Templates

?           [D??? Music Videos

Desktop Pictures Yangtab

Documents Public

4. List the jobs submitted

After a job is committed, you can use the "at-l" command to list all the jobs.

Example: Use "at-l" to list all jobs.

[Email protected] ~]# at-l

6 2015-01-25 06:13 a root

[Email protected] ~]#

Where the 1th column is the job ID, followed by the date/time that the job was run, and a represents the at,root that represents the user executing the at job.

You can also use the ATQ command to accomplish the same function, which is a link to the AT command. When a job is submitted, it is copied to the/var/spool/at directory, ready to run at the expected time.

[Email protected] ~]# at now +5 minutes

at> Echo Beijing Welcome you! >/root/a

At> <EOT>

Job 7 at 2015-01-25 06:20

[Email protected] ~]# ATQ

7 2015-01-25 06:20 a root

[Email protected] ~]# ls-l/var/spool/at

Total 8

-rwx------1 root root 2487 Jan 06:15 A000070169aedc

DRWX------. 2 Daemon daemon 4096 Jan 06:13 spool

[Email protected] ~]#

You can see that the job is copied to the/var/spool/at directory

5. Clear at Job

To clear a job, first execute the "at-l" command to view the job identity, and then clear the job.

Command syntax:

ATRM [Job representation number]

at-r [Job representation number]

Example: Use the command ATRM to delete a job.

[Email protected] ~]# at now +5 minutes

at> RM A.SH-RF

At> <EOT>

Job 8 at 2015-01-25 06:26

[Email protected] ~]# at-l

8 2015-01-25 06:26 a root

You can see that there is a job with an identification number of 8.

[Email protected] ~]# ATRM 8

[Email protected] ~]# at-l

[Email protected] ~]#

You can see that the job has been deleted.

Linux Task Scheduler

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.