Megatest

Artifact [6c86d9c83e]
Login

Artifact 6c86d9c83ef9c6e425374f09430cd31258096af6:


#!/bin/bash
###############################################################################
#
# nbfake - capture command output in a logfile
#
# nbfake behavior can be changed by setting the following env vars:
#   NBFAKE_HOST       SSH to $NBFAKE_HOST and run command
#   NBFAKE_LOG        Logfile for nbfake output
#
###############################################################################

if [[ -z "$@" ]]; then
  cat <<__EOF

nbfake usage:

nbfake <command to run>

nbfake behavior can be changed by setting the following env vars:
   NBFAKE_HOST       SSH to \$NBFAKE_HOST and run command
   NBFAKE_LOG        Logfile for nbfake output

__EOF
  exit
fi

#==============================================================================
# Setup
#==============================================================================

# Can't always trust $PWD
CURRWD=$(pwd)

# Make sure nbfake host and logfile are set. Fall back to old-style variable names

if [[ -z "$NBFAKE_HOST" && -n "$TARGETHOST" ]];     then NBFAKE_HOST=$TARGETHOST;     fi

if [[ -z "$NBFAKE_LOG" && -n "$TARGETHOST_LOGF" ]]; then NBFAKE_LOG=$TARGETHOST_LOGF; fi

# Set default nbfake log

if [[ -z "$NBFAKE_LOG" ]]; then
  NBFAKE_LOG=NBFAKE-$(date +%GWW%V.%u_%T)
fi

#==============================================================================
# Run and log
#==============================================================================

cat <<__EOF >&2
#======================================================================
# NBFAKE logging command to: $NBFAKE_LOG
#     $*
#======================================================================
__EOF

if [[ -z "$NBFAKE_HOST" ]]; then
  # Run locally
  sh -c "cd $CURRWD;export DISPLAY=$DISPLAY; export PATH=$PATH; nohup $* >> $NBFAKE_LOG 2>&1 &"
else
  # run remotely
  ssh -n -f $NBFAKE_HOST "sh -c \"cd $CURRWD;export DISPLAY=$DISPLAY; export PATH=$PATH; nohup $* >> $NBFAKE_LOG 2>&1 &\""
fi