Under Linux delay at graphic tasks and the timing crontab command tasks

In the Linux system, there are two commands that can be used as a scheduled task to perform.

at: a one-time implementation of the plan scheduled tasks
crontab: Task Scheduler to perform regular daily

crontab command instruction set to be executed periodically. The command instructions read from the standard input device, and store it in the "crontab" file for read and executed later.

Under Linux delay at graphic tasks and the timing crontab command tasks

Let me talk about a one-time task execution plan (at)
you want to use one-time schedule, Linux must be responsible for this task service plan that atd service. But not all Linux distributions are opened by default, we need to manually activate the task atd

[linuxmi@linux:~/www.linuxmi.com]$ /etc/init.d/atd start
[linuxmi@linux:~/www.linuxmi.com]$ /etc/init.d/atd stop

If you start to worry about whether OK, then use the command to view:

[linuxmi@linux:~/www.linuxmi.com]$ ps aux |grep atd
daemon    31233  0.0  0.0  28328  2396 ?        Ss  03:04  0:00 /usr/sbin/atd -f
linuxmi  32404  0.0  0.0  21532  1052 pts/2    S+  03:18  0:00 grep --color=auto atd

Under Linux delay at graphic tasks and the timing crontab command tasks

If the system is not installed, then we need to manually install

[linuxmi@linux:~/www.linuxmi.com]$ yum install - y at
[linuxmi@linux:~/www.linuxmi.com]$ sudo apt-get install at

Under Linux delay at graphic tasks and the timing crontab command tasks

First, the system delay

It is temporary. The system made the task a specified point in time. Command is initiated at

at time point (now + 1min) ## implementation of tasks set time

at> ## tasks to perform command actions

at> <EOT> ## mission launched by ctrl + D

at commands commonly used parameters:

(1), AT # view a list of the -l

[linuxmi@linux:~/www.linuxmi.com]$ at 19:18
warning: commands will be executed using /bin/sh
at> rm -rf /home/linuxmi/linuxmi/*
at> <EOT>
job 2 at Tue Feb 11 19:18:00 2020
[linuxmi@linux:~/www.linuxmi.com]$ at -l
2 Tue Feb 11 19:18:00 2020 a linuxmi

Under Linux delay at graphic tasks and the timing crontab command tasks

(2), at -c job number # to view the contents of the task

[linuxmi@linux:~/www.linuxmi.com]$ at 19:19
warning: commands will be executed using /bin/sh
at> touch /home/linuxmi/linuxmi/linuxmi{1,3} 
at> <EOT>
job 3 at Tue Feb 11 19:19:00 2020
[linuxmi@linux:~/www.linuxmi.com]$ at -c 3

Under Linux delay at graphic tasks and the timing crontab command tasks

(3), at -r task number # Cancel the task execution

[linuxmi@linux:~/www.linuxmi.com]$ at -r 3

Note: The task has output, the output will be sent to the initiator at the task in the form of e-mail. Check Mail command in super user: mail (e-mail in / var / mail / linuxmi).

Use: mail -u linuxmi # linuxmi View user's mail

N by n represents the view of the closure of contents

Press q for quit

> / Var / spool / mail / linuxmi Empty Mail

(4), 15:07 days after execution /bin/linuxmi.sh
#: AT. 3 Days pm+7
AT> /bin/linuxmi.sh
AT> Press ctrl + D

(5), tomorrow 20, the output time to the specified file
#: AT 20:00 Tomorrow
AT> DATE> /tmp/2019.log
AT> Press ctrl + D

Second, set the delay task of black and white list

/etc/at.deny system exists by default, users in this file can not be executed at

/etc/at.allow system default does not exist; when there is, ordinary users can not execute at the command, and only the user can perform in the document

Blacklist in the root user: vim /etc/at.deny add at this time set linuxmi ## linuxmi user, not delay the implementation of tasks

White list under the root user: /etc/at.allow (the default file does not exist), if established out of the ordinary user can not execute at command; in addition to super-user, only the user in order to use at at.allow inside command.          

Note: The list appears white, black list will fail!

Third, the system timing task set

The command is permanent.

(1), crontab system control service

crond.service ## when the service is turned on, controls the timing of entry into force of the task (not to perform tasks point error time)

Start: sudo /etc/init.d/cron start
closed: sudo /etc/init.d/cron stop
reboot: sudo /etc/init.d/cron restart
reload the configuration: sudo /etc/init.d/cron reload
can use ps aux | grep cron command to see if cron has started

Under Linux delay at graphic tasks and the timing crontab command tasks

Fourth, the global configuration file

crontab in the / etc directory exists below cron.hourly, cron.daily, cron.weekly, cron.monthly, cron.d five directories and crontab, cron.deny two files

cron.daily is performed once a day job

cron.weekly the job is executed once each week

cron.monthly it is to perform monthly job

cron.hourly is performed once every hour job

cron.d is automatically on a regular basis to do the task

crontab file is set the timer task execution

cron.deny file is used to specify which users use Crontab function control to prevent

Fifth, the user profile

Each user has their own cron configuration file can be edited via crontab -e, under normal circumstances we edited the user's cron configuration file save and exit, the system will automatically will be stored in / var / spool / cron / directory, file with your name .linux the cron service once a minute to read all of the content below every /var/spool/cron,/etc/crontab,/etc/cron.d.

Six, crontab file format

  *           *        *        *        *           command

minute   hour    day   month   week      command

Minutes and days of the week command month

[linuxidc@localhost ~]$more /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 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,f
ri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
Under Linux delay at graphic tasks and the timing crontab command tasks

minute:  represents minutes, may be any integer from 0 to 59.

hour: the hour, may be any integer from 0 to 23.

day: the date, can be any integer from 1 to 31.

month: the month, can be any integer from 1-12.

week: represents the day of the week, it can be any integer from 0 to 7, where 0 or 7 represents Sunday.

the Command : command to be executed, the system can command, you can also write their own script files

Seven, special characters

An asterisk (*): On behalf of all possible values, such as month if the field is an asterisk, it means that the command is executed operation after a month constraints to meet other fields.

Comma (,): a comma-separated values ​​can be used to specify a list of ranges, e.g., "1,2,5,7,8,9."

The bar (-): can represent an integer in the range of a bar between the integer, e.g., "2-6" indicates "2,3,4,5,6."

Forward slash (/): n frequency intervals can slash specified time, for example, "0-23 / 2" is performed once every two hours. At the same time slash can be used with an asterisk, for example * / 10, if used in minute field representing performed once every ten minutes.

Eight, the use crontab -e add or edit tasks

For example, a system administrator to schedule a backup job so that it runs every day, or you can define a command regularly clean up junk files every day, how to add a cron job in?

# crontab –e

Crontab Examples

3, the first day of every month to run 5 22:11
11 22 5 * * /root/bin/full-rm.sh

Crontab Options

The following are valid options for the crontab:
crontab -e: Modify crontab file if the file does not exist will be created automatically.
crontab -l: Display a crontab file.
crontab -r: remove crontab files.
crontab -ir: crontab files before deleting alert the user.

Under Linux delay at graphic tasks and the timing crontab command tasks

crontab system Linux or Unix system is commonly used in regular order, you can use crontab execute a shell script or a series of Linux / Unix commands at a specified time.

Guess you like

Origin www.linuxidc.com/Linux/2020-02/162280.htm