How to find pid of PS which executed by perl system function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find pid of PS which executed by perl system function
# 1  
Old 07-27-2008
How to find pid of PS which executed by perl system function

hello All,

I need to invoke by perl script some program/command and monitor it for 5
minutes . In case it still running for more then 5 min I need to send a signal which will stop it.

I implemeted this as shown below by using eval & alarm
and I'd like to know if there is a better way to find the PID of the PS which I executed by calling system , with perl command , instead of grep the ps command ?

Thanks in Advance.

eval {
local $SIG{ALRM} = sub {die "died in SIG ALRM";};
alarm(10);
`./time.pl | tee -a $log`;
alarm(1); # Somehow alarm(0) cause the prgram to get stuck
$i++ while 1;
};
print "out from eval\n";
if ($@) {
if ($@ =~ /died in SIG ALRM/) {
$ps = `ps -ef |grep user |grep time.pl |grep -v grep|grep -v nedit |grep -v "tee" |tr -s " " " " | cut -f2 -d " " `;
chomp $ps;
print "ps = $ps\n";
if($ps ne ""){
if(kill 0 => $ps){print "process : $ps is alive\n";}
kill 1 , $ps;
sleep(1);
if(kill 0 => $ps){print "process : $ps is alive\n";}
elsif($! == EPERM)
{
print "process $ps has escaped my control\n";
}
elsif($! == ESRCH)
{
print "prosess : $ps has deceased\n";
}
else
{
print "Odd: Icouldn't check the status of $c_pid\n";
}
}
}
else {
print $@;
}
}
}
# 2  
Old 07-27-2008
These lines look wrong:

f(kill 0 => $ps){print "process : $ps is alive\n";}
kill 1 , $ps;
sleep(1);
if(kill 0 => $ps){print "process : $ps is alive\n";}

the "=>" operator should be ">=" to compare values. "=>" is the comma operator that is used for a different purpose.

I know there are modules you can use to check pids but I don't know if they are anymore efficient than your method. Hopefully someone else will have a suggestion.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to include a text pattern in system function in PERL

Pleeeeease help.. I'm working in perl i have a system function to check whether a file is readable for others or not. i just want to print a text in that command my command is : system ("ls -la $filename | awk '\$1~ /^-......r../ {print \$9}'"); i know for displaying text in awk... (6 Replies)
Discussion started by: shubhamsachdeva
6 Replies

2. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

3. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

4. IP Networking

The system function gethostbyname() failed to find the client's host name

As we are facing issue with this server connection. The error is: The system function gethostbyname() failed to find the client's host name. how can i check if the server "server1" is able to resolve the client hostname (hosts / dns)? i can ping the client from server. any... (1 Reply)
Discussion started by: jinslick25
1 Replies

5. Shell Programming and Scripting

A question about the PID of a background function

Dear all, I'm writing a KornShell script that calls inside it a function in background mode #!/bin/ksh function myfunction { . . .} myfunction |& . . . How can I capture the PID of the function myfunction that runs in background? Thanks in advance :) (2 Replies)
Discussion started by: dariyoosh
2 Replies

6. Shell Programming and Scripting

how to find status of last executed cmd in perl?

In shell we can find the status of last executed command by $? In perl what is the command to find the status of last executed command... Can any one please say??????????????? Thanks, Prabhu (1 Reply)
Discussion started by: prsampath
1 Replies

7. Solaris

rsh commands not getting executed from Solaris 10 System to AIX System

Hi Friends, I am trying to execute rsh commands from Solaris 10 system to AIX system. When I give; Solaris10# rsh <hostname> ls -l , it gives me an error rshd : 0826-826 The host name for your address is not known At the same time, Solaris10# rsh <hostname> ---- gives me remote shell of... (25 Replies)
Discussion started by: jumadhiya
25 Replies

8. Programming

How to get system() function executed cmd return value ?

Hi, How I can get system function executed command return value ? I want to know mv command success or not ? #include <stdio.h> main() { int ret; ret = system( "mv x.dat y.dat" ); printf( "system ret:\n", ret ); } (3 Replies)
Discussion started by: haiudhaya
3 Replies

9. Shell Programming and Scripting

Perl system ssh find question

I have the following perl code that I use to run backups on remote machines. No problems here, the code works without problems system "ssh", $SHORT_NAME, "cd /;", "sudo", "tar", "cvf", "-", "--exclude=/dev", "--exclude=/proc", "/|", "netcat", "-w2", "troll", "2011" ; The code is used for full... (3 Replies)
Discussion started by: thumper
3 Replies

10. UNIX for Dummies Questions & Answers

FIND function - system wide

Hi, I have a task to search for a file called 'Xstartup' in the whole system because there might be different versions of it which overrite eachother. Can anyone suggest a smart command to run this search ? The machine needs to scan every single folder beginning from root. Please help, I am... (5 Replies)
Discussion started by: DGoubine
5 Replies
Login or Register to Ask a Question