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 configure scheduled task cron for linux

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

Share

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

This article is about how linux configures cron for scheduled tasks. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The implementation of linux timing tasks include: cron, anacron, at, and cron tasks are most commonly used.

1. Noun interpretation

Cron-- service name; a daemon used under crond--linux to periodically execute certain tasks or wait for certain events to be processed, similar to scheduled tasks under windows; crontab-- is a customized schedule of scheduled tasks.

two。 Software package installation

To use the cron service, you first install the vixie-cron package and the crontabs package, which serve the following purposes:

The vixie-cron package is the main program of cron. The crontabs package is a program that installs, uninstalls, or enumerates tables used to drive cron daemons.

Check whether the cron package is installed: rpm-qa | grep vixie-cron

Check whether the crontabs package is installed: rpm-qa | grep crontabs

If it is not installed, execute the following command to install the package (the package must exist)

Rpm-ivh vixie-cron-4.1-54.FC5

Rpm-ivh crontabs

If the package is not installed locally, it can be installed online if it can be connected to the network.

Yum install vixie-cron

Yum install crontabs

3. Check whether the crond service is running

Pgrep crond or / sbin/service crond status or ps-elf | grep crond | grep-v "grep"

4.crond service operation command

/ sbin/service crond start// start the service

/ sbin/service crond stop// shuts down service

/ sbin/service crond restart// restart the service

/ sbin/service crond reload// reload configuration

5. Configure scheduled tasks

Cron has two configuration files, one is a global configuration file (/ etc/crontab) for system tasks, and the other is a set of configuration files generated by the crontab command (files under / var/spool/cron) for a user. Scheduled tasks can be configured to any one of them.

View global profile configuration: cat/etc/crontab

-

SHELL=/bin/bash

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

MAILTO=root

HOME=/

# run-parts

01 root run-parts/etc/cron.hourly

02 4 root run-parts/etc/cron.daily

22 4 0 root run-parts/etc/cron.weekly

42 4 1*root run-parts/etc/cron.monthly

-

View scheduled tasks under the user: crontab-l or cat/var/spool/cron/ username

Basic format of 6.crontab task configuration

Command

Minute (0-59) hour (0-23) date (1-31) month (1-12) week (0-6 for Sunday) order

The first column represents minutes 1: 59 per minute or / 1

The second column represents the hour 1: 23 (0 means 0 o'clock)

The third column represents the date 1: 31

The fourth column represents the month from January to December.

The fifth column identification number is 0006 (0 means Sunday)

Column 6 commands to run

An asterisk () can be used to represent all valid values in any of the above values. For example, the asterisk in the month value means that the command is executed every month after other constraints are met.

The short line (-) between integers specifies a range of integers. For example, 1-4 means integers 1, 2, 3, 4.

Specify a list with a series of values separated by commas (,). For example, 3, 4, 6, 8, indicates the four specified integers.

The forward slash (/) can be used to specify the interval frequency. Adding / after the range means that integer can be skipped within the range. For example, 0-59 impulse 2 can be used to define every two minutes in the minutes field. The interval frequency value can also be used with an asterisk. For example, the value of / 3 can be used in the month field to indicate that the task is run every three months.

Lines that begin with a pound sign (#) are comments and will not be processed

In the crontab file, each line represents a task, and each field on each line represents a setting. Its format is divided into six fields. The first five paragraphs are the time setting section, and the sixth paragraph is the command segment to be executed. Each field is separated by spaces, and useless segments are replaced by the following format:

M h dom mon dow user command

Where:

M: represents minutes and can be any integer from 0 to 59.

H: represents an hour and can be any integer from 0 to 23.

Dom: represents the date and can be any integer from 1 to 31.

Mon: represents the month and can be any integer from 1 to 12.

Dow: represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

User: represents the user who executes.

7. Use an example

Example 1: execute command every 1 minute

Command: command

Example 2: execution at the 3rd and 15th minutes of each hour

Command: 3 command 15 min

Example 3: executed at the 3rd and 15th minutes from 8 a.m. to 11:00

Command: 3Jing 15 8-11 command

Example 4: execution of the 3rd and 15th minutes from 8am to 11:00 every two days

Command: 3meme 15 8-11 command 2

Example 5: execution of the 3rd and 15th minutes from 8am to 11:00 every Monday

Command: 3Jing 15 8-11 1 command

Example 6: restart smb at 21:30 every night

Command: 30 21/etc/init.d/smb restart

Example 7: restart smb at 4:45 on the 1st, 10th and 22nd of each month

Command: 45, 4, 1, 10, 10, 22, ETC, restart, init.dUnix SMB.

Example 8: restart smb at 1:10 every Saturday and Sunday

Command: 10-16-10-10-10-5-10-5-5-5-2-2-5-2-2-4-2-2-3-2-3-2-3-2-3-1-2-1-2-4-4-2-4-4-2-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-2-4-4-4-4-2-3-2-4-3-2-3-2-3-2-3-2-4-4-3-2-3-2-3-2-3-2-3-2-3-2-3-2-3-1-2-1-2-3-D-D-

Example 9: restart smb every 30 minutes between 18:00 and 23:00 every day

Command: 0Pol 30 18-23/etc/init.d/smb restart

Example 10: pm restarts smb at 11:00 every Saturday night

Command: 0 23 6/etc/init.d/smb restart

Example 11: restart smb every hour

Command: / 1/etc/init.d/smb restart

Example 12: restart smb every other hour between 11:00 and 7am

Command: 23-7/1/etc/init.d/smb restart

Example 13: restart smb on the 4th of each month and 11:00 from Monday to Wednesday

Command: 0 11 4 mon-wed/etc/init.d/smb restart

Example 14: restart smb at 4: 00 on January 1

Command: 0 4 1 jan/etc/init.d/smb restart

Example 15: hourly execution of scripts in the / etc/cron.hourly directory

Command: 01 root run-parts/etc/cron.hourly

Description:

Run-parts this parameter, if you remove this parameter, you can write the name of a script to run instead of the directory name

Thank you for reading! This is the end of this article on "how to configure scheduled tasks cron in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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