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

What if crontab output redirection does not take effect in Linux

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

Share

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

This article mainly introduces the Linux crontab output redirection does not take effect how to do, the article is very detailed, has a certain reference value, interested friends must read it!

problem

In LINUX, periodic tasks are usually handled by the cron daemon [ps-ef | grep cron]. Cron reads one or more configuration files that contain the command line and its call time.

The configuration file for cron is called "crontab", which is short for "cron table".

Recently, a scheduled task has been added to crontab, and the task will have normal output by default after it is executed. In order to ensure that the abnormal information during the execution of the task can also be captured and facilitate the problem location, I wrote this command in crontab:

01 09 * cd / opdir/test/ & &. / test.sh & > > test.log

The above command is easy to understand. The test.sh script is executed at 9:01 every day and the standard error output and standard output of the script are all redirected to the file test.log. It turns out that the script executes normally, but there is nothing in the log file test.log.

In order to solve and explain this problem, let's briefly introduce the problem of redirection in linux system.

Concept

In Linux systems:

1: represents standard output (stdout), which is output to the screen by default

2: indicates standard error output (stderr), which is output to the screen by default

In normal times, we often use the following methods to redirect the results of script execution:

The standard output of the bash test.sh > test.out / / script is written to the file test.out, and the standard error output is printed directly on the screen equivalent to: bash test.sh 1 > test.outbash test.sh > test.out 2 > & 1 / / both standard output and standard error output are written to test.out and do not overwrite each other, equivalent to bash test.sh & > test.outbash test.sh > test.out 2 > test.out / / standard output and standard error output are written to test.out There will be the problem of mutual coverage. Normally, it is not recommended to use bash test.sh & > test.out / / which is equivalent to the second method.

Compare the effects of the above:

First: error output in the screen, normal output in the file test.out

Root@mengalong:~/opdir/mengalong/t/t# cat bash test.sh. Line 2: t: command not foundroot@mengalong:~/opdir/mengalong/t/t# cat test.outWed Oct 31 11:07:24 CST 2018

Second: both error output and normal output are redirected to the file test.out

Root@mengalong:~/opdir/mengalong/t/t# bash test.sh > test.out 2 > & 1root@mengalong:~/opdir/mengalong/t/t# cat test.outtest.sh: line 2: t: command not foundWed Oct 31 11:09:02 CST 2018

Third, error output and normal output cover each other.

Root@mengalong:~/opdir/mengalong/t/t# bash test.sh > test.out 2 > test.outroot@mengalong:~/opdir/mengalong/t/t# cat test.outWed Oct 31 11:10:36 CST 2018ot found

Fourth, in special cases, compare the difference between bash test.sh 2 > & 1 > test.out and bash test.sh > test.out 2 > & 1:

Root@mengalong:~/opdir/mengalong/t/t# bash test.sh 2 > & 1 > test.outtest.sh: line 2: t: command not foundroot@mengalong:~/opdir/mengalong/t/t# cat test.outWed Oct 31 11:12:13 CST 2018

Here we just put 2 > & 1 in front of > test.out, but the result is not as we thought, the error and normal output go into the test.out file. This is because in the bash test.sh 2 > & 1 > test.out command, 2 > & 1 only redirects the error output to standard output, while the default value for standard output is the screen, so it is actually equivalent to the standard error output being redirected to the screen rather than to the file. Therefore, redirection needs to pay attention to the order.

Problem solving

Next, looking back, I wrote the crontab task:

01 09 * cd / opdir/test/ & &. / test.sh & > > test.log

According to the above conceptual analysis, this writing should be equivalent to. / test.sh > test.log 2 > & 1, and the output executed by the script and the standard error output are all redirected to test.log. But the reality is that there is nothing in the test.log file.

This is because the default shell environment used by crontab is / bin/sh, and / bin/sh does not support the redirection method & > > test.log, so the effect we see is that there is no content in test.log.

So the way to solve the problem is to modify the redirection method of crontab:

01 09 * cd / opdir/test/ & &. / test.sh > > test.log 2 > & 1

A long sentence

During the execution of crontab, if the script output is not redirected, an email will be sent to the system user by default. The content of the email is generally stored in / var/mail/$user. If it is not cleaned up, the root partition of the server will be full, resulting in the machine being unable to log in. Therefore, the recommended crontab command is written as follows:

01 09 * cd / opdir/test/ & &. / test.sh > > test.log 2 > & 1

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