The shell provides a lot of useful capabilities, and it is not very difficult to learn, or it is better to say "not more than C/C++ language". In these two language the user has to learn the language and also the compiler, and if he want to develop large projects it has to learn, also, an IDE.

Really the shell doesn't have the power of compiled languages, but it has a lot of flexibility, sometimes it is difficult to find some examples, but if you google a little you can find what you are looking for.

 

In C/C++ this task is very simple, take a look to the related posts. With the shell……

it is simpler!!

The first thing to say is that backgrounding the script won't demonize it.

Yes you really have to do some task to demonize your script:

  1. Disconnect stdin
  2. Disconnect stdout
  3. Disconnect stderr
  4. Ignore the hangup signal

the first three tasks will be implemented with the redirection

the last one will be implemented using the nohup tool.

 

from the Mac Man page

NOHUP

 

nohup -- invoke a utility immune to hangups

The nohup utility invokes utility with its arguments and at this time sets the signal SIGHUP to be ignored.  If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory.  If standard error is a terminal, it is directed to the same place as the standard output. Some shells may provide a builtin nohup command which is similar or identical to this utility.  Consult the builtin(1) manual page.

 

The above command will do the work, so let's start.

 

you can type the following command:

 

 

# nohup ./thescript.sh 0<&- &>/dev/null &

 
it works fine
 
if you want to capture the output in thescript.log file you can modify the previous command in the following one:
 

# nohup ./thescript.sh 0<&- &> thescript.log &

 
I found another strange way to do this job:
 
# (./thescript.sh &) & 

 

 

 

 
 
Otherwise to do something complex:
 

 

 

 

 
On Mac OS X you can use a launchd script for shell daemon.

 

 

 

 
On other Operating Systems, use your system's daemon facility, such as start-stop-daemon for linux. Take a look to the following page: http://manpages.ubuntu.com/manpages/karmic/man8/start-stop-daemon.8.html
 
enjoy the shell!
 
Gg1