LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2006, 08:55 PM   #1
JCHANX
LQ Newbie
 
Registered: Sep 2006
Posts: 2

Rep: Reputation: 0
how to run perl pgm in background??


Hi,

I've a perl pgm that calls another perl that could run about an hour. So I want the 1st perl to call 2nd perl to run in the background while I set some timer in the 1st perl to loop, say every 5 minutes to tell me the 2nd perl is still running. Any help would be appreciated.

-Jeff
 
Old 09-14-2006, 11:27 PM   #2
Zmyrgel
Senior Member
 
Registered: Dec 2005
Location: Finland
Distribution: Slackware, CentOS, RHEL, OpenBSD
Posts: 1,006

Rep: Reputation: 37
I've no idea about perl but generally on linux you can make process work on the background by issuing it with ampersand "commmand &".
Hope this helps.
 
Old 09-15-2006, 03:46 AM   #3
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 22
Hiya
I have to ask - why do you want to bother doing all that just to get prog1 to tell you prog2 is still running? If you execute prog2 in the foreground, then prog1 won't return until prog2 finishes, so there's nothing to do! You know prog2 is still running because prog1 is still running...
 
Old 09-15-2006, 09:19 AM   #4
eblack
LQ Newbie
 
Registered: Sep 2004
Location: Boston
Distribution: ubuntu
Posts: 3

Rep: Reputation: 0
The most common way would to be to fork the first program:

# If $pid is undefined, fork was not successful.
if( ! defined( $pid = fork ) ) {
return $pid;
} elsif( $pid ) {
# If $pid is greater than 0, this is the parent fork and
# $pid is the child's pid
# Create a second kid to kill the first if running too long
if( $type eq "LOG" || $type eq "MONITOR" ) { timeout( $pid ); }
return $pid; # i'm the parent
}


Then the first program can run some timeout stuff:

sub timeout {
my $sibling = shift;
my $pid;
my $i;

# If $pid is undefined, fork was not successful.
if( ! defined( $pid = fork ) ) {
return $pid;
} elsif( $pid ) {
# If $pid is greater than 0, this is the parent fork and
# $pid is the child's pid
return $pid; # i'm the parent
}

# Else if $pid is 0, this is the child fork
# Pause for awhile then check the other process
for( my $x = 0; $x < 10; ++$x ) {
# See if the sibling is still alive by sending kill 0 signal.
# If it's dead, kill yourself
exit unless ( kill 0 => $sibling );
# Pause a sec
sleep( 1 );
}

# See if the sibling is still alive by sending kill 0 signal.
# If it is still alive, kill it because the time is up
if( kill 0 => $sibling ) { $sibling = kill KILL => $sibling; }

exit;
}

Check the perldocs by typing 'perldoc -f fork'.

Hope this helps.
 
Old 09-15-2006, 02:33 PM   #5
JCHANX
LQ Newbie
 
Registered: Sep 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you ALL for the quick reply. I've tried with the "&" a bit with no luck yet. The reason for this is someone else is running the program. I want to give a job status every so often to anyone looking at the console. It is very easy for a person to enter a ctl-c to end the process if he/she thinks something got hung. Thank you eblack for the sample program. I'll give it a try.
 
Old 09-22-2006, 08:22 AM   #6
sidh4u
LQ Newbie
 
Registered: Feb 2006
Posts: 1

Rep: Reputation: Disabled
1. You can use perl system() function
someyhing like => system(/usr/bin/perl xyz.pl &); => for background running.
2. You can check the status in loop using
system(pidof xyz.pl) => If its giving some value that means its running else would be blank return

Good Luck.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C++: Way to have System run in background? dive Programming 3 08-17-2006 07:29 PM
how to run a process in the X background sh4d0w13 Linux - General 6 09-12-2005 02:54 PM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 12:27 AM
Run programs in background... Cybers19 Linux - Newbie 1 06-02-2004 03:54 AM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration