check if program is running and run it if not in perl

perl

Solution

Send a 0 (zero) signal to the process ID you want to check using the kill function. If the process exists, the function returns true, otherwise it returns false.

Example:

#-- check if process 1525 is running
$exists = kill 0, 1525;
print "Process is running\n" if ( $exists );

You can call any program like you would from the command line using a system call. This is only useful if you do not need to capture the output of the program.

#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi fred.txt");

Or if you don't want to involve the shell:

#!/usr/bin/perl
use strict;
use warnings;
my $status = system("vi", "fred.txt");

Problem

I want to know how to check whether is program running and run this program if not.

Original source

Related problems