Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Is the program there? (W32)

by herby1620 (Monk)
on Mar 23, 2006 at 19:22 UTC ( [id://538834]=perlquestion: print w/replies, xml ) Need Help??

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

Wonderful people (who have helped me so very much in the past)

I need to launch a program from a Perl script in WinXP/2K. While I can easily do 'system...' (which DOES work) that is not the end of the story. You see, if the program is already launched (by a previous invocation of my script, or someone else) I DON'T want to launch it again (the application really barfs if you do!). So, how do I tell if a program is running. Were this Linux, or similar, I'd look at the output of 'ps' and parse that. Alas, this is Windows, and even though I've got cygwin [sp?] the 'ps' output isn't anything close to having enough information that I desire. I've looked at such things as Win32::OLE and Win32::Process and they won't tell me what I want to know. There ought to be a 'simple' way I can tell if a particular program is running somewhere on the system. Then again, this is Windows, and nothing is 'simple' (*SIGH*).

Replies are listed 'Best First'.
Re: Is the program there? (W32)
by asz (Pilgrim) on Mar 23, 2006 at 21:16 UTC

    you just gived me a reason to reboot into Windows XP :)

    i found 2 ways of getting the PID of a process on a Windows XP system:

    1. use the command tasklist (installed by Windows in %WINDIR%\system32) to get a table with details of the running processes on your system that you could easly parse
    2. use Win32::Process::Info from CPAN to get more details

      #!perl use strict; use warnings; use Data::Dumper; use Win32::Process::Info; my $procs = Win32::Process::Info->new(); print Dumper $procs->GetProcInfo();

    hope this helps...

    :)))))
Re: Is the program there? (W32)
by pKai (Priest) on Mar 23, 2006 at 21:20 UTC
    Is the program there?

    Yes

    Actually IMHO these people do a tremendous job. Even though this isn't a perl solution. But it echos the sentiment of your plea for a win32-ps.

Re: Is the program there? (W32)
by BrowserUk (Patriarch) on Mar 24, 2006 at 07:10 UTC

    Here's another way using a semaphore.

    #! perl -slw use strict; use Win32::Semaphore; ( my $semName = $0 ) =~s[\\][/]g; die 'Program already running' if Win32::Semaphore->open( $semName ); my $sem = Win32::Semaphore->new( 1, 1, $semName ) or die $^E; sleep 1000; __END__ c:\test>start /b loner.pl c:\test>loner.pl Program already running at c:\test\loner.pl line 6.

    I've used the script's full pathname as the semephore name, with backslashes switched to forward slashes as the former are disallowed. If there is the possibility of multiply named copies of the script, then use a well-known name instead.

    Oh. And don't let $sem or whatever you call yours go out of scope otherwise the system semaphore gets freed. Obvious I guess, but it hung me up for a while.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is the program there? (W32)
by lima1 (Curate) on Mar 23, 2006 at 21:10 UTC

    you could use Proc::PID_File to avoid running multiple instances of your script with a few lines of code.

    but this does not work if someone starts the program in an alternate way.

    googeling gave this link

Re: Is the program there? (W32)
by Thelonius (Priest) on Mar 23, 2006 at 22:40 UTC
    Searching through ps output is not really the right way to do it on Unix. It's time-consuming and error-prone--someone may create a link to the program with a different name or there may be two different programs with the same name.

    Fortunately there's a simple solution that works on Unix and Windows:

    #!perl -w use strict; use Fcntl qw(LOCK_EX LOCK_NB); my $lockfile = "/someabsolutepath/writeabledirectory/lockfile"; open LOCKFILE, ">", $lockfile or die "Cannot open lockfile\n"; if (!flock(LOCKFILE, LOCK_EX|LOCK_NB)) { print STDERR "Another copy of application is already running\n"; exit(0); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://538834]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found