How to create scheduled tasks using crond tool in Linux

How to create scheduled tasks using crond tool in Linux

Preface

  • Crond is a scheduled execution tool under Linux (equivalent to the scheduled task under Windows), which can run tasks on a regular basis without human intervention. The crond tool provides the crontab command to set scheduled tasks. It is a daemon process that can only be accurate to the minute. You can set it to execute Linux commands or Shell scripts periodically. Every minute, crond will check whether there is a scheduled task to be executed.
  • The experimental system is CentOS 7

Procedure

(1) Check whether the crond tool is installed

yum list installed | grep crontabs

Check whether the crond tool is installed

If it is not installed, install it using the following command:

sudo yum install crontabs

(2) Check whether the crond service is enabled . Since it is CentOS7, use the systemctl command instead of the service command.

systemctl status crond.service

Check whether the crond service is enabled

If it is not enabled, use the following command to enable the service:

sudo systemctl start crond.service

(3) Use the crond tool to create a task schedule crontab command usage

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
 # Note that crontab -r deletes all scheduled tasks of the user (use with caution!)

You can view the task definition format and set the task execution environment through the /etc/crontab file


Task definition format

Take "Write the date into a specified file every minute" as an example

Method 1: Use the crontab command to edit the current user's scheduled task (effective immediately)**

crontab -e

Insert the following command in the editor (note that you should not add users at this time, otherwise it will not be executed, because this method directly sets the scheduled task for the current user)

*/1 * * * * date >> /home/TomAndersen/currentDate

Check the insertion results

[tomandersen@hadoop101 bin]$ crontab -l
*/1 * * * * date >> /home/TomAndersen/currentDate

Method 2: Edit the /etc/crontab file and insert it according to the format (slower to take effect)**

*/1 * * * * tomandersen date >> /home/TomAndersen/currentDate

(4) Check whether the setting is successful

[tomandersen@hadoop101 bin]$ cat /home/TomAndersen/currentDate 
Sunday, February 9, 2020 18:12:01 CST
Sunday, February 9, 2020 18:13:01 CST
Sunday, February 9, 2020 18:14:01 CST
Sunday, February 9, 2020 18:15:01 CST
Sunday, February 9, 2020 18:16:02 CST
Sunday, February 9, 2020 18:17:01 CST
Sunday, February 9, 2020 18:18:01 CST
Sunday, February 9, 2020 18:19:01 CST
Sunday, February 9, 2020 18:20:01 CST

Summarize

The above is the editor's introduction to using the crond tool to create scheduled tasks in Linux. I hope it will be helpful to everyone!

You may also be interested in:
  • A brief discussion on the usage of crond and crontab commands under Linux
  • Detailed explanation of Linux scheduled tasks Crond
  • How to use the crond command for scheduled tasks in Linux
  • How to use the crond command in Linux VPS scheduled tasks
  • How to set up scheduled tasks in Linux
  • How to use crontab to execute a scheduled task once a second in Linux
  • Detailed explanation of Python script self-starting and scheduled tasks under Linux

<<:  Summary of three rules for React state management

>>:  Method of using MySQL system database for performance load diagnosis

Recommend

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

The reason why MySQL uses B+ tree as its underlying data structure

We all know that the underlying data structure of...

Vue+Element realizes paging effect

This article example shares the specific code of ...

How to delete the container created in Docker

How to delete the container created in Docker 1. ...

How to build svn server in linux

1: Install SVN yum install -y subversion 2. Creat...

Common array operations in JavaScript

Table of contents 1. concat() 2. join() 3. push()...

Media query combined with rem layout in CSS3 to adapt to mobile screens

CSS3 syntax: (1rem = 100px for a 750px design) @m...

Detailed explanation of js closure and garbage collection mechanism examples

Table of contents Preface text 1. Closure 1.1 Wha...

Tutorial on installing MySQL 5.7.18 decompressed version on Windows

1. Installation process MySQL version: 5.7.18 1. ...

Detailed tutorial on installing mysql 8.0.13 (rpm) on Centos7

yum or rpm? The yum installation method is very c...

Element Plus implements Affix

Table of contents 1. Component Introduction 2. So...

Summary of two methods to implement vue printing function

Method 1: Install the plugin via npm 1. Install n...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...