Summary of some related operations of Linux scheduled tasks

Summary of some related operations of Linux scheduled tasks

I have searched various major websites and tested the operation of related scheduled tasks to facilitate everyone's reference and operation.

1. Introduction to cron

The crontab command we often use is the abbreviation of cron table. It is the configuration file of cron, and it can also be called job list. We can find the relevant configuration files in the following folders.

1.1. Cron related directories

  • The /var/spool/cron/ directory stores crontab tasks for each user, including root. Each task is named after the creator.
  • /etc/crontab This file is responsible for scheduling various management and maintenance tasks.
  • /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
  • We can also put the script in the /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories to have it executed every hour, day, week, or month.

1.2. Common commands of crontab

  • crontab [-u username] //Omitting the user table means operating the crontab of the current user
  • crontab [-u username] -e //Edit worksheet)
  • crontab [-u username] -l //List the commands in the worksheet)
  • crontab [-u username] -r //Delete job)

1.3. Writing specifications

1. The parameters of each position are as shown below, and there are also notes for details.

Remark:

1) * means that any time (minute, hour, day, month, week) will be executed

2) - Indicates a time range, such as 5-7 o'clock

3) , represents the time interval, such as 6,0,4 represents Saturday, Sunday, and Thursday.

4) /1 means every n time units, such as */10 every 10 minutes

2. Set up scheduled tasks

2.1. Globally set up scheduled tasks crontab --- when the task is to link, curl or write files

(1) Execute the command crontab -e

(2) Write a scheduled task

If the scheduled task is to link

*/1 * * * * /usr/local/curl (your own curl path) www.baidu.com >/dev/null 2>$1

If you need to write the content to the file

*/1 * * * * echo "hello" >> abc.log

I would also like to share a few points

  • Standard input 0 gets input from the keyboard /proc/self/fd/0
  • Standard output 1 is output to the screen (i.e. console) /proc/self/fd/1
  • Error output 2 is output to the screen (ie console) /proc/self/fd/2
  • /dev/null represents the empty device file of Linux. All the contents written to this file will be lost, commonly known as the "black hole"
  • >/dev/null means to output errors to the "black hole"
  • >/dev/null 2>&1 The default is 1, which is equivalent to 1>/dev/null 2>&1. This means redirecting the standard output to the "black hole" and redirecting the error output 2 to the standard output 1, that is, both the standard output and the error output go into the "black hole".
  • 2>&1 >/dev/null means redirecting error output 2 to standard output 1, that is, the screen, and the standard output goes into a "black hole", that is, the standard output goes into a black hole, and the error output is printed to the screen
  • Regarding the role of "&" here, we can understand it this way: 2>/dev/null redirects to a file, then 2>&1, if & is removed here, the error output is sent to file 1, and using & indicates that 1 is the standard output.

(3) Save scheduled tasks

  1. Press i to insert the task and write the scheduled task
  2. Press Esc to exit and enter :wq , then press Ctrl + C to save successfully.

2.2. .sh method implementation

(1) Create a Shell Script

  • Create a file with the suffix .sh in the project
  • Add sufficient permissions to this shell file in this directory
chmod -R 777 you create the file name

(2) Write the Shell script into the scheduled task

Write the corresponding shell file into the scheduled task

*/1 * * * * The absolute path of your .sh file>/dev/null 2>$1

Write the corresponding shell file to the log when executing the scheduled task

*/1 * * * * The absolute path of your .sh file >> a.log >/dev/null 2>$1

(3) Restart crond

service crond restart

(4) Check whether it is written into the project

crontab -l

Thank you for watching, if you have any questions please leave a message in the comment section.

Summarize

This is the end of this article about some related operations of Linux scheduled tasks. For more related Linux scheduled tasks, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use cron to execute tasks regularly in Linux
  • Linux crontab scheduled task configuration method (detailed explanation)
  • Linux uses crontab to implement PHP execution plan timing tasks
  • How to use crontab to execute a scheduled task once a second in Linux
  • The server regularly executes scheduled tasks and regularly accesses pages (windows/linux)
  • How to set up scheduled tasks in Linux
  • Detailed explanation of Python script self-starting and scheduled tasks under Linux
  • How to execute tasks regularly under Linux and instructions on how to use crontab (collected and sorted)
  • Linux uses scheduled tasks to clean up logs 45 days ago every week
  • Detailed explanation of how to use PHP to schedule cron tasks in Linux

<<:  JS realizes the automatic playback effect of pictures

>>:  Sample code for implementing PC resolution adaptation in Vue

Recommend

How to use Webstorm and Chrome to debug Vue projects

Table of contents Preface 1. Create a new Vue pro...

Directory permissions when creating a container with Docker

When I was writing a project yesterday, I needed ...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...

Web Design Summary

<br />From the birth of my first personal pa...

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

HTML code that can make IE freeze

We simply need to open any text editor, copy the f...

Vue custom v-has instruction to implement button permission judgment

Application Scenario Taking the background manage...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...

HTML table tag tutorial (31): cell width and height attributes WIDTH, HEIGHT

By default, the width and height of the cell are ...

An article to understand what is MySQL Index Pushdown (ICP)

Table of contents 1. Introduction 2. Principle II...

Typescript+react to achieve simple drag and drop effects on mobile and PC

This article shares the specific code of typescri...