Usually i use node to log some server message into the local file, but sometimes the file too large, so i will regular archiving of this large file by logroate tool(etc: move the linz.log to the linz-2017-06-19.log, now the linz.log file is empty, but sometimes node will still write data into the linz-2017-06-19.log, i guess, maybe the linz.log descriptor not be changed)
Asked
Active
Viewed 1,050 times
1
-
2See [`inotify()`](http://man7.org/linux/man-pages/man7/inotify.7.html) – Jun 20 '17 at 11:15
-
3This is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Should instead explain in detail and ask about the real problem - writing data to wrong file. – kaylum Jun 20 '17 at 11:34
-
@kaylum Indeed. Solve the *real* problem - don't write even more code to hide the symptoms. Unless you like hacked-together, complex, unmaintainable systems that don't work very well. Just because you know how to write code doesn't mean you should solve every problem by tossing more code at it. The standard way to notify a daemon process that it needs to switch to a new log file is to send `SIGHUP` to the process. Hopefully you're not logging by redirecting `stdout` or `stderr`. If you are, you just learned why that's a **BAD** idea. – Andrew Henle Jun 20 '17 at 11:51
-
Please show your code. Lack of code is generating confusion from both the question side of things and the answer side of things. Related, see [Tool or script to detect moved or renamed files on Linux prior to a backup](https://serverfault.com/q/171871/145545) on Server Fault. – jww Jun 20 '17 at 13:57
-
Possible duplicate of [Best approach to detecting a move or rename to a file in Linux?](https://stackoverflow.com/q/3514771/608639), [linux inotify events for rename() with overwrite](https://stackoverflow.com/q/26932459/608639) and [Monitor directory content change](https://stackoverflow.com/q/10512707/608639) – jww Jun 20 '17 at 13:59
1 Answers
2
You can use inotifywait
inotifywait efficiently waits for changes to files using Linux's inotify(7) interface. It is suitable for waiting for changes to files from shell scripts. It can either exit once an event occurs, or continually execute and output events as they occur.
Example:
touch /tmp/foofile
inotifywait -e move /tmp/
mv /tmp/foofile /tmp/barfile
Output from inotifywait
Setting up watches.
Watches established.
/tmp/ MOVED_FROM foofile

sob
- 982
- 11
- 31
-
You should probably show the code to do it, and not the commands. Stack Overflow is a site for programming and development question. The programming answer applies here. – jww Jun 20 '17 at 13:55