Continuing execution of a script after using "exec program" from within it.

Continuing execution of a script after using "exec program" from within it.

am 07.09.2005 13:01:29 von MarkG

All,

This message leads on from the following thread....
http://groups.google.com/group/comp.unix.shell/browse_frm/th read/125f3adad090ed26/b2c14743235aee2c?q=grimshaw&rnum=1#b2c 14743235aee2c

I have a script that invokes a program using 'exec', the idea being
that the program can be killed in another session by another
script/program. This has been working fine for some time now.
The essential elements of the script are: -

echo $$ > .pidlog
exec program

this allows the pid of the script to be used to kill the program.

However, I now require the ability to write logging information,
specifically, an end date/time to a file called .joblog *AFTER*
'program' has completed. Currently, any subsequent commands after the
program has completed don't get executed which makes complete sense to
me. What are my options, if any?

Thanks,
Mark.

Re: Continuing execution of a script after using "exec program" from within it.

am 07.09.2005 15:11:37 von Icarus Sparry

On Wed, 07 Sep 2005 04:01:29 -0700, MarkG wrote:

> All,
>
> This message leads on from the following thread....
> http://groups.google.com/group/comp.unix.shell/browse_frm/th read/125f3adad090ed26/b2c14743235aee2c?q=grimshaw&rnum=1#b2c 14743235aee2c
>
> I have a script that invokes a program using 'exec', the idea being
> that the program can be killed in another session by another
> script/program. This has been working fine for some time now.
> The essential elements of the script are: -
>
> echo $$ > .pidlog
> exec program
>
> this allows the pid of the script to be used to kill the program.
>
> However, I now require the ability to write logging information,
> specifically, an end date/time to a file called .joblog *AFTER*
> 'program' has completed. Currently, any subsequent commands after the
> program has completed don't get executed which makes complete sense to
> me. What are my options, if any?

In your original posting you said that you wanted the PID in a file so you
could kill the job. Can you do something like this

script1
#!/bin/sh
../script2
date > .joblog

script2
#!/bin/sh
echo $$ > .pidlog
exec program

then start your program as 'script1'?

Re: Continuing execution of a script after using "exec program" from within it.

am 07.09.2005 17:35:53 von Joe User

On Wed, 07 Sep 2005 04:01:29 -0700, MarkG wrote:

> I have a script that invokes a program using 'exec', the idea being
> that the program can be killed in another session by another
> script/program. This has been working fine for some time now.
> The essential elements of the script are: -
>
> echo $$ > .pidlog
> exec program
>
> this allows the pid of the script to be used to kill the program.
>
> However, I now require the ability to write logging information,
> specifically, an end date/time to a file called .joblog *AFTER*
> 'program' has completed. Currently, any subsequent commands after the
> program has completed don't get executed which makes complete sense to
> me. What are my options, if any?

You need some process alive after the program terminates to log its'
termination.

So, don't use exec, do something like:

program &
echo $! >.pidlog
wait $!; echo "Terminated before $(date)" >>.joblog ; exit

or:

{ echo $$ > .pidlog; program ; \
echo "Ended before $(date)" >>.joblog ; } &
exit

or, write one of the above into a script file, and exec the script file.

The suggested methods were not tested. I use bash.

Re: Continuing execution of a script after using "exec program" from within it.

am 08.09.2005 17:35:11 von MarkG

Thanks for the replies. I have opted to use the solution proposed by
Icarus which I have tested and works.