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

Using the exec or system functions, does anyone know how to only start an external program(lets say Notepad) only if no instance of the program is running? I'm writing a small multiuser server in Perl which is started off by Shockwave. But only if it's not already running. Thank you.

Replies are listed 'Best First'.
Re: Exec or system functions
by clintp (Curate) on Jul 16, 2001 at 02:43 UTC
    This is an OS-specific question. The usual answers are to leave a file with a process ID laying around and variations of that. If the PID is still active, then the process must be too. Or grunge through some kinds of process listing looking for the name.

    Another answer is to open/lock a file -- if it fails (for a specific reason) then someone else has the file open and locked: your other instance.

    One way I like to do it is to bind a specific network port on localhost. You don't actually have to listen to the port, just bind it. Only one "process" can bind a port at a time. If you try to bind and it's already bound, then there's already an instance of you running. (Note: pick a good out-of-the-way port number...) It's a portable technique and almost foolproof.

      If it's an application that you can access with OLE, you could use something like this (which I use to either use an open instance of excel or create a new one..
      my $exl = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application');
      There's also some API call's.. but I've never used them with Perl. Here's the link to some VB code that will do what you're looking for. Now, you just have to figure out how to use that and Win32::API

      Hope this helps..
      Rich

      Whoops.. this was supposed to be a reply to the question.. not the first answer :)

Re: Exec or system functions
by HyperZonk (Friar) on Jul 16, 2001 at 06:13 UTC
    There is a module on CPAN at www.cpan.org/modules/by-module/Win32/Win32-GuiTest-1.1.zip that includes a FindWindowLike method. I haven't tried it out, but you may find it does what you want. I don't think that the Shockwave server application is OLE accessible, though it may be.
Re: Exec or system functions
by MZSanford (Curate) on Jul 16, 2001 at 15:50 UTC
    in complete agreement with clintp, that the pid file creation and file locking is about as standard as can be. The only problem is that it only works if a the program in question creates the pid file everytime, or is always started by the same application. If this is Win32 specific, checkthe Win32 modules on CPAN, as there are a few that do basic OLE work.

    For simpler programs, where OLE is not available, and findWindow style functions are not used (for whatever reason), the last would be reading the process list. This is a few of the things i can think of, including the downsides to a few.
    OH, a sarcasm detector, that’s really useful
Re: Exec or system functions
by Anonymous Monk on Jul 16, 2001 at 17:58 UTC
    This is just to say thank you to all of you that helped me with that question. Omena