SitefinderOracle and Sun
Secure Search

BigAdmin System Administration Portal
Community-Submitted Tech Tip
Print-friendly VersionPrint-friendly Version
This content is submitted by a BigAdmin user. It has not been reviewed for technical accuracy by Sun Microsystems, though it may have been lightly edited to improve readability. If you find an error or would like to comment on the article, please contact the submitter or use the comment field at the bottom of the article. Community submissions may not follow Sun trademark guidelines. For information on Sun trademarks, please see http://www.sun.com/suntrademarks/.
 
 

Automatically Saving Program Output With a nohup Script

István Bátori, August 2007


Introduction

This tip can help you save script output by using the nohup utility, which is intended to start in the background. You can use the script I provide below under "Installation." This tip corresponds to the tip Automatically Saving Script Output Using the tee Utility.

I have created tons of scripts for administrators, and I was interested in how script output can be saved in an easy, elegant way. Scripts can have their own logging functions, but the output also contains important information. Saving the output is typically solved by output redirection, for example:

nohup mynohupscript.sh >/var/log/myscript.log 2>&1

How do you feel about typing such a long command? Isn't it boring? It's a lot to type. And it's confusing to read the long, uninteresting ballast in an admin document, especially if the script has many arguments and options and the log file contains the start date.

The nohup.sh wrapper script simplifies the process and hides the nohup command from the user. You only need to insert the following line as the first line of your script:

#!/usr/bin/ksh /opt/bin/nohup.sh

That's all! The script is started automatically by nohup and the output is saved as well.

This method is usable for shell, Perl, Python, or Ruby scripts, and it is easy to extend for other script languages.

First, let me explain the trick I use.


Script File Execution

UNIX executes a script file according to its first line:

#! <exec_command> [<args...>]

The system invokes the first line and passes the script file name and the command-line arguments. <exec_command> needs to be an executable binary file. It cannot be a script file (although some Linux distributions support using a script file).

For example, the foo.sh script file starts with the following line:

#!/usr/bin/ksh

If you type this at the command prompt:

foo.sh /tmp 3 1

The following is invoked:

/usr/bin/ksh foo.sh /tmp 3 1

If foo.sh is started with this:

#!/usr/bin/ksh /opt/bin/nohup.sh

The following is invoked:

/usr/bin/ksh /opt/bin/nohup.sh foo.sh /tmp 3 1

nohup.sh gets the command-line parameters and it calls foo.sh.


nohup.sh Installation and Usage

Installation

Copy the nohup.txt file, changing the ".txt" suffix to ".sh", and save into the /opt/bin directory. Then give it execute rights.

cp nohup.sh /opt/bin
chmod 755 /opt/bin/nohup.sh
chown root:admin /opt/bin/nohup.sh

Usage

There are two different ways to use the wrapper script:

1. Start the wrapper script directly from the command line:

/opt/bin/nohup.sh <command> [<args...>]

For example:

/opt/bin/nohup.sh foo.sh

2. Put the following into a script as the first line:

#!/usr/bin/ksh /opt/bin/nohup.sh

For example:

cat >/tmp/nohup_test.sh <<EOF
#!/usr/bin/ksh /opt/bin/nohup.sh
# example script to demonstrate nohup.sh usage
default_cycles=3
default_sleeping=1
cycles=${1:-$default_cycles}
sleeping=${2:-$default_sleeping}
i=0
while [[ $i -lt $cycles ]]
do
  echo "$(date): $i"
  sleep $sleeping
  i=$((i+1))
done
EOF

Executing nohup_test.sh automatically calls nohup.sh, which starts the script with the nohup utility and redirects the script output into /var/tmp/nohup_test_<date>.log.


Configuration

Both usage methods have the same functionality. The functionality can be configured by shell variables, as follows:

  • nohup.sh starts the script with the interpreter specified in the $NOHUP_SHELL variable. If the variable is not set, the default interpreter is started according to the script file's extension:
    • A .sh script is started with /usr/bin/ksh.
    • A .pl script is started with /usr/bin/perl.
    • A .rb script is started with /usr/bin/ruby.
    • A .py script is started with /usr/bin/python.
  • Output is redirected into the directory specified by $NOHUP_LOGDIR. If $NOHUP_LOGDIR is not set, the /var/tmp directory is used.
  • The log file name is the script name postfixed with the date, for example, foo_2007_03_23_11_11_22.log.
  • Set NOHUP_MONITORING=yes to automatically start the monitoring of the command output with tail -f just after starting the script in the background. You can stop this monitoring process without stopping the nohup command.

Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this License.


BigAdmin
  
 
BigAdmin Solaris 10 Survey
 
Oracle - The Information Company