Some obvious problems: there's a race condition. If two
instances start at the same time, they might both determine
the lockfile isn't there (the
-e failing),
and both create the file and write the PID in it.
Furthermore, if the program doesn't get the chance to run
the END block (kill -9, exec), it might happen that the
next time an instance is run, it finds the lockfile, and it
just happens there's an unrelated process running with the
same PID as mentioned in the PID file.
I'd say, open the file for read/write/create, and then try
to gain an exclusive lock (non-blocking). If you get the lock,
you're the only instance running. Keep the lock until the
program terminates. Regardless how the process terminates,
the kernel will release the lock (but make sure you pick a
local file, not one mounted by NFS).
Abigail