A Program Which Generates a Script Which Kills It

I have a C++ program, running on Fedora Linux, which has to be able to update itself to a new version, which it can obtain from a server. The way I do this is to have it create a shell script which kills it (the parent process), uninstalls it, downloads the new version (actually it does this first), installs the new version, starts the new version in the background, and deletes itself (the shell script file). I create and run the shell script from the parent program by issuing system commands. I have done this successfully both with system() and popen(). Here is an example:

int i = system\("cd /tmp;/tmp/LTUpdateVersion &"\);

where UpdateVersion is the generated shell script. Here is the problem. Once the parent process is killed, any command in the script which would normally show progress on the screen, such as untarring files, or any echo comands, produce an error message like, "Write error: broken pipe."

How can I generate a shell script from a running process, which then kills the process, but is still able to echo messages to the screen?

Thanks.

Don't spawn a shell that kills you, just write(), close(), chmod() and execvp() the shell script, giving it the parent process to run the shell. Make sure the script starts with an accurate #! line per man execvp.

When the shell script gets to running the equivalent executable, it should 'exec' the new executable and so stop running, starting the new executable in that same process.