Answer the question
In order to leave comments, you need to log in
One process writes to a file, another renames it?
I want to implement something like log rotation,
the reality is that the demon script constantly writes to the log file, this is done and works
at midnight, I want to rename the file, what will happen: the
demon does not know that the file has been renamed, the file will be reopened?
whether there will be a loss of log information, I’m
wondering what a rake can be
in addition to the above: the
daemon is started via exec from the script via nohup with redirecting stdout to the log
file, it will not be possible to restart the file
Answer the question
In order to leave comments, you need to log in
The file descriptor remains unchanged, i.e. if the script has received the inode of the file, it can write to it until it closes, regardless of the renaming and even deletion.
Alternatively, you can open/close the file each time the log is written. Well, or write the logic of closing / renaming / opening in the daemon itself.
If this is, for example, a bash script that uses output like ">> /var/log/mydaemon.log" then there will be no problems.
If it were a C program, say, opening a file for writing, then the renaming would most likely not work until reopened.
You need to reopen the file, otherwise, after renaming, the old file will continue to be written.
If special accuracy is not needed, you can simply reopen the file for writing, say, once an hour.
I understand that renaming the file is necessary for log-rotate? Then the most correct thing to do is:
cp logfile logfile.old
cp /dev/null logfile
You need to lock the file when writing, and the daemon that backup does must wait until the file is unlocked and do what you need.
in general, “in the classics” the scheme is as follows:
1. the demon opens a file and writes a log to it
2. logrotate makes an mv log file and sends a signal to the demon, for example SIGHUP
3. The demon has a handler for this that EITHER reopens the file OR does seek (0)
T .to. in your case, there is no way to control the file descriptor, the solution suggests itself - write or find a ready-made “proxy” to which the output will be redirected and which will be able to catch signals and reopen the file.
exec("my_daemon | my_proxy --out=/my/log/file")
You can not signal, but, for example, touch a file. But the signal is more efficient and simpler.
Here is another example hg.python.org/cpython/file/2145593d108d/Lib/logging/handlers.py#l358 where the inodes of the file are compared each time before writing.
PS: does nohup write something to stderr/out???
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question