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

The solution of html2canvas that pictures cannot be captured normally

question First, let me talk about the problem I e...

How to change the MySQL database directory location under Linux (CentOS) system

How to change the MySQL database directory locati...

MySQL 8.0.12 winx64 detailed installation tutorial

This article shares the installation tutorial of ...

Detailed explanation of the use of props in React's three major attributes

Table of contents Class Component Functional Comp...

Deeply understand how nginx achieves high performance and scalability

The overall architecture of NGINX is characterize...

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

Detailed explanation of MySQL group sorting to find the top N

MySQL group sorting to find the top N Table Struc...

Nginx implements https website configuration code example

https base port 443. It is used for something cal...

Example code for converting html table data to Json format

The javascript function for converting <table&g...

Details on using JS array methods some, every and find

Table of contents 1. some 2. every 3. find 1. som...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...