Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IO Redirection in Linux Bash shell scripts not recreating moved/deleted file?

I am quite new to shell programming on Linux and in my Linux instance, I am redirecting the stdout and stderr of a program to two files in following manner and run it in background

myprog > run.log 2>> err.log &

This works fine, and I get my desired behavior

Now there is a another background process that monitors the run.log and err.log, and moves them to other file names, if the log files grow beyond a certain threshold.

e.g. mv err.log err[date-time].log

my expectation is that after this file move happens, err.log will be created again by the myprog output redirection and new output will be written to that new file. However, after my log file monitoring process moves the file, err.log or run.log never get created again although myprog continues to run without any issues.

Is this the normal behavior in Linux? If it is, what should I do to get my expected behavior working?

like image 439
ancient demon Avatar asked Jun 19 '15 15:06

ancient demon


People also ask

How do you redirect both the output and error of a command to a file?

Any file descriptor can be redirected to other file descriptor or file by using operator > or >> (append).

How do I redirect all output from a bash script to a file?

For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected. “>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents.

How do I redirect stdout and stderr to the same location?

Solution. Use the shell syntax to redirect standard error messages to the same place as standard output. where both is just our (imaginary) program that is going to generate output to both STDERR and STDOUT.


1 Answers

Yes, it is. Unless you first program reopen the files, it will keep writing to the old file, even if you can't access it anymore. In fact, the space used by that removed file will only be available after every process closes it. If reopening it is not possible (ie. you can't change the executable nor restart it), then a solution like http://httpd.apache.org/docs/2.4/programs/rotatelogs.html is your best bet. It can rotate logs based on filesize or time, and even call a custom script after a rotation.

Example usage:

myprog | rotatelogs logname.log 50M

This way the log will be rotated whenever the size reaches 50 megabytes.

[EDIT: pointed to a newer version of rotatelogs]

like image 151
Vitor Avatar answered Sep 28 '22 17:09

Vitor