Question

I have a shell script, That writes a content in to the file,

#!/bin/bash
SLEEP=1
FILE_PATH=${1}
while true
  do
    ps aux wc -l > ${FILE_PATH}
    sleep ${SLEEP}
done

I have another PHP Script that reads the content of the file using file_get_contents(). I am aware that file_get_contents() will make a LOCK_EX (Exclusive lock).

My Question is,

  1. Will the shell script lock the file while writing in to the file? if yes what type of lock will it use exclusive, shared ..?
  2. Since My PHP Script is also locking the file, will it stop shell script from writing and make the file unavailable for shell script?

Since i am writing the file in shell script in 1 sec interval. I am afraid of the chances of file writing/reading getting failed in both the scripts.

Was it helpful?

Solution

Generally, any file write will require an exclusive lock.

The PHP script will not be able to acquire its lock until the shell script releases, and conversely. This leads to what is known as a deadlock, when two processes are contending for the same resource.

There are strategies you can use for richer interprocess cooperation, but I would suggest using a different strategy for generating your process list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top