user2094311
user2094311

Reputation:

Shell script - Redirect to file not working after editing the file manually

I have a script file that runs following. It actually runs a Java application and console logs are redirected to a log file.

$JAVA_CMD $APP_ENV $APP_MAIN_CLASS >>&! $LOGFILE &

It runs fine. I tried editing the file manually when the process is running. But the problem is, after I edit the file, and when I do some operation in the application, there are no logs appended to the existing file anymore.

Why is that? And how to Resolve it?

Upvotes: 0

Views: 42

Answers (1)

tripleee
tripleee

Reputation: 189387

You cannot edit a file while it is being written to. Most editors will end up moving the file to a different inode, at least temporarily, while your Java process only has a file handle to a file which has now ceased to exist.

The fundamental concept here is that a file is basically a unidirectional sequence. Appending to a file while something earlier in the sequence is modified by another process is simply not well-defined.

A common workaround is to copy the file before editing it; another, for log files, is to implement some sort of log file rotation mechanism (so you can say "let go of the log and start a new one, please"), but then simple redirection won't work for writing to the log.

Upvotes: 0

Related Questions