Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl & Processes

Status
Not open for further replies.

jameslu

Programmer
Nov 21, 2002
8
0
0
GB
Hiya,

I need some Perl help!!

Basically, I'm running a Perl script on Unix which makes a call to 'nohup' and starts a process off in the Background. 'nohup' returns a process id number (pid) which is also being used by my script.

If I do a 'ps -efx' after calling 'nohup' I will see my command in the current process list. Simplified example:

12234 abcd /opt/app/weblogic/bin/startweblogic -servername=MyServer -domainname=MyDomain

'12234' is the process number and the remaining entries are the details of that current process.

What I need to do in Perl is to be able to perform a pattern match on the details of the process I have just submitted using 'nohup'. Is there something in Perl I can use to search for the details of all current processes, identify the process I have just submitted using the pid returned form 'nohup' and then pattern match the details of that pid entry?

Any help would be greatly appreciated!!

Thanks,

James

 
@rc=`ps -efx`
foreach $line (@rc) {
if ((index $line, $pid) != -1) {
($_pid,$res,$dir,$server,$domain)=split / /, $line;
}

Haven't tested it, but it looks good on paper
Paul
 
Thanks Paul.
Will give this a go.

Regards,

James
 
@rc=`ps -efx`
foreach $line (@rc) {
if ((index $line, $pid) != -1) {
chomp $line;
($_pid,$res,$dir,$server,$domain)=split / /, $line;
}
 
What are you actually trying to do? It looks as if you are reading info about all processes in order to get info about one. It also looks like you're reading all info about your process and my guess is you only want to know one thing, such as "Is it still running?" or a detail such as the process' owner or somesuch.

If I'm right, there are much easier ways to do what you are trying. One easy win is to ask [tt]ps[/tt] only about the process id in which you have an interest. [tt]ps[/tt] arguments vary between systems but [tt]ps -p $pid[/tt] works for me. You can also give many versions of [tt]ps[/tt] an option list to specify exactly which info you want.

Either way, you are still starting an extra process ([tt]ps[/tt] itself) and that is needlessly expensive if you are on Linux. The proc filesystem contains all the info about all running processes. Simply read "/proc/$pid/cwd", for example, to find it's working directory.

PS - rather than use nohup, the standard perl is

[tt]use POSIX 'setsid';

chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
if ( ! $pid ) {
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
exec( "background_program" ); # never returns
}
# parent carries on
[/tt]

I hope that this is helpful.

Yrs,


fish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top