Connecting Tech Pros Worldwide Forums | Help | Site Map

Execution of bash script inside a bash script

m6s's Avatar
m6s m6s is offline
Member
 
Join Date: Aug 2007
Location: GR
Posts: 49
#1: May 20 '08
Hello to all,
I have this code :

echo "$SERVICE is not running" >> /var/log/messages
START=$(date +%s)
exec '/home/m6s/projects/services/runtest.sh'
END=$(date + %s)
echo $END
DIFF=$(( $END - $START ))
echo $DIFF
echo "$SERVICE, took $DIFF seconds" >> /var/log/messages

It takes the time it started, it tries to executed the runtest.sh, and then it is supposed to return back to the main program after the execution take the END, calculate the DIFF and report the result to /var/log/messages.

But when the second program terminates, it terminates entire script. At least is seems so. What is wrong with this snippet?

Thank you in advance...

Expert
 
Join Date: Sep 2007
Posts: 856
#2: May 20 '08

re: Execution of bash script inside a bash script


That's because exec kills the current process and replaces it with the other. Use backticks to do this, eg `sh scriptname.sh`. Using backticks also allows you to capture any input that comes from the commands inside.
m6s's Avatar
m6s m6s is offline
Member
 
Join Date: Aug 2007
Location: GR
Posts: 49
#3: May 20 '08

re: Execution of bash script inside a bash script


Quote:

Originally Posted by Laharl

That's because exec kills the current process and replaces it with the other. Use backticks to do this, eg `sh scriptname.sh`. Using backticks also allows you to capture any input that comes from the commands inside.

You were right, wrong of me to use this exec.It returns now back to the script, but END has never the variable of date... ?!? So diff takes only the -START!!
m6s's Avatar
m6s m6s is offline
Member
 
Join Date: Aug 2007
Location: GR
Posts: 49
#4: May 20 '08

re: Execution of bash script inside a bash script


Quote:

Originally Posted by m6s

You were right, wrong of me to use this exec.It returns now back to the script, but END has never the variable of date... ?!? So diff takes only the -START!!

It was a space in +s :-)
Ok, thank you very much for the help about exec.
Reply


Similar Unix / Linux / BSD bytes