develooper Front page | perl.beginners | Postings from February 2002

Help ** Getting the pid of a background process **

From:
Groove Salad
Date:
February 27, 2002 01:12
Subject:
Help ** Getting the pid of a background process **
Message ID:
0961A17F-2B62-11D6-BC3B-0030657E219C@mac.com
Greetings all,

I am trying to assign the results of a background process to $variable. 
I can't seem to get the correct syntax, could one of you good people 
assist?
Below is a snippet of what I have ( running on Solaris 2.6 ), I know 
there are more elegant ways to do this. I'm open to any suggestions.

I want the results of the ( echo $! ) to be assigned to 
( $save_this_pid ) and returned,  so I can do other processing like 
create a pid file.

$pid = &run_sh_commands("[ -f $logfile ] && mv $logfile $logfile.$time > 
/dev 2>&1",
						"echo \"## $0: restarted on `date`\" >> $logfile",
						 "$appdir{$os}/$app_name{$port}->{name} 
$app_options >> $logfile 2>&1 &",
						 "echo \$!")
						 unless ($DEBUG_MODE);
......

sub run_these_sh_commands  {

my @commands = @_;
open(PIPE, "|sh") || die;

	foreach $command (@commands)
	{
		if ( $command =~ /(echo\s\$!)$/
		{
			$save_this_pid = ( something would go here but I don't know what )
			# otherwise, the pid number gets printed to the screen and 
I want to save it.
		{
		  else
		{
			print PIPE "$command\n";
		}
		return($save_this_pid);
	}
close(PIPE);
}


Thanks,

gS



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About