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

Linux scheduling tasks Crontab instance tutorial

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the "Linux planning task Crontab example tutorial", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and study the "Linux planning task Crontab example tutorial"!

① environment variable

The code is as follows:

Cd ~

Pwd

It will go to the root directory of the user you logged in

The code is as follows:

Ls-a

You can see the .bash _ profile file.

The code is as follows:

Vim .bash _ profile

Some environment variables are set here.

You can set a new environment variable with the following at the end of the file:

The code is as follows:

APPDIR=/etc

Export APPDIR

: wq! Save exit

The code is as follows:

Source .bash _ profile

Echo $APPDIR

If global variables are used in scheduled tasks, such as

The code is as follows:

Crontab-e

Write

The code is as follows:

* / 1 * echo $APPDIR > > / tmp/appdir.log

But use it at this time

The code is as follows:

Tail-f / tmp/appdir.log

When viewing the log, it is found that the blank line is displayed and / etc should normally be output.

$APPDIR is not recognized at all.

The reason is that the environment variable set in .bash _ profile is not recognized by crontab, so be careful.

When% is used in ② command line double quotes, there is no backslash\

The OR operation is performed between the third and fifth domains of the ③

The third domain is the day of the month, and the fifth domain is the day of the week of the month. For example, a.sh runs at 01:59 on the first Sunday in April.

The code is as follows:

59 1 1-7 4 * test `date +\% w`-eq 0 & & / root/a.sh

W eq 0 means that the day of the week in which the current day is located is executed if it equals 0

Test says to judge whether it is successful or not.

& & means to execute when the previous test is successful

[supplement]

The code is as follows:

Echo $?

Indicates whether the execution of the last command was successful. 0 indicates success, and 1 indicates failure.

Misuse of ④ minute setting

[run every two hours]

The correct way to write

The code is as follows:

0 * / 2 * date

[another example]

The code is as follows:

1-20Bax 2 * COMMAND

Indicates that the command is run in 1, 3, 5, 7, 9.19 minutes.

=

[comprehensive case]

[example 1] in Crontab, the minimum can only be set to execute one command per minute. How do you do that if you want to execute a command every half a minute?

This function can be accomplished by using the sleep command of the shell script with Crontab.

The code is as follows:

Date & & sleep 0.5s & & date

For example:

This example indicates that date is executed first, and then date is executed after 0.5s.

[example 2]

The code is as follows:

Crontab-e

* / 1 * date > > / tmp/date.log

* / 1 * seep 30s * date > > / tmp/date.log

The first command ensures that the command is executed every minute

The second order, when executing, wait for the obstruction for 30s, and then execute it.

Use the following command to view the time of the write

The code is as follows:

Tail-f / tmp/date.log

Thank you for reading, the above is the content of "Linux Planning Task Crontab example tutorial". After the study of this article, I believe you have a deeper understanding of the problem of Linux Planning Task Crontab example tutorial, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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