Make a script withdraw itself to the background

sbron 16 Aug 2005: In the Tcl chatroom Donal Fellows provided a method for a script to restart itself in the background. It goes something like this:

 if {[lindex $argv 0] ne "-dorealprocessing"} {
     set command [list [info nameofexecutable]]
     # A starpack doesn't use a script parameter on the command line
     if {![info exists ::starkit::mode] || $starkit::mode ne "starpack"} {
         lappend command [info script]
     eval exec $command -dorealprocessing $argv >/dev/null &
 # The real code goes here ...

This code simply restarts the shell that's executing the script in the background with one additional parameter and then exits. This should of course only be done when the additional parameter is not present to prevent an endless sequence of new processes.

CL notes that, with TclX, it's also possible for a process to send itself into the background, on the model of [recipe to appear later] ...

Simple way to startup as daemon mode without using fork from Expect or Tclx