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 linux crontab

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces "how to use linux crontab" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use linux crontab" article can help you solve your doubts.

Set up to execute my program every minute:

# crontab-e

* * / 1 * / home/lfzhou/vhost/vhost

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

/ sbin/service crond start / / start the service

Or

/ sbin/service crond reload / / reload the configuration

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 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 a user's cron service

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/spool/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.

2. 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.

-

Basic format:

* command

Time-sharing, day, month and week order

Column 1 represents minutes 1: 59 per minute is represented by * 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

Some examples of crontab files:

30 21 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 21:30 every night.

45-4-1-10-10-22 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 4: 45 on the 1st, 10th, and 22nd of each month.

10 1 * * 6 0 / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 1: 10 every Saturday and Sunday.

0pr 30 18-23 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted every 30 minutes between 18: 00 and 23: 00 every day.

0 23 * * 6 / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 11: 00 pm every Saturday.

* * / 1 * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd every hour

* 23-7bat 1 * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd every other hour between 11:00 and 7 a.m.

0 11 4 * mon-wed / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd on the 4th of each month and 11:00 from Monday to Wednesday

0 4 1 jan * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd at 4: 00 on January 1st

After reading this, the article "how to use linux crontab" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report