SoFunction
Updated on 2025-04-21

Detailed explanation of Linux's planning tasks and dispatch commands at/cron

Linux planning tasks and dispatch commands at/cron

1. Plan tasks

The process is generated after the program is executed. The execution of the program can be achieved in one of two ways

  1. Manual execution
  2. Use scheduling tasks to be automatically executed according to certain conditions. Just like planning tasks in windows

If you plan to let the program execute automatically, you can use the following command to implement it:

  • at
  • cron

2. Introduction to command {at}

The service is responsible and managed by the atd service. The daemon belonging to at. You can view, start and close atd through the following commands.

#systemctl stop atd
#systemctl start atd
#systemctl restart atd
#systemctl status atd

at, this is a one-time planned task. The execution of at mainly relies on the system service atd. There are 2 files in the system

  • /etc/
  • /etc/

These two files specify who can use at the system and who cannot use it. Super users can use at any time. Other users are subject to the above two files.

The summary is as follows:

  • If both files exist, only check /etc/, and only users specified in /etc/ can use it.
  • If only /etc/ exists, every user that does not exist in the file can use it.
  • If the two files do not exist, only the super user can use it.

3. Command syntax and function: at

  • Syntax: at [Options] [time]
  • Function: Execute the specified process based on the predefined time

1) at acceptable time format

1. Date

  • MM/DD/YY
  • MMDDYY

2. Time

  • HH:MM
  • HH:MMam
  • HH:MMpm

3. Special date

midnight(0am)
noon(0pm)
teatime(4pm)
now( Now )
now + N(minutes,hours,days,weeks)
eg1:
at 23:00
at>reboot
at><EOT> ← according to ^D Get this symbol , Meaning end
eg2:
at now
at>ls
at><EOT>

2) Time input specification:

  1. Enter only the time. If you do not enter the date, it will be considered at some point today.
  2. If you only enter the date and do not enter the time, it is considered that the current time when the date is entered.
  3. Supports entering a full time.

at time format

eg:

#at 23:00
#at 12/31/13
#at 23:00 12/31/13
#at noon
#at 0:00pm 12/31/13
#at teatime + 30 minutes (30 minutes after 4pm)#at 4:00pm + 4 days (4 days from 4pm)#at now + 4 weeks(From now on4week)

3) at queue management

at After editing, it will be entered into the queue. The task is scanned by the atd service and waits for the task to be executed

1/Click at the queue to use the following command

  • Command:atq
  • Function: View at queue
  • Structural syntax: atq [Options]

at After editing, if you plan to view the information of a task queue and the commands you are preparing to execute, you can execute it as follows:

#at -c atqueueID

2/at After editing, the queue waits for execution. If you cancel the scheduled at task, you can use atrm to complete the cancellation action.

  • Command:atrm
  • Function: Delete the specified task in the at queue
  • Structural Syntax:atrm [Option] [Quote ID]

4. Introduction to command {cron}

The biggest difference between cron and at is that cron is to perform predetermined tasks according to the specified cycle. The daemon of cron is crond.

#systemctl stop crond
#systemctl start crond
#systemctl restart crond
#systemctl status crond

5. Cron syntax and functions

  • Command:crontab
  • Function: Implement the execution of periodic planning tasks
  • Syntax format: crontab [Options]

Options:

  • -l View the cron scheduled task list.
  • -e Edit the cron scheduled task list as the default text editor.
  • -r Delete the cron scheduled task list.
  • -u Specify the user. If the user is not specified, -l, -e, -r is considered to be an operation on the current user.

(1) Cron editing format:

1) Shell and mail format definition

SHELL=/bin/bash

SHELL Specifies which shell to handle the scheduled task.

MAILTO=root

MAILTO specifies that if you run the scheduled task or if you encounter obstacles, you will send an email to a user and give a prompt.

2) Time format definition

  • Minutes 0-59
  • Hours 0-23
  • What day of the month 1-31
  • Monthly 1-12
  • Week 0-7

//* Mid-day 0 and 7 also mean Sunday

  • "*" in the first column can represent every minute.
  • "*" in the second column can represent every hour.
  • "*" in the third column can represent every day of the month.
  • "*" in the fourth column can represent every month of the year.
  • "*" in the fifth column can represent every day of the week.

3) Special time format definition

Continuous situations, taking the hour as an example

1,2,3,4,5,8,9,10 You can write 1-5,8-10 like this.

Weekly interval

If it is written every 5 minutes in the first column:

*/5

4) cron format

* * * * * The commands and paths required to execute

eg:

0 0 1 1 * echo “Happy New Year”

Example

0 */2 13 * 5 /usr/bin/wall be careful,dark friday!

Represents Black Friday, prompting users every 2 hours.

0 20 * * 1-5 /sbin/shutdown -h now

From 1 to 5 weeks, the power will be automatically turned off at 8 pm every day.

(2) Cron writing tasks

1. Create cron scheduled tasks with current account

#crontab -e

2. Create their cron scheduled tasks with specified users (root only)

#crontab -e -u snow

3. Requirements for using specified files as planned tasks

#vim newcron
*/5 * * * * wall hello
#crontab newcron
or
#crontab -u snow newcron

(3) Check the cron task queue

1) View the cron queue of the currently used account

#crontab -l

2) Check the cron queue of other accounts (only root can view the cron queue of other users)

#crontab -l -u snow

(4) Clear the cron task queue

1) Clear the cron queue of the currently used account

#crontab -r

2 Clear the cron queue of other accounts (only root can view the cron queue of other users)

#crontab -r -u snow

(5) Related configuration files

/etc/crontab
System plan task list and description。
/etc/
Specific system schedule task list
/etc/
Scheduled tasks to run every hour of the system。
/etc/
The scheduled tasks that the system needs to run every day。
/etc/
The scheduled tasks to run every week of the system。
/etc/
The scheduled tasks to run every month of the system。

Although all the /etc/ and other directories are shell scripts, in the /etc//0hourly file, it is stipulated that the run-parts command is used to execute the specified directory script file. This enables the tasks in these above-mentioned shell script directories to be executed.

Use run-parts script to execute

/etc/
/etc/
/etc/
/etc/

Scripts in the above directory, these scripts are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.

run-parts is a script, in /usr/bin/run-parts, which is used to execute all scripts/programs in a directory.

6. Anacron

Cron's scheduled task execution must keep the computer on. If the power is shut down or not executed at the specified time, you will have to wait for the next cycle. Anacron is different. If it is not successfully executed at the specified time, it will be executed again within a period of time at the specified retry time.

anacron configuration file

#vim /etc/anacrontab

Format

  • Time interval (default is days, @monthly is executed within 1 month)
  • Waiting interval (default is minutes)
  • Task identification

Order

illustrate:

  • Time interval: The time interval for performing tasks
  • Waiting interval: After the time interval expires, the task is not executed, so try to execute again using the time set here
  • Task ID: Task Description
  • Command: The actual task executed
period(frequency.Days)
delay(Delay,minute)
job-identifier command
  • The first line means: check whether the file has been executed 5 minutes after booting every day. If it is not executed today, execute it.
  • The second line means: check whether the file has been executed 25 minutes after turning on the computer every 7 days. If it is not executed within a week, execute it.
  • The third line means: check whether the file has been executed 45 minutes after booting every month. If it is not executed today, execute it.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.