Can a script test if an other script is running and delay?

I have on my server sometimes a conflict that one script is running and interferes with an other that is already runing.

is it possible to script in such a way that a script checks if this is so and than delay? Any samples available (I would not know where to begin to do this).


Depends how where the processes are running and how well it needs to be protected.

There are hundreds of different ways to do this the question is how good should it work. Assuming that we’re talking about multiple processes on different machines (clients) trying to access a file on a shared volume. The most easiest way is use a try catch

repeat 5 times
	if writeToFile(theFile, "Hello world", text) then exit repeat
	delay 5 --(wait 5 seconds)
end repeat

on writeToFile(theFile, theContents, inClass)
		set fd to open for access file theFile with write permission
		set eof of fd to 0
		write theContents to fd as inClass
		close access fd
		return true
	on error
		close access file theFile
		return false
	end try
end writeToFile

Morning Davie, Thanks,

first of all the script does not run, I get: The variable theFile is not defined.

we are also not talking about multiple processes running on different machines, my situation is one server that does task via AplleScript, and sometimes two run simultaneously (accidental overlap) and so they interfere with one and other. I want each script to check if an other script is running, recheck and recheck and first run when no other one is running.


well first of all you had to define theFile yourself, how does a script from a forum knows where the file is without you telling us. :smiley:

What you’re looking for is a PID file (there are of course more ways to achieve what you want) which is used very often. When you start your script you open the PID file. You look for the process number in it look in the process list if that PID still exists. If it does delay and try again otherwise continue your script. When you continue you write your PID to that file so the next run it looks if your still running. The only flaw you have to catch is that when the server reboots you remove the pid file from your harddrive. The easiest way is storing the file in temporary items that is cleaned up after a reboot.