Sponsored Content
Top Forums Shell Programming and Scripting inbuilt nohup logic into the script Post 302404252 by clx on Tuesday 16th of March 2010 05:06:44 AM
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.
 

2 More Discussions You Might Find Interesting

1. 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

2. 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
NOHUP(1)                                                           User Commands                                                          NOHUP(1)

NAME
nohup - run a command immune to hangups, with output to a non-tty SYNOPSIS
nohup COMMAND [ARG]... nohup OPTION DESCRIPTION
Run COMMAND, ignoring hangup signals. --help display this help and exit --version output version information and exit If standard input is a terminal, redirect it from an unreadable file. If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use 'nohup COMMAND > FILE'. NOTE: your shell may have its own version of nohup, which usually supersedes the version described here. Please refer to your shell's doc- umentation for details about the options it supports. AUTHOR
Written by Jim Meyering. REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report nohup translation bugs to <http://translationproject.org/team/> COPYRIGHT
Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
Full documentation at: <http://www.gnu.org/software/coreutils/nohup> or available locally via: info '(coreutils) nohup invocation' GNU coreutils 8.28 January 2018 NOHUP(1)
All times are GMT -4. The time now is 08:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy