[Novalug] bash scripting

donjr djr1952@hotpop.com
Sat Jun 2 15:21:50 EDT 2007


On Sat, 2007-06-02 at 14:24 -0400, Ben Creitz wrote:
> On 6/2/07, donjr <djr1952@hotpop.com> wrote:
> > The answer to your request for a simple shell script that runs a command
> > in the backgrounds and allows you to close the terminal where it was
> > started from is as follows:
> >
> >  #!/bin/sh
> >  trap "" 1   # Trap Hangup (usually logout) {ie what nohup does <GRIN>}
> >
> >  # Now run the command in the "background" while redirecting the
> >  # wrapping background shell's standard out and error to /dev/null
> >  ( ( mycommand | mail -s "done" me@mine.com )& ) >/dev/null 2>&1
> >
> >  exit 0  # end of the script.
> >
> > And yes all the command grouping '()' is required in order for it to
> > work correctly.
> 
> Ahhh, I guess I was thinking that a script whichs spawns a background
> process may not finish until its background process finishes.  I was
> wrong!
> 
> Ben

Take a look at most of the scripts in the directory /etc/init.d/
Almost all of these scripts start a command running in the background
and then exit.

The following two commands is more or less what a shell does each time a
command is invoked:

  ( command ) &     # execute the command
  wait $$           # and now wait for it to finish

When as a "user" you or a script executes a command in the "background"
the shell simply doesn't execute wait command after invoking the
command.

Note Background/Foreground is really a user terminal point of view
concept.


--  
-- 
 Don E. Groves, Jr. 

$ /usr/games/fortune : 
Future looks spotty. You will spill soup in late evening.



More information about the Novalug mailing list