POSIX (IEEE Std 1003.1-2001) does not define the behavior of parallel write () syscalls that send data to the same file, so you may get different results depending on your platform. You can try to combine all printfs
into one, hoping that this will work, but even if this happens, there is no guarantee that it will be in the future or on another platform.
Instead of using concurrency control and cleanup to ensure write consistency, you can send messages to a third process that will write your log messages sequentially to a file on behalf of all processes. In fact, this is what is done with logger
and syslog
in the post you specified. The logger
command does not send messages to /var/log/messages
. It sends log messages to syslog
, which can be configured to save log messages anywhere. However, administrative privileges are usually required to change this configuration.
If you cannot or do not want to use syslog
, you can also use netcat as a log server. Run this to multiplex all incoming log messages from all scripts into a file (this task should constantly run in the background all the time, you can also run it in screen ):
nc -luk localhost 9876 > shared_log_file &
(port 9876 is just an example) and log in to each script as follows:
printf "Log message\n" > /dev/udp/localhost/9876
You can also use your own UDP server instead of netcat (like this one ).
source share