Red_Dragon has asked for the wisdom of the Perl Monks concerning the following question:

Environment: Windows 2000, Perl: ActiveState 5.8, is it possible to use one Perl script to see if another Perl script is running, that is to say script 'A' is run and as part of its execution checks to see if script 'B' is active. As a novice to Perl (a five day class 4 wks ago) any guidance or examples will be greatly appreciated.

Replies are listed 'Best First'.
Re: Check If Active
by sgifford (Prior) on Aug 13, 2003 at 16:22 UTC

    The easiest way to do this is to have Script B leave some kind of marker when it's running, like a locked file in a pre-arranged location. When it starts up, it write-locks the file, and when it shuts down it unlocks it. On UNIX, this works well, because if Script B crashes the OS will automatically revoke its locks; not sure about Windows.

    When Script A wants to check if B is running, it attempts nonblockingly to get a read-lock on this file. If that succeeds, B isn't running, and A should give up the lock. If it doesn't succeed, B is running.

    On UNIX, you get locks with fcntl; not sure about Windows.

Re: Check If Active
by ChrisS (Monk) on Aug 13, 2003 at 17:31 UTC
    Check this node for more information on file locking on windows.
Re: Check If Active
by NetWallah (Canon) on Aug 13, 2003 at 18:59 UTC
    Another, kind-of-klunky way to do this is to run the handle program from SysInternals.

    Scan the output of this - you get the process ID and open directories of all programs - look for "perl".

    Now comes the ticky part - Perl has already closed your particular program, but it has the working directory Open. If you can identify your perl code based on the working directory, you can use this method. Basically, all you need to do is to place the program in a directory with no other perl code. Then, if you see that directory opened by Perl, you know the program is running.

    Like I said before - this is kludgy, but easy to implement. Here is sample output from "handle:".

    ---------------------------------------------------------------------- +-------- cmd.exe pid: 2948 MyDomain\MyUserID c: File G:\WINDOWS\system32 54: Section \BaseNamedObjects\ShimSharedMemory 58: File G:\Documents and Settings\BLAH\test\handleout.txt ---------------------------------------------------------------------- +-------- ... ---------------------------------------------------------------------- +-------- perl.exe pid: 4008 MyDomain\MyUserID 110: File G:\Documents and Settings\MyDir\test ---------------------------------------------------------------------- +--------
Re: Check If Active
by PodMaster (Abbot) on Aug 14, 2003 at 08:49 UTC
    Try win32 mutex objects with Win32::Mutex. Have app A obtain a mutex. If app B can obtain the same mutex, then app A is not running.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.