Linux Scheduled Tasks Crontab notes and summaries (5) Crontab common mistakes and cases

Source: Internet
Author: User

① Environment variables

CD ~pwd

It will go to the root of the user you landed on.

Ls-a

able to view the. bash_profile file

Vim. Bash_profile

Some environment variables are set up here.

You can set a new environment variable and add it at the end of the file:

appdir=/etcexport appdir

: wq! Save exit

source. Bash_profileecho $APPDIR

If a global variable is used in a scheduled task, such as

Crontab-E Write */1 * * * * echo $APPDIR >>/tmp/appdir.log

But at this point, use

Tail-f/tmp/appdir.log

When viewing the log, go to discover that the blank line is displayed and normally output/etc

$APPDIR is not recognized at all.

The reason is that environment variables set in. Bash_profile are not recognized by crontab, so be aware of them.

command line double quotes when using%, no backslash \

③ the or action is performed between the third and fifth domains

The third field is the day of the month, and the fifth field is the weekday of the month, for example: April the first Sunday 1:59 A.M. run a.sh

 - 1 1-74* test ' date +\%w '-eq 0 &&/root/a.sh

%w eq 0 indicates the day of the week on which the current day is executed if it equals 0

Test indicates whether the decision was successful

&& indicates that the previous test succeeds when it executes

Added

echo $?

Indicates whether the last command succeeded or failed, 0 indicates success, 1 indicates failure

④ minutes Setting misuse

"Run Two hours"

The correct wording

0 */2 * * * Date

"Re-example"

1-/2 * * * * COMMAND

Represents the 1th, 3, 5, 7, 9.....19 minutes to run the command

============

"Comprehensive Case"

"Example 1" Crontab the smallest can only be set to every minute to execute a command, if you want to execute a command every half minute how to do?

--This can be done with the Crontab of the shell script's sleep command

0. 5s && Date

For example:

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

"Example 2"

Crontab-e*/1 * * * * Date >>/tmp/date.log*/1 * * * * Seep 30s;date >>/TMP/DATE.L og

The first command guarantees the execution of the command every minute.

The second command, the execution of the time, the first to prevent 30s, then go to execute

Use the following command to view the time of the write

Tail-f/tmp/date.log

Linux Scheduled Tasks Crontab notes and summaries (5) Crontab common mistakes and cases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.