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

A practical record of checking and processing duplicate MySQL records on site

Table of contents Preface analyze Data Total Repe...

When to use table and when to use CSS (experience sharing)

The main text page of TW used to have a width of 8...

Summary of the differences between Html, sHtml and XHtml

For example: <u> This has no ending characte...

How to deploy MongoDB container with Docker

Table of contents What is Docker deploy 1. Pull t...

Neon light effects implemented with pure CSS3

This is the effect to be achieved: You can see th...

Implementation methods of common CSS3 animations

1. What is CSS Animations is a proposed module fo...

Oracle VM VirtualBox installation of CentOS7 operating system tutorial diagram

Table of contents Installation Steps Environment ...

JavaScript Array Detailed Summary

Table of contents 1. Array Induction 1. Split a s...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...

Bootstrap FileInput implements image upload function

This article example shares the specific code of ...

JavaScript web page entry-level development detailed explanation

Part 3: ❤Three ways to overlook backend data rece...