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


i quite new shell programming on linux , in linux instance, redirecting stdout , stderr of program 2 files in following manner , run in background

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

this works fine, , desired behavior

now there background process monitors run.log , err.log, , moves them other file names, if log files grow beyond threshold.

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

my expectation after file move happens, err.log created again myprog output redirection , new output written new file. however, after log file monitoring process moves file, err.log or run.log never created again although myprog continues run without issues.

is normal behavior in linux? if is, should expected behavior working?

yes, is. unless first program reopen files, keep writing old file, if can't access anymore. in fact, space used removed file available after every process closes it. if reopening not possible (ie. can't change executable nor restart it), solution http://httpd.apache.org/docs/2.4/programs/rotatelogs.html best bet. can rotate logs based on filesize or time, , call custom script after rotation.

example usage:

myprog | rotatelogs logname.log 50m 

this way log rotated whenever size reaches 50 megabytes.

[edit: pointed newer version of rotatelogs]


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -