inbuilt nohup logic into the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting inbuilt nohup logic into the script
# 1  
Old 03-15-2010
inbuilt nohup logic into the script

Hi All,

I know one way to use nohup as below:

Code:
nohup myscript.ksh

However, just wanted to know is there any way to write the nohup logic directly into the script so that we can execute the script without need to specify the nohup like this:

Code:
myscript.ksh

Many Thanks in advance.
# 2  
Old 03-15-2010
This might do what you want:
Code:
mynohup(){
# Close stdin, and make any read attempt an error
    if [ -t 0 ]
    then
        exec 0>/dev/null
    fi

# Redirect stdout to a file if it's a TTY
    if [ -t 1 ]
    then
        exec 1>nohup.out
        if [ $? -ne 0 ]
        then
            exec 1>$HOME/nohup.out
        fi
    fi

# Redirect stderr to stdout if it's a TTY
    if [ -t 2 ]
    then
        exec 2>&1
    fi

# Trap the HUP signal to ignore it
    trap : HUP
}

Save it in a file, source that in any script you want to ignore SIGHUP in, call the mynohup function (or whatever you want to call it) once, and you're good to go. Tested with ksh and bash, and modeled after the nohup shipped with the GNU coreutils.
# 3  
Old 03-16-2010
Thanks for the reply Pludi. I have implemented the solution.
# 4  
Old 03-16-2010
Quote:
Originally Posted by pludi
This might do what you want:
Code:
mynohup(){
# Close stdin, and make any read attempt an error
    if [ -t 0 ]
    then
        exec 0>/dev/null
    fi

# Redirect stdout to a file if it's a TTY
    if [ -t 1 ]
    then
        exec 1>nohup.out
        if [ $? -ne 0 ]
        then
            exec 1>$HOME/nohup.out
        fi
    fi

# Redirect stderr to stdout if it's a TTY
    if [ -t 2 ]
    then
        exec 2>&1
    fi

# Trap the HUP signal to ignore it
    trap : HUP
}

Save it in a file, source that in any script you want to ignore SIGHUP in, call the mynohup function (or whatever you want to call it) once, and you're good to go. Tested with ksh and bash, and modeled after the nohup shipped with the GNU coreutils.
I found this thing very useful. Thanks for the info.
just one query, is there any side effects if someone executes the script explicitly with nohup having this logic inside the script.
# 5  
Old 03-16-2010
Haven't explicitly tested it, but there shouldn't. After all, the external nohup utility should make sure that the default FDs aren't connected to a terminal anymore, and that SIGHUP is ignored.
# 6  
Old 03-16-2010
i have just tried but i think its not working with external nohup command alongwith.

Code:
. nohup_file
mynohup

while :
do
echo running
sleep 1
done


when I run this, normally (without external nohup) its working fine and the stdout has been appending in the nohup.out as expected.

but when I run this with,
Code:
nohup ./script.ksh &

nothing happens. nohup.out file is zero size.


Is there any way ( some checks ) so that we can detect the external nohup and ignore anyone of that (external or internal). so that it works in all cases..
or any other solution to make it generic.

the reason for asking this is even if I put the logic inside the script and some people may unaware of that and may execute the script with external nohup.

thanks.
# 7  
Old 03-16-2010
What platform are you on? Because I couldn't confirm that behavior, neither on Linux nor HP-UX, neither with bash nor ksh.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Saving nohup output to a file other than nohup.out

Shell : bash OS : Oracle Linux 6.4 I want to save the ouput of a nohup command to file other than nohup.out . Below are my 3 attempts. For both Attempt1 and Attempt2 , the redirection logs the output correctly to the output file. But I get the error "ignoring input and redirecting stderr to... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Solaris

inbuilt modem problem

when I use the on-board modem.........and I dial in I get the login prompt when I enter id and password it gives..........invalid login.... i hv sun fire 280R system pls help me urgently........i m frustated thx girish (3 Replies)
Discussion started by: girish_shukla
3 Replies
Login or Register to Ask a Question