I want to know how to check whether is program running and run this program if not.
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");
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
kill 0, $pid
option works on other OSs, like Windows. :-) –
Subinfeudation pgrep
, grepping running processes is so common, there is a special command for that ;) –
Eozoic 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! :)
If $pid
is empty, the process is not running:
my $pid = `ps -C $progname -o pid=`; # non-windows solution, sorry
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;
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");
}
© 2022 - 2024 — McMap. All rights reserved.