LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2006, 11:05 AM   #1
twistedpair
Member
 
Registered: Jan 2004
Posts: 71

Rep: Reputation: 15
perl ignore return codes


All,

I am making a system call in perl like this:

system ("/usr/bin/mycommad");

Without getting into specifics, I don't want the perl script that runs this to wait for a return code from "mycommand" before it continues with the rest of its code. I did some reading and saw something about %SIG{CHLD} and 'IGNORE' but I'm not sure if I am on the right track with that. Any ideas?

-Pair
 
Old 02-14-2006, 12:22 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
It's just waiting for the system() call to return. You might just be able to do
Code:
system("command &");
or use fork() to launch a separate process.
 
Old 02-14-2006, 01:17 PM   #3
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
You definitely want to use fork and exec for this. One idiom would be

Code:
my $pid = fork;
if ($pid) {

  # the parent process, $pid contains the child pid
  print "$$: I'm the parent, child is $pid\n";
  # continue with other stuff

} elsif (defined $pid) {

  # the child process, $pid is zero
  print "$$: I'm the child\n";

  # run the program you want to replace this process
  exec "command";

} else {
  die "Fork error: $!\n";
}
Note that $$ is the special variable that contains the current process pid. Remember to use wait or waitpid to reap your dead children, or you'll have zombies until the parent exits (morbid terminology . Also, if you want to control the input and output of the child process, look at IPC::Open2 or IPC::Open3. You can also open a socket or two before forking to do the IPC. Check out the Perl Cookbook for all this crap .
 
Old 02-14-2006, 02:47 PM   #4
twistedpair
Member
 
Registered: Jan 2004
Posts: 71

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by puffinman
You definitely want to use fork and exec for this. One idiom would be

Code:
my $pid = fork;
if ($pid) {

  # the parent process, $pid contains the child pid
  print "$$: I'm the parent, child is $pid\n";
  # continue with other stuff

} elsif (defined $pid) {

  # the child process, $pid is zero
  print "$$: I'm the child\n";

  # run the program you want to replace this process
  exec "command";

} else {
  die "Fork error: $!\n";
}
Note that $$ is the special variable that contains the current process pid. Remember to use wait or waitpid to reap your dead children, or you'll have zombies until the parent exits (morbid terminology . Also, if you want to control the input and output of the child process, look at IPC::Open2 or IPC::Open3. You can also open a socket or two before forking to do the IPC. Check out the Perl Cookbook for all this crap .
Thank you for all the tips. Wouldn't I not want the wait? Exec calls just a normal binary which is coded to just run then stop and I actually don't want the perl script to wait for that binary to return. Is the above code supposed to do that, or is it made specifically to wait (assuming I use "wait"?

BTW, I have tried the:

system ("\usr\bin\mycommand &");

command, and it didn't work.

Am I just completely misunderstanding?

-Pair
 
Old 02-14-2006, 03:41 PM   #5
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Well, if you don't care to track the progress of your child processes, then using wait would not be necessary. However, if you don't reap them when they are done, zombie processes will accumulate until the parent process exits. Read this for a detailed explanation and solutions for dealing with this problem.

Using the code I posted earlier, the parent process will not stop execution. Try the fork man page or read here for some info on process forking, it may make things a little clearer. That page talks about forks in C but it is very similar in perl, and forking is a feature of the operating system, not a particular language.
 
Old 02-14-2006, 04:54 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, for this idiom:
Code:
system ("\usr\bin\mycommand &");
what you need is to detach it from the calling proc as well ie
Code:
system ("nohup \usr\bin\mycommand &");
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PPP library & pppd, chat return codes ruchika Linux - Networking 1 03-08-2011 02:50 AM
mprotect() return codes tim_l Programming 2 05-09-2005 07:38 AM
return codes in c exvor Programming 4 01-21-2005 08:45 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
Tools to help read/understand perl codes?? friendklay Programming 0 04-21-2004 03:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration