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

does not wait in the shell script

Status
Not open for further replies.

geffry

Programmer
Jun 26, 2002
33
US
i have a shell script which is like below

===================

A () {

calls some external commands and gets the output

}

B () {

sends mails using mailx command with output of the function A (which is file produced)

}

main () {

A
B

}


===========

my problem is before the completion of function A, function B is executed . I dont know how much time it will take to complete function A, so i could
not put a sleep time.

any help would be appreciated.
 
I am running this script in a cron job...
 
Try something like this then:

#!/usr/bin/ksh -p

A() {
echo "Executing function A....."
}

B() {
echo "Executing function B....."
}

MAIN() {
A
B
}

# Start script here.
MAIN
# EOS


If that doesn't work, then some sort of logic issue could be in function A. You might want to either step through function A manually or re-read what it is doing to make sure. If function A fails, should you go to function B? If not, then perhaps an exit was missed somewhere.
 
.....or function A contains an invokation of the background job that isn't being waited on to finish.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
in func A () , i am calling a command by `xyz...`
i guess it is running through the execution with out waiting for the external command to be completed.
 
That shouldn't be happening. Could you not just post your script to make this a little easier?
 
all the variables are defined....

I put the below script in cron job.


=======================================
OUTPUT=&quot;filea&quot;
DIR=&quot;/home&quot;
PROJECTS=&quot;mic&quot;

A()
{

for project in $PROJECTS
do
total=`$DIR/findx -p ${project}`
sleep 1
if test ${total} -ne 0;then

echo &quot; Project:$project &quot; >> ${OUTPUT}

for user in $USERS
do

$DIR/findx -d -p ${project} -s -u ${user} > /tmp/${user}_tmp

if test -s &quot;/tmp/${user}_tmp&quot;;then

${AWK} -f ${AWK_FILE} /tmp/${user}_tmp >> ${OUTPUT}

${RM} /tmp/${user}_tmp
fi
done
fi
done

${VI} ${OUTPUT} > /dev/null <<MSG
:1,\$ s/Description://g
:wq
MSG
}

Sendmail()
{
${MAILX} -s &quot;${SUBJECT}&quot; ${MAILID} < ${OUTPUT}
}

main()
{

A
Sendmail

}

main


=======================================
 
And doesn't the
Code:
 /home/findx
command run in background ?

Hope This Help
PH.
 
If that is running in the background, then your script doesn't know that, it keeps going forward. There is the logic error. So while you have the findx running, your script cannot continue.

Perhaps using a process status check and sleeping for x seconds would help while it finds the command findx running.
 
Just putting a &quot;[tt]wait[/tt]&quot; command after anything that runs as a subprocess with cause it to wait until it finishes before going on.

Hope this helps.

 
&quot;wait&quot; command does not help either...

From the cron job, i run a script which call another command (executable that connect to database and gets the result ). before this command finishes the send mail gets executed.

How do i stop the flow ?? if i run the script at command prompt everything works fine. but only as a cron job it fails.
 
make sure your function 'A' has all the neccessary paths and variables set correct and is not relying on any environment variables.

One of reasons why your 'Sendmail' gets executed incorrectly might be that your function 'A' actually fails to execute under cron (for whatever reasons mentioned above) and function 'Sendmail' actually 'sends over' output file (empty, half-baked etc) as a result of function 'A' simply failing.

I'd suggest debugging function 'A' first.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Append function B into function A, is it possible?

tikual
 
The executable i call from the script, losses some environment when i run thru cron job. How do i keep the environment variable when i launch the executable from the script.

is there any 'set' command i have to run..
 
Incorporate the necessary environment variables in front of the script runned by cron.
One way to do this in vi is
Code:
:r !env

Hope This Help
PH.
 
You could use

a && b

to run then in order

b will only run if a runs ok

Mike

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top