check if program is running and run it if not in perl
Asked Answered
B

6

12

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

Becnel answered 30/6, 2012 at 11:15 Comment(2)
Which operating system are you using?Toed
Debian 6, but the question is solved.Becnel
C
9

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");
Cask answered 30/6, 2012 at 11:19 Comment(2)
search.cpan.org/~durist/Proc-ProcessTable-0.39/ProcessTable.pm I don't have much know how with perl, but this I'm sure is what you're looking for.Cask
See also #3844668Buy
H
5

Similar to another answer, but you need to make sure and use "grep -v grep" to not match the grep call itself. This will ensure you aren't evaluating to true when you don't want to.

use strict;
use warnings;

my($cmd, $process_name) = ("command here", "process name here");

if(`ps -aef | grep -v grep $process_name`) {
    print "Process is running!\n";
}#if
else {
    `$cmd &`;
}#else
Hypoacidity answered 3/5, 2013 at 15:55 Comment(2)
This answer works only on *nix. One of the main points of using built in commands and CPAN modules is in most cases not having to worry about portability. The kill 0, $pid option works on other OSs, like Windows. :-)Subinfeudation
I think you can just use pgrep, grepping running processes is so common, there is a special command for that ;)Eozoic
D
3

I tried "kill 0..." thing, but that won't work if you do not have a permission to the process, since "kill 0" only checks if it is possible to send a signal. Since you have no permission to it (your UID is not 0 and process is not UID either), you will get always false.

I also tried to take to the account /proc/ in Linux to just check if the PID directory is there, but that part is not very portable: good for linux, but won't really work elsewhere in UNIX without an additional love.

So I wrote this sub, HTH:

sub is_running() {
    my $pid = shift;
    my @proc_data = split(/\s+:\s+/, 
                          `ps uax | awk '{print \$1,":",\$2}' | grep $pid`);
    return (@proc_data && $proc_data[1] == $pid) ? $proc_data[0] : undef;
}

Usage is simple and pretty useful, as it returns you also owner of the process:

my $pid = 12345;
my $owner = &is_running($pid);
if ($owner) {
    print "Process with PID $pid is running and owned by \"$owner\".\n";
}

Have fun! :)

Dialogism answered 22/5, 2014 at 13:22 Comment(0)
O
2

If $pid is empty, the process is not running:

my $pid = `ps -C $progname -o pid=`; # non-windows solution, sorry
Overman answered 20/3, 2015 at 20:2 Comment(0)
A
0

We use this to check if a daemon is running, based on the content of the daemon start pid-file on Linux:

#!/usr/local/bin/perl
use strict;
use warnings;

use feature qw/ say /;

# vars we report
my (
    $token,         # optional arg - check against cmd_line if specified
    $pid_file,      # daemon pid-file, e.g. /var/run/mysqld/mysqld.pid
    $pid,           # the pid to investigate...
    $pid_running,   # found a pid and it is running
    $cmd_line,      # cmd_line of the running pid
    $result,        # 0-OK, 1=FAIL, exit value
    $error,         # error message if necessary
  );

# working vars
my ( $fh, $cmd_file );

die "Daemon pid-file required" unless scalar @ARGV >= 1;
( $pid_file, $token ) = @ARGV;

if ( -s $pid_file ) {
  open( $fh, "<", $pid_file ) or die "open $pid_file: $!";
  ( $pid ) = <$fh>; chomp $pid; close $fh;

  $pid_running = kill 0, $pid;

  if ( $pid_running ) {
    $cmd_file = "/proc/$pid/cmdline";
    local($/) = "\0"; # C-String NULL terminated
    open( $fh, "<", $cmd_file ) or die "open $cmd_file: $!";
    ( $cmd_line ) = <$fh>; close $fh;
    if ( $cmd_line && $token && $cmd_line !~ m/$token/ ) {
      $error = "token not found: $token in $cmd_line";
    }
  }
  else {
    $error = "process not found: $pid";
  }
}
else {
  $error = "file not found: $pid_file";
}

# if TOKEN - OK running process with matching cmdline
$result = $token ? ( $pid_running && $cmd_line && $cmd_line =~ m/$token/ )
                 : ( $pid_running || 0 );

say "token:     ", $token if $token;
say "file:      ", $pid_file;
if ( $pid ) {
  say "pid:       ", $pid;
  say "running:   ", $pid_running;
  say "cmdline:   ", $cmd_line if $cmd_line;
}
say "error:     ", $error if $error;
say "exit:      ", $result ? 'ok' : 'fail';

exit $result;
Aluminate answered 15/1, 2014 at 19:48 Comment(0)
V
0

On Linux simply check if the process id exists in the /proc filesystem

if (not -d "/proc/$pid") {
  print "Process with $pid is not running");
}
Vladivostok answered 10/3, 2023 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.