Script that runs a command with 'exec' doesnt end until 'Enter' is pressed

0

We have a KSH script that runs a process using exec and hangs.
We want to be able to run it automatically and keep it running in background.

We tried this:

./myScript.ksh &

However this left us waiting in "mid air", and we still need to press on the Enter key to continue.

The only solution we found was to change the actual script to look like this:

....
exec <some command> &
exit 0

Without both & and the exit 0 it did not work.

Any better solution?

RonK

Posted 2011-08-04T08:55:49.113

Reputation: 1 280

Answers

1

I have difficulties in accepting your first example where you start the job in the background and you say it's leaving you in "mid-air" and you have to press [Enter] to continue.

Note that any output the background script produces will be printed after the shell prompt - giving the illusion of a missing prompt (in fact the shell prompt is already there, but scrolled up). You don't have to press [Enter] to enter the next command!

The 2nd example (exec ... &) makes no sense at all: The ampersand instructs the shell to start the command as a parallel background process, whereas the exec instructs the shell to start no new process at all and execute the new command "recycling" the current process. Technically spoken (very simplified): The code of the shell program will be replaced by the code of the called program.

Conclusion:

  • You don't have to press [Enter] (but it does not harm if you do so)

  • Since the shell script is started as a parallel task anyway, the ampersand may be omitted.

  • To use exec makes absolutely sense, if the command to be started is the last one to be executed by the script anyway, so sacrifying the Shell saves one otherwise useless process.

[added:] Just one thing came to mind why you may think you have to press [Enter]:

While the shell process is displaying the command prompt (awaiting your next command) there will be no notification about finished background processes at all.

You'll only get the notification when the shell displays the next command promt. This also may lead to the illusion that 'one has to press [Enter] to allow a background process to terminate'.

ktf

Posted 2011-08-04T08:55:49.113

Reputation: 2 168