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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

started background process

Status
Not open for further replies.

razalas

Programmer
Apr 23, 2002
237
0
0
US
Am not getting the e-mail with output from script (NIGHTLY-PROCESSING.s) run from cron. Suspect another script (LISTEN-PTS.s) started as background task from cron script is holding things up. It's as if the script started in background is holding up the cron script from finishing. If I manually stop the background script, then the cron script appears to terminate and I get the e-mail with output messages.

Does this make sense? Is it ok to start a script as a background task from within a script run from cron? Do I have to do something special to let the first script terminate normally after starting the background task?

Script started by cron (NIGHTLY-PROCESSING.s)
Code:
#
#  Start "nightly processing" Cobol driver, SY-010
#
   runcobol SY-010
#
#  Send a copy of nightly tar backup to development server
#
   if  [ "$HOSTNAME" = "mgmt" ]
   then
       ftp -n -v 172.16.20.1 < FTP-NIGHTLY > FTP-BACKUP-LOG
   fi
#
#  Restart "Listener" scripts for Lucas Voice Systems results files
#
   if  [ -x LISTEN-PTS.s ]
   then
       ./LISTEN-PTS.s &
   fi
#
#  End of NIGHTLY-PROCESSING.s
#

Background started script (LISTEN-PTS.s)
Code:
if  [ -e PTS.log ]
then
    echo "Picking Listener may already be in progress..."
    echo "Please check existing PTS.log!"
    echo "If no longer active, please remove PTS.log before restarting..."
else
    echo "Initializing PTS.log..."
    echo "Picking Results Queue Processing Log" >  PTS.log
    echo "Initialized: " `date +"%D %T"`              >> PTS.log
    echo "By:          " `id`                         >> PTS.log
    echo "On:          " $HOSTNAME "PID #" $$         >> PTS.log
    echo "Starting..."
    while [[ -e PTS.log ]];
    do
        echo "Processing: " `date +"%D %T"` >> PTS.log
        find voicedata -name "LUC*.PTS" -exec runcobol OE-001P -k -a "{}" \;
        sleep $INTERVAL;
    done;
fi;



Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
You may try this:
if [ -x LISTEN-PTS.s ]
then
./LISTEN-PTS.s &
kill -9 $$
fi

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps, your LISTEN-PTS.s will never end if PTS.log doesn't exist.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
On some operating systems, I have seen the following:
When you are running an 'interactive shell' and run a script in background and then try to exit, you get the warning:
You have running jobs
and the shell doesn't exit. This is because the 'background script' is a child process of the 'interactive shell', stop the shell and there would be an orphan process. This scenario (but from cron) might explain what is happening.

You could try the nohup command, see the man pages, to launch LISTEN-PTS.s. as
nohup ./LISTEN-PTS.s &

I hope that helps.

Mike
 
Mike,

I figure that my issue is something along the lines you suggest, but I read somewhere in the man pages, that tasks started in background are automatically "nohup". So I am confused. I guess it wouldn't hurt to try adding the nohup and see what happens.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Mike,

I tried adding the nohup, but it didn't seem to help.

Code:
   if  [ -x LISTEN-PTS.s ]
   then
       nohup ./LISTEN-PTS.s >> 00-EVENT-LOG &
   fi

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Try redirecting both stdout and stderr of LISTEN-PTS.s to a file and see if that makes a difference; cron may be waiting for them both to be closed before it sends the output of the cron job to you.

Annihilannic.
 
I tried adding the nohup, but it didn't seem to help.
Hmmm ...
Can you check the PID and PPID of your processes at run time and let us know what processes are 'parent' of your processes (and any 'parents' of those, if they exist).

What OS (& version) and hardware are you using?

Have you posted the entire scripts (or just the edited highlights where the issues are)?

Regards.

Mike
 
Razalas, what about my post timestamped 7 Mar 09 23:39 ?
 
Is your script setting up the environment (directory and PATH) that Cron requires? I don't see it, but maybe you are only showing a portion of the code....



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Mike042,

[dsalazar@mgmt sales]$ uname -a
Linux mgmt 2.6.9-55.0.6.ELsmp #1 SMP Thu Aug 23 11:13:21 EDT 2007 x86_64 x86_64
x86_64 GNU/Linux

And, yes, I did only post the tail end of the scripts that are the relevant portions, but...

I think Annihilannic is on the right track. We were having trouble (wouldn't stay running in background this way) starting this manually from the command line as a background task, but that cleared up once we started redirecting both stdout and stderr.

Unfortunately, for operational reasons, we took this out of the cron script and were just starting it manually. So I don't know if simply redirecting both stderr and stdout would have cleared up my original problem (not getting e-mail from cron). Hopefully, when things quiet down here, I'll try putting it back in.

At the moment, we are set up to start the LISTEN-PTS.s script directly from cron. Although that doesn't seem to be working either. So, we are back to the manual starts.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
PHV,

regarding post from 7 Mar 09 23:39, you have it backwards.

The script runs as long as the PTS.log file exists. It stops when the PTS.log is removed.

But it will only run if PTS.log does not exist when you start.
If you start the script and a PTS.log exists, you get a warning and the script terminates immediately.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
PHV,

my apologies. I replied reference the wrong post.[blush]

So you are suggesting forcibly killing the starting process (NIGHTLY-PROCESSING.s script), since it is not terminating normally. I'll see if I can go back and try that, but it'll be awhile. I'll report back once I can try it.



Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top