f



Can a script nohup itself?

Is it possible to have a script nohup itself in the Bourne shell? I
know there are work-arounds (multiple scripts being the most obvious)
but I'm curious if I can do this self-contained.

I believed (foolishly as it turns out) that I might be able to create
a function and then nohup my_function & to get the desired effect, but
it doesn't work that way since nohup only accepts commands or
programs, not functions, as a parameter. Anyone have a suggestion?
0
JustBob4Now
6/23/2004 8:59:29 PM
comp.unix.shell 15484 articles. 3 followers. Post Follow

6 Replies
7156 Views

Similar Articles

[PageSpeed] 21

JustBob4Now@hotmail.com wrote:
 > Is it possible to have a script nohup itself in the Bourne shell? I
 > know there are work-arounds (multiple scripts being the most obvious)
 > but I'm curious if I can do this self-contained.
 >
 > I believed (foolishly as it turns out) that I might be able to create
 > a function and then nohup my_function & to get the desired effect, but
 > it doesn't work that way since nohup only accepts commands or
 > programs, not functions, as a parameter. Anyone have a suggestion?

# Untested:
if [ -s /tmp/foo ]; then
   : Do useful stuff here
else
   echo $$ > /tmp/foo
   nohup $0 "$@" &
   wait
   rm /tmp/foo
fi

-- 
Kevin Rodgers

0
Kevin
6/23/2004 9:59:58 PM
JustBob4Now@hotmail.com wrote:

> Is it possible to have a script nohup itself in the Bourne shell? I
> know there are work-arounds (multiple scripts being the most obvious)
> but I'm curious if I can do this self-contained.
> 
> I believed (foolishly as it turns out) that I might be able to create
> a function and then nohup my_function & to get the desired effect, but
> it doesn't work that way since nohup only accepts commands or
> programs, not functions, as a parameter. Anyone have a suggestion?

Sure. 

I'm assuming you want to run your script with 'nohup' as a background job.
You can have your script recursively call itself with: 
        
        nohup "$0" "$@" &

However, make sure you have some *termination condition* that you check for,
else your script will recurse forever. For example, you might pass a
command-line parameter stating if your script was called by itself:
        
        #!/bin/bash

        if [ "$1" != "calling_myself" ]
        then
                # this script has *not* been called recursively by itself

                nohup "$0" "calling_myself" "$@" &
                exit
        else
                # this script has been called recursively by itself

                shift # remove the termination condition flag in $1
        fi

        # the rest of your script goes here

0
foo
6/23/2004 10:06:10 PM
JustBob4Now@hotmail.com wrote:
> Is it possible to have a script nohup itself in the Bourne shell? I
> know there are work-arounds (multiple scripts being the most obvious)
> but I'm curious if I can do this self-contained.

I'm not sure if other flavors of Unix alow this but Solaris 9 and better let you nohup 
pids and gpids.  

So you could do a nohup -p $$

It's so ellagent, you'd think they would have thought of this a long time ago.

--Brett
0
foo
6/24/2004 3:28:35 PM
2004-06-23, 15:59(-05), JustBob4Now@hotmail.com:
> Is it possible to have a script nohup itself in the Bourne shell? I
> know there are work-arounds (multiple scripts being the most obvious)
> but I'm curious if I can do this self-contained.
>
> I believed (foolishly as it turns out) that I might be able to create
> a function and then nohup my_function & to get the desired effect, but
> it doesn't work that way since nohup only accepts commands or
> programs, not functions, as a parameter. Anyone have a suggestion?
[...]

You can do:

#! /bin/sh
{
  trap '' HUP
  # the script
} &

-- 
Stephane
0
Stephane
6/24/2004 3:39:46 PM
2004-06-24, 15:39(+00), Stephane CHAZELAS:
[...]
> #! /bin/sh
> {
>   trap '' HUP
>   # the script
> } &

Actually:

#! /bin/sh
{
  trap '' HUP
  # the script
} > nohup.out 2>&1 &

would be closer to nohup behavior.

#! /bin/sh
{
  trap '' HUP
  # the script
} < /dev/null > nohup.out 2>&1 &

would even be better.

-- 
Stephane
0
Stephane
6/24/2004 3:48:41 PM
In article <1088090586.458586@news.zipcon.net>,  <foo@bar.com> wrote:
>I'm not sure if other flavors of Unix alow this but Solaris 9 and better
>let you nohup 
>pids and gpids.  
>
>So you could do a nohup -p $$
>
>It's so ellagent, you'd think they would have thought of this a long time ago.

The disown command has been in ksh93 a long time ago.
But I find it is of little use if you do not put the
process ($$ in your example) in background.
-- 
Michael Wang * http://www.unixlabplus.com/ * mwang@unixlabplus.com
0
mwang
7/17/2004 7:27:32 PM
Reply: