#!/usr/bin/ksh # INSTALLATION: # Copy this script as /opt/bin/nohup.sh and give execute rights # # See article at # http://www.sun.com/bigadmin/content/submitted/save_with_nohup.jsp # # CONFIGURATION: # Script is started with the interpreter specified in $NOHUP_SHELL # variable. If variable is not set, the default interpreter is started # according to the script file's name extension: # - *.sh started with /usr/bin/ksh # - *.pl started with /usr/bin/perl # - *.rb started with /usr/bin/ruby # - *.py started with /usr/bin/python # output is logged into the /var/tmp directory; export the # NOHUP_LOGDIR variable to change the directory # set NOHUP_MONITORING=yes to monitor 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 # # USAGE: # There are 2 different ways: # 1. starting from command line # nohup.sh # 2. put into a script as the first line # #!/usr/bin/ksh /opt/bin/nohup.sh # nohup.sh starts automatically by calling the script and # executes the original script in the background # # Both ways have the same functionality: # - the corresponding interpreter is started according to the file # extension or the $NOHUP_SHELL variable # - output is logged into $NOHUP_LOGDIR or if it is not specified # into the /var/tmp # - log file is the command or script name postfixed with date # e.g: mynohupscript_2007_03_23_11_11_22.log # # Example: # uncomment, save following lines and execute to see how it works # #!/usr/bin/ksh /opt/bin/nohup.sh # # 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 signal_handler() { kill $TAIL_PID echo " --- $LOGFILE monitoring has stopped --- --- background process is running yet, pid: $PID" ps -fp $PID | tail -1 exit 0 } filename=`basename $1` if [[ -z $NOHUP_SHELL ]] ;then # starting the interpreter according to its extension, .pl perl, .rb ruby ... extension=${filename##*.} case $extension in pl) NOHUP_SHELL=/usr/bin/perl ;; rb) NOHUP_SHELL=/usr/bin/ruby ;; py) NOHUP_SHELL=/usr/bin/python ;; *|sh) NOHUP_SHELL=/usr/bin/ksh ;; esac fi # configuration setting, default values : ${NOHUP_LOGDIR:=/var/tmp} : ${NOHUP_MONITORING:=} filename=${filename%.*} LOGFILE=$NOHUP_LOGDIR/${filename}_`date +%Y_%m_%d_%H_%M_%S`".log" echo "start command : $NOHUP_SHELL $@" nohup $NOHUP_SHELL $@ >>$LOGFILE 2>&1 & PID=$! echo "pid : $PID" echo "check log file: $LOGFILE" # optional tailing the output if [[ -n $NOHUP_MONITORING ]] ;then trap signal_handler INT TERM echo " $LOGFILE is reported here with tail -f Stop it with CTRL-C. CTRL-C doesn't mean stopping the background process. --- monitoring $LOGFILE --- " # touching the log file to ensure having file when starting the tail touch $LOGFILE tail -f $LOGFILE & TAIL_PID=$! wait $PID kill $TAIL_PID echo " --- background process has finished --- exiting... " fi ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright 2008 Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.html ##############################################################################