[Novalug] bash scripting

Nino Pereira pereira@speakeasy.net
Sat Jun 2 14:11:36 EDT 2007


Don,

I don't quite understand it just yet, but I really like
the way you comment the commands, so that one could follow
what's going on with a little more study.

This is the type of answer that helps!

Thank you,

Nino


donjr wrote:
> On Sat, 2007-06-02 at 09:47 -0400, Ben Creitz wrote:
>>> I needed nohup because i want to launch the command on a SSH session and
>>> log off later and get the result by email later. Does it make sense?
>>> Maybe I'm missing something here...
>> Yeah, that makes perfect sense.  What you want to do is wrap the
>> *whole* thing in nohup and send the *whole* thing to the background.
>> I think the simplest thing to do is just manually type "nohup
>> myscript.sh &", where "myscript.sh is something like:
>>
>> mycommand | mail -s "done" me@mine.com
>>
>> .... and log out of your session.  I know you wanted to avoid that, but
>> I think any other answer is unnecessarily complex, because then you're
>> essentially talking about a script that sends itself into the
>> background.  It is easier (using &) to send something to the
>> background from "outside" (i.e. a parent process).  If somebody can
>> post an example of a script that nohup's and "backgrounds" itself
>> totally, and completely under its own power**, please post it!  I
>> don't have time to think right now (my Jetta finally bit the dust, and
>> it is minivan time).
>>
>> As somebody else said, if you want to avoid the extra typing you can
>> set an alias!
>>
>> Ben
>>
>> ** in Solaris you can nohup an already-running process by giving the
>> PID, so that would be an approach.  Not sure about Linux (maybe
>> depends on shell??)
> 
> 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.
> 
> --  



More information about the Novalug mailing list