Timing Auto-Execution in Linux (at,crontab)

Keywords: crontab Linux vim shell

Timing Auto-Execution in Linux (at,crontab)

concept

In Linux systems, there are two ways to schedule work ahead of time

  • at executes only once

  • crontab repeats periodically

Through the application of these two tools, we can realize the function of timing and automatic operation at the operating system level.

Experimental environment

Operating System: CentOS Linux release 7.3.1611 (Core)

IP:  192.168.230.134

at introduction

The at command will and will be executed once

This feature will be used with the atd tool, which needs to be guaranteed to be in startup 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 Example Significance
HH:MM 01:01 Next 1:1
HH:MM YYYY-MM-DD 01:00 2017-09-08 Designate specific years and months for implementation
HH:MM[am/pm]+number[minutes/hours/days/weeks] now+3 days After a little more time

at example

Create test sh and modify permissions

[root@localhost ~]# vim test.sh

echo 'Bean sister'

[root@localhost ~]# chmod 755 test.sh 
[root@localhost ~]# ./test.sh 
//Bean sister

View the current time

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

Using the at command, set 23:21 to execute the test.sh command, and note that it ends with Ctrl+d

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

After sitting for a minute, I received the information from the system, checked the time and set the time.

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

View the 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)

//Bean sister

Copy a more practical script:
Regular shutdown

[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 18:00 in 2017/09/07

Cancellation of at command

View the current at command
Use the atq command

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

Cancel order
Use the atrm command

[root@localhost ~]# atrm 6

Introduction to crontab

The crontab command executes periodically according to configuration

His records will be kept in / var/log/cron

This function requires the use of crond services, which need to be guaranteed to be in good condition.

[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.

crontab uses

Common orders 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 example

Or experiment with test.sh created earlier

Use the crontab command to create a new plan

Look at the current time first

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

Create a plan to execute / root/test.sh scripts at 23:55 a day

[root@localhost ~]# crontab -e

55 23 * * * /root/test.sh

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

View the current plan:

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

After sitting and waiting for two minutes:

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

View records:

[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)

//Bean sister

Deletion of crontab plan

If you need to delete one or two plans, go directly to the edit page using crontab-e to delete
If you want to empty the plan, -r will do.

[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 directly edit / etc/crontab files to implement the plan 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 it doesn't take effect immediately, and you need to restart the crond service.

Posted by Plex on Sun, 26 May 2019 16:15:48 -0700