Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use timing execution tools under linux cron

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the use of timing execution tools under linux cron". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the use of timing execution tools under linux cron"!

Cron is a timed execution tool under linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, but it does not start automatically, you can start and shut down the service in the following ways:

/ sbin/service crond start / / start the service

/ sbin/service crond stop / / shut down the service

/ sbin/service crond restart / / restart the service

/ sbin/service crond reload / / reload the configuration

You can also start this service automatically when the system starts:

At the end of the script / etc/rc.d/rc.local, add:

/ sbin/service crond start

Now that the Cron service is in the process, we can use this service. The Cron service provides the following interfaces for everyone to use:

1. Edit directly with the crontab command

The cron service provides a crontab command to set the cron service. Here are some parameters and instructions for this command:

Crontab-u / / sets a user's cron service. Generally, root users need this parameter when executing this command.

Crontab-l / / lists the details of a user's cron service

Crontab-r / / Delete cron services without users

Crontab-e / / Edit a user's cron service

For example, root checks its cron settings: crontab-u root-l

For example, root wants to delete the cron setting of fred: crontab-u fred-r

When editing a cron service, there are some formats and conventions for editing. Enter: crontab-u root-e

When you enter vi editing mode, the edited content must conform to the following format: * / 1 * ls > > / tmp/ls.txt

The first part of this format is the setting of time, and the second part is the commands to be executed. if there are too many commands to be executed, you can write these commands into a script, and then call the script directly here. Remember to write the full path of the command when you call it. We have a certain agreement on the setting of time. The first five * signs represent five digits. The range and meaning of the numbers are as follows:

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

The week (0-6) / / 0 represents Sunday

In addition to numbers, there are several special symbols, namely "*", "/" and "-", * represents all the numbers in the range of values, "/" represents each, "* / 5" means every five units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples:

6: 00 every morning

0 6 * echo "Good morning." > / tmp/test.txt / / Note simple echo, no output can be seen on the screen, because cron email any output to the mailbox of root.

Every two hours

0 * / 2 * echo "Have a break now." > > / tmp/test.txt

Every two hours between 11:00 and 8 a.m., 8 a.m.

0 23-7 echo 8 * echo "Have a good dream:)" > > / tmp/test.txt

11:00 on the 4th of every month and from Monday to Wednesday

0 11 4 * 1-3 command line

January first at four o'clock in the morning

0 4 1 1 * command line

Each time you edit a user's cron settings, cron automatically generates a file with the same name as the user under / var/cron. The user's cron information is recorded in this file. This file cannot be edited directly, but can only be edited with crontab-e. Read this file every time you start cron to check if you want to execute the commands in it. Therefore, there is no need to restart the cron service after this file is modified.

two。 Edit / etc/crontab file to configure cron

The cron service not only reads all the files in / var/spool/cron once per minute, but also reads / etc/crontab once, so we can configure this file to do something with the cron service. Configuration with crontab is for a user, while editing / etc/crontab is a system-specific task. The file format of this file is:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root / / if there is an error, or if there is data output, the data will be sent to this account by email

The path where the HOME=/ user runs. Here is the root directory.

# run-parts

01 * root run-parts / etc/cron.hourly / / execute scripts in / etc/cron.hourly every hour

02 4 * root run-parts / etc/cron.daily / / execute scripts in / etc/cron.daily every day

22 4 * * 0 root run-parts / etc/cron.weekly / / execute scripts in / etc/cron.weekly weekly

42 4 1 * * root run-parts / etc/cron.monthly / / execute the scripts in / etc/cron.monthly every month

Note the parameter "run-parts". If you remove this parameter, you can write the name of a script to run instead of the folder name.

At this point, I believe you have a deeper understanding of "the use of timing execution tools under linux cron". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report