or I should close file - then mount overlay - and then re-open file again? i.e.
#!/bin/bash
my_background_process >log.txt &
...
pkill my_background_process
mount overlay
my_background_process >>log.txt &
...
is it necessary?
or I should close file - then mount overlay - and then re-open file again? i.e.
#!/bin/bash
my_background_process >log.txt &
...
pkill my_background_process
mount overlay
my_background_process >>log.txt &
...
is it necessary?
Once a file is open, it stays open until the process closes it. Reading and writing from a file don't care whether the file is still available under its original name. The file may have been renamed, deleted, shadowed… it's still the same file.
If you open a file /somewhere/somefile
then mount a filesystem at /somewhere
, then after that point /somewhere/somefile
will designate a file on the new filesystem. But that only matters when you open /somewhere/somefile
. If a process already had /somewhere/somefile
open on the original filesystem, that doesn't change.
Redirection opens a file when the shell processes the redirection operator. After that, it keeps being the same open file, even if multiple processes are involved. For example, in the snippet below, program2
writes to the file at /mnt/log.txt
on the root partition, whereas program3
writes to /log.txt
on /dev/something
.
do_stuff () {
program1
mount /dev/something /mnt
program2
program3 >/mnt/log.txt
}
do_stuff >/mnt/log.txt
If you've already started a program and you want to change where its output goes, you need to ask the program to do it. In theory, you can force the program to do it using a debugger — this thread lists some programs that can do it. But getting down and dirty with a program like this can crash it.
If you really need to change where the program's output goes midway, you need to relay the output through a helper that is able to change its own output. Here's a tiny proof-of-concept in a shell script:
my_background_process | {
while IFS= read -r line; do
printf '%s\n' "$line" >>/mnt/log.txt
done
} &
This script opens the output file for each line, so if the file designated by /mnt/log.txt
changes (because the file has been moved, because a new filesystem has been mounted at /mnt
, etc.) then the subsequent lines will be written to the new file. Note that you need to specify the name of the directory: with just >log.txt
, this would always open the file in the current directory, so it wouldn't be affected by a mount operation (the current directory works like an open file: mounting something on /mnt
doesn't affect what processes see as their current directory even if their current directory is /mnt
).
There is no support in Linux for automatically redirecting files which were opened on an arbitrary filesystem. It is necessary to do so yourself.