Oracle FAQ Your Portal to the Oracle Knowledge Grid

Home -> Community -> Mailing Lists -> Oracle-L -> RE: OT:UNIX: Anyway to run this in the background and return to ca

RE: OT:UNIX: Anyway to run this in the background and return to ca

From: <>
Date: Fri, 22 Aug 2003 17:39:25 -0800
Message-ID: <>

Here's a rather standard way of doing a fork in Perl.

Also a nice little daemon. Works very well for Oracle utilities.


> package Daemon;
> use strict;
> use Carp;
> use POSIX;

> sub daemon {
> my($tries, $pid, $sess_id);
> {
> if ($pid = fork) { exit 0 } # exit parent
> if (defined($pid )) {

         close STDOUT;

> close STDIN;
> chdir('/');
> croak "Cannot detach from controlling terminal" unless $sess_id
= POSIX::setsid();
> return $pid;
> }
> if (++$tries>5 ) {
> die "fork failed after $tries attempts: $!\n"
> } else {
> sleep 3;
> redo;
> }
> }


"Matthew Zito" <>
Sent by:
 08/22/2003 02:44 PM
 Please respond to ORACLE-L  

        To:     Multiple recipients of list ORACLE-L <>
        Subject:        RE: OT:UNIX: Anyway to run this in the background and return to ca


Errrm - this script doesn't do what I think you're trying to do. Perl starts, it forks a child process, the parent process "does a bunch of stuff", but the child process only runs ps once. Soooo, you end up with a zombie child process (until the parent is done with its business) and a parent process that is doing all the work.  

A better solution would be to reverse the ps and the "doing stuff". Let the child process do the dirty work and let the parent focus on either generating more children or status monitoring the existing children. You could use a loop that watches for the existence of a particular pid or has a set condition and add a SIGCHLD handler that will handle cleanup of the child and remove the loop condition.  


Matthew Zito
GridApp Systems
Cell: 646-220-3551
Phone: 212-358-8211 x 359 
-----Original Message-----
Sent: Friday, August 22, 2003 2:04 PM
To: Multiple recipients of list ORACLE-L

use strict;
use bytes;
my $pid=0;
if (!defined ($pid=fork())) { die "Problem with cutlery:$!\n"}
elsif ($pid) { print "Do stuff here\n";
               print "Cutlery is back\n";
else {
         open(SPY,"ps -ef|grep sqlplus|");
         while (<SPY>) {    print;}
Mladen Gogala
Oracle DBA 
-----Original Message-----
Sent: Friday, August 22, 2003 1:25 PM
To: Multiple recipients of list ORACLE-L

$ORACLE_HOME/bin/sqlplus "/ as sysdba"  << EOD 
select ...... 
echo "this is a test" 
ps -ef|grep sqlplus 
I wish to put that into a unix command but at the same time run it in the 
background and return back to the parent script to do some other stuff?
Anyone done this? 
This message is for the named person's use only.  It may contain 
confidential, proprietary or legally privileged information.  No 
confidentiality or privilege is waived or lost by any mistransmission.  If 
you receive this message in error, please immediately delete it and all 
copies of it from your system, destroy any hard copies of it and notify 
the sender.  You must not, directly or indirectly, use, disclose, 
distribute, print, or copy any part of this message if you are not the 
intended recipient. Wang Trading LLC and any of its subsidiaries each reserve the right to monitor all e-mail 
communications through its networks.  Any views expressed in this message 
are those of the individual sender, except where the message states 
otherwise and the sender is authorized to state them to be the views of 
any such entity.

Please see the official ORACLE-L FAQ:

Fat City Network Services    -- 858-538-5051
San Diego, California        -- Mailing list and web hosting services
To REMOVE yourself from this mailing list, send an E-Mail message
to: (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
Received on Fri Aug 22 2003 - 20:39:25 CDT

Original text of this message