Execution of bash script inside a bash script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m6s
    New Member
    • Aug 2007
    • 55

    Execution of bash script inside a bash script

    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...
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.

    Comment

    • m6s
      New Member
      • Aug 2007
      • 55

      #3
      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!!

      Comment

      • m6s
        New Member
        • Aug 2007
        • 55

        #4
        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.

        Comment

        Working...