Solution to the problem that crontab output redirection does not take effect in Linux

Solution to the problem that crontab output redirection does not take effect in Linux

question

In LINUX, periodic tasks are usually handled by the cron daemon process [ps -ef | grep cron]. Cron reads one or more configuration files that contain command lines and the times they are called.

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

Recently, a scheduled task was added to crontab. After the task is executed, there will be normal output by default. In order to ensure that the exception information during the task execution can also be captured and facilitate problem location, I wrote such a command in crontab:

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

The above command is very easy to understand. It executes the test.sh script at 9:01 every day and redirects the script's standard error output and standard output to the file test.log. Finally, it was found that the script was executed normally, but there was no content in the log file test.log.

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

concept

Linux:

1: indicates 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

Usually we often use the following method to redirect script execution results:

bash test.sh >test.out //The standard output of the script is written to the file test.out, and the standard error output is directly printed on the screen. Equivalent to: bash test.sh 1>test.out
bash test.sh >test.out 2>&1 //Both standard output and standard error are written to test.out and will not overwrite each other, which is equivalent to bash test.sh &>test.out
bash test.sh >test.out 2>test.out //Both standard output and standard error output are written to test.out, which may overwrite each other. It is not recommended to use bash test.sh &>test.out //Equivalent to the second method

Compare the above effects:

The first type: Error output is on the screen, normal output is in the file test.out

root@mengalong:~/opdir/mengalong/t/t# cat test.sh
#!/bin/bash
t
date

root@mengalong:~/opdir/mengalong/t/t# bash test.sh >test.out
test.sh: line 2: t: command not found
root@mengalong:~/opdir/mengalong/t/t# cat test.out
Wed Oct 31 11:07:24 CST 2018

The second type: 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>&1
root@mengalong:~/opdir/mengalong/t/t# cat test.out
test.sh: line 2: t: command not found
Wed Oct 31 11:09:02 CST 2018

The third type: error output and normal output cover each other

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

Fourth, special case, 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.out
test.sh: line 2: t: command not found
root@mengalong:~/opdir/mengalong/t/t# cat test.out
Wed Oct 31 11:12:13 CST 2018

Here we just put 2>&1 before >test.out, but the result is not what we expected. Both error and normal output go into the test.out file. This is because, in the command bash test.sh 2>&1 >test.out, 2>&1 only redirects the error output to the standard output, and the default value of the standard output is the screen at this time, so it is actually equivalent to the standard error output being redirected to the screen instead of the file. Therefore, the redirection sequence needs to be taken into consideration.

Problem Solving

Next, let’s look back at the crontab task I wrote:

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

According to the above conceptual analysis, this writing method should be equivalent to ./test.sh >test.log 2>&1. The output of the script execution and the standard error output are all redirected to test.log. But the actual situation 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 of &>>test.log, so the effect we see is that there is no content in test.log.

Therefore, the solution to the problem is to modify the redirection method of crontab:

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

A word of advice

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

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

Specifically, </dev/null & is added at the end. I won’t explain the meaning of this in detail. If you are interested, you can analyze it yourself.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Linux crontab scheduled task configuration method (detailed explanation)
  • How to use Linux Crontab to execute PHP scripts regularly
  • Linux uses crontab to implement PHP execution plan timing tasks
  • Linux crontab example analysis
  • MySQL scheduled backup using crontab scheduled backup example under Linux
  • How to use crontab to execute a scheduled task once a second in Linux
  • How to execute tasks regularly under Linux and instructions on how to use crontab (collected and sorted)
  • Detailed explanation of using python crontab to set up Linux scheduled tasks
  • Detailed explanation of Linux input and output redirection
  • Detailed instructions for using Linux input and output redirection

<<:  Let’s take a look at JavaScript precompilation (summary)

>>:  Solution to the error when importing MySQL big data in Navicat

Recommend

Detailed installation and configuration of hadoop2.7.2 under ubuntu15.10

There are many Hadoop installation tutorials on L...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

How to uninstall Linux's native openjdk and install sun jdk

See: https://www.jb51.net/article/112612.htm Chec...

JavaScript implements click toggle function

This article example shares the specific code of ...

202 Free High Quality XHTML Templates (1)

Here 123WORDPRESS.COM presents the first part of ...

Detailed explanation of the marquee attribute in HTML

This tag is not part of HTML3.2 and is only suppo...

React.js framework Redux basic case detailed explanation

react.js framework Redux https://github.com/react...

Explore JavaScript prototype data sharing and method sharing implementation

Data Sharing What kind of data needs to be writte...

Analysis of the principles of docker containers

Table of contents 01 What is the essence of a con...

Use the CSS border-radius property to set the arc

Phenomenon: Change the div into a circle, ellipse...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

Tutorial on resetting the root password of Mac MySQL

Disclaimer: This password reset method can direct...