1

I have 2 programs, both writing to the same file (/tmp/outfile). Started by cron at the same time.

Basically this is what is happening:

echo -n "1111111111" >> /tmp/outfile

And at the same time:

echo -n "2222222222" >> /tmp/outfile

The output file says "11111222222222211111". This is an example, I am talking about hundreds of lines, where one line is "cut" mid-sentence, but simply put, above thing is happening.

How to prevent this behavior?

galoget
  • 349
Karel
  • 1,468
  • 1
    Well, you have different threads with a handle on the same file, the behavior that you're seeing is perfectly logical. To prevent it, the different cron jobs that you have should each lock a file when they're writing to it, unlock it when they're done, and wait before writing to a file when it is locked. Have a look at flock -- it provides the necessary mechanisms to do so. – Malte Skoruppa Mar 15 '18 at 10:30
  • Related: https://unix.stackexchange.com/questions/299627/multiple-appenders-writing-to-the-same-file-on-nfs-share/299653#299653 –  Mar 15 '18 at 10:33
  • Prevent it and do what? – muru Mar 15 '18 at 11:01
  • Flock sounds like an answer. I want to prevent sentences from being "broken" in 2 parts :) – Karel Mar 15 '18 at 13:10

1 Answers1

2

There are two immediately obvious ways to solve this:

  1. Serialize the tasks. Instead of scheduling the two tasks at the same time, schedule a script that runs the tasks one after the other.

  2. Use a advisory locking scheme to lock the writing operation of the tasks in such a way that only one task can write at a time. See questions tagged with and .

These two may be combined into a single script that runs both tasks in the background while the tasks themselves uses some form of locking as to not produce garbled/intermingled output.

Kusalananda
  • 333,661