Scheduled automatic execution function (at, crontab) Linux of

Scheduled automatic execution function (at, crontab) Linux of

concept

In the Linux system, it provides two ways to advance the work arrangements

  • at only performed once

  • crontab repeated periodically

By applying these two tools allows us to achieve timing auto-run feature in the operating system level

lab environment

操作系统:CentOS Linux release 7.3.1611 (Core)

IP: 192.168.230.134

at introduction

at command, and will be executed once

This feature uses the atd tool, you need to ensure that the active state

[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-09-07 18:18:18 CST; 4h 2min ago
 Main PID: 921 (atd)
   CGroup: /system.slice/atd.service
           └─921 /usr/sbin/atd -f

Sep 07 18:18:18 localhost.localdomain systemd[1]: Started Job spooling tools.
Sep 07 18:18:18 localhost.localdomain systemd[1]: Starting Job spooling tools...

at use

Common commands are as follows

[root@localhost ~]# at -help
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
 atrm [-V] job ...  batch

Time format:

format Examples significance
HH:MM 01:01 The next execution 1:01
HH:MM YYYY-MM-DD 01:00 2017-09-08 Specify a specific time execution date
HH:MM[am/pm]+number[minutes/hours/days/weeks] now+3 days After a certain point in time to put in some time

Examples at

Create a test sh, and modify permissions

[root@localhost ~]# vim test.sh

echo '豆姐威武'

[root@localhost ~]# chmod 755 test.sh [root@localhost ~]# ./test.sh 豆姐威武

View the current time

[root@localhost ~]# date
Thu Sep  7 23:19:51 CST 2017

Use the at command, set 23:21 test.sh execute commands attention with Ctrl + d end

[root@localhost ~]# at 23:21
at> /root/test.sh
at> <EOT>
job 5 at Thu Sep  7 23:21:00 2017

After waiting a minute, receive information system to view the time and the set time

You have new mail in /var/spool/mail/root
[root@localhost ~]# date
Thu Sep  7 23:21:04 CST 2017

View information received by the system

[root@localhost ~]# cat /var/spool/mail/root

From root@localhost.localdomain  Thu Sep  7 23:21:00 2017 Return-Path: <root@localhost.localdomain> X-Original-To: root Delivered-To: root@localhost.localdomain Received: by localhost.localdomain (Postfix, from userid 0) id A139B243D2; Thu, 7 Sep 2017 23:21:00 +0800 (CST) Subject: Output from your job 5 To: root@localhost.localdomain Message-Id: <20170907152100.A139B243D2@localhost.localdomain> Date: Thu, 7 Sep 2017 23:21:00 +0800 (CST) From: root@localhost.localdomain (root) 豆姐威武

A more practical copy of the script: 
time off

[root@localhost ~]# at 18:00 2017-09-10
at> /bin/sync
at> /bin/sync
at> /sbin/shutdown -h now
at> <EOT> job 6 at Sun Sep 10 18:00:00 2017

It will shut down automatically at 2017/09/07 18:00

at cancel command

View the current at a command 
using the command atq

[root@localhost ~]# atq
6   Sun Sep 10 18:00:00 2017 a root

Cancel command 
using the command atrm

[root@localhost ~]# atrm 6

crontab Introduction

crontab command performed periodically according to the configuration

His record will be stored in / var / log / cron in

This feature requires crond service, the need to ensure its normal state

[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-09-07 18:18:18 CST; 5h 51min ago Main PID: 920 (crond) CGroup: /system.slice/crond.service └─920 /usr/sbin/crond -n Sep 07 18:18:18 localhost.localdomain systemd[1]: Started Command Scheduler. Sep 07 18:18:18 localhost.localdomain systemd[1]: Starting Command Scheduler... Sep 07 18:18:18 localhost.localdomain crond[920]: (CRON) INFO (RANDOM_DELAY will be ...) Sep 07 18:18:19 localhost.localdomain crond[920]: (CRON) INFO (running with inotify ...) Hint: Some lines were ellipsized, use -l to show in full. 

use crontab

Common commands are as follows:

[root@localhost ~]# crontab -help
crontab: invalid option -- 'h'
crontab: usage error: unrecognized option
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab -r delete user's crontab -i prompt before deleting -n <host> set host in cluster to run users' crontabs -c get host in cluster to run users' crontabs -s selinux context -x <mask> enable debugging

Time format:

*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7) | | | +---------- month (1 - 12) | | +--------------- day of month (1 - 31) | +-------------------- hour (0 - 23) +------------------------- min (0 - 59)

crontab examples

Or use test.sh created before doing the experiment

New plan to use crontab command

To view the current time

[root@localhost ~]# date
Thu Sep  7 23:52:12 CST 2017

Create a plan, set to 23:55 every day script execution /root/test.sh

[root@localhost ~]# crontab -e

55 23 * * * /root/test.sh

no crontab for root - using an empty one crontab: installing new crontab

View the current schedule:

[root@localhost ~]# crontab -l
55 23 * * * /root/test.sh

Wait two minutes:

[root@localhost ~]# date
Thu Sep  7 23:55:07 CST 2017 You have new mail in /var/spool/mail/root

View record:

[root@localhost ~]# cat /var/spool/mail/root 
From root@localhost.localdomain  Thu Sep  7 23:55:01 2017 Return-Path: <root@localhost.localdomain> X-Original-To: root Delivered-To: root@localhost.localdomain Received: by localhost.localdomain (Postfix, from userid 0) id 45D1028EB7; Thu, 7 Sep 2017 23:55:01 +0800 (CST) From: "(Cron Daemon)" <root@localhost.localdomain> To: root@localhost.localdomain Subject: Cron <root@localhost> /root/test.sh Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated Precedence: bulk X-Cron-Env: <XDG_SESSION_ID=52> X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0> X-Cron-Env: <LANG=en_US.UTF-8> X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/root> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=root> X-Cron-Env: <USER=root> Message-Id: <20170907155501.45D1028EB7@localhost.localdomain> Date: Thu, 7 Sep 2017 23:55:01 +0800 (CST) 豆姐威武

Removing crontab program

If you need to remove one or two programs, the direct use crontab -e to enter the edit delete 
If you want to empty the plan, -r can

[root@localhost ~]# crontab -l
55 23 * * * /root/test.sh [root@localhost ~]# crontab -r [root@localhost ~]# crontab -l no crontab for root

crontab supplement

You can also edit / etc / crontab file to achieve planned script

[root@localhost ~]# vim /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,fri,sat # | | | | | # * * * * * user-name command to be executed

But sometimes this way does not take effect immediately, you need to restart the crond service

Guess you like

Origin www.cnblogs.com/telwanggs/p/11547398.html