Friday, June 6, 2008

Perl script to check the number of instances of a program

To get the number of instances of a program running in your machine you can use the following Perl code:

my $program_name = 'abc.pl';
my $data = `ps ax | grep perl`;
my @ara = $data =~ /perl\s+$program_name/g;
my $count = @ara;
print "$count\n";


Sometime it might be useful for you to know at the start of a program whether it's already running. You can get the name of your current Perl script/program from $0. If the name of your Perl script is program.pl, then $0 will contain program.pl. You can try print $0;
Hope this tiny piece of code will be useful for you. :)

No comments: