Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to File

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hi, My first script writes to a file. Second script also tries to write to the same file. Basically, they have to do that seperately. Is it possible to lock the file while the first script is writing?

Thanks in advance.
 
You could add a "lock file" script. Example:

script 1:

before to write check if lock.pid exist?
yes: wait and recheck
No: create lock.pid
when done writing-> erase lock.pid

script 2:
before to write check if lock.pid exist?
yes: wait and recheck
No: create lock.pid
when done writing-> erase lock.pid

Hope this helps.
 
Could you give me the syntax or a simple example?
 
an example:

script 1:

while test -f /tmp/lock.pid
do
sleep 500
done
> /tmp/lock.pid
write_to_file >> file.out
rm /tmp/lock.pid


script 2:

while test -f /tmp/lock.pid
do
sleep 500
done
> /tmp/lock.pid
write_to_file >> file.out
rm /tmp/lock.pid

man sh, ksh or the shell you are working with for "while" and "test" use.

Cheers.
 
Code:
#this is the file you are going to 
#see if it exists for a lock
lockfilename="/path/to/file/to/create/for/lock"

#file you are writing to
writefile="/path/to/writefile"

if [ -f $lockfilename ]
 then
   echo "File is in use"
else
   touch $lockfilename
   echo "informationtowritefile" >>$writefile
   rm $lockfilename
fi

___________________________________
--... ...--, Eric.
 
WARNING: Processes are running concurently. After process running script1 checked that the lock file does not exists and before it create it, the process running script2 can also be checking that the file (still) does not exist and create it too.

To have a secure lock you need a test-and-set unsplittable operation and not two (test then set) operations.

The easiest way to achieve this is to use a directory as lock because the [tt]mkdir[/tt] command is a test-and-set unsplittable operation. ([tt]man mkdir[/tt])

Here is a test script implementing this solution. You just have to add the part from [tt]Start of lock[/tt] to [tt]End of lock[/tt] at top of your own script:
Code:
#!/bin/ksh

#===================
# Start of lock part

# Set the lock dir name (shared by processes so NO $$ in it !)
lockdir=/tmp/$(basename $0)-lockdir

# Set a trap to remove lock at script end (or abort)
trap "if test -d '$lockdir'; then
         echo '$0($$): Releasing $lockdir';
         rmdir '$lockdir';
       fi" 0

while true; do
  if mkdir "$lockdir" 2>/dev/null; then
     # Ok we just created the lock dir so exit the waiting loop
     echo "$0($$): Got lock '$lockdir': go on ..."
     break
  fi
  # We failed to create the lock dir so wait and try again
  echo "$0($$): Waiting for lock '$lockdir'"
  sleep 1;
done

# End of lock part
#=================

# Here we have the lock
# Do our work

for i in 1 2 3 4 5; do
  echo "$0($$): counting $i ..."
  sleep 3;
done

echo "$0($$): Work done"
# Lock released by the trap "" 0 at start of script

--------------------

Denis
 
Basically, you could have a file written as the first instruction in the first script called "script1run". The second script begins with a check for a file called "script1run" in the location that the first script put it. As long as its there, the second script waits in a loop. When the first script finishes, you erase the file and the second script is pulled out of its loop.

You can do the same thing with the second script... have it start by making a file called "script2run" and start the first script by checking for its presence and only starting if it fails to find it.

I would imagine that you can set an environmental variable to hold the running status of the scripts. Call it $ACTIVESCRIPT and have the scripts check its value before running and then set it to their script numbers while they are running. You can have multiple processes running while pass priority to whichever script holds the value in the $ACTIVESCRIPT variable. Hope this helps!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top