Eric Peterson a écrit :
I'm having this same issue. I would like to call a function which has its
STDOUT and STDERR visible on the terminal, yet appended to the log file. And I
want to know if my function finished successfully or had an error. So thinking
I can do the following yet I am caught by this pipe happening after the $? is
read.
please, wrap your lines to something like 70 chars
# -- attempt 1 ——————————
<snip>
Is there a way around this? I tried:
# -- attempt 2 --------------------
integer rc=0
{
foo arg1 arg2
rc=$? # lost at closing brace
} | tee -ai a.log 2>&1
print "rc: $rc\n"
(( rc > 0 )) && exit ${rc}
# -- attempt 2 --------------------
but it prints 0 as the below example does.
don't remember, but I suppose that the arguments to this function are
protected using shell-quote (or something like that) or that they don't
have blanks, so, I don't care about $*
the idea is to launch the process in background and to wait for its
termination with some timeout (maybe 0 : infinite), then to return
the code of the launched process or 4 if timed out.
the key is echo; echo $rc in nohup so I'm sure to read the return code
using tail -1 in the waiting loop. don't care to redirections, they are
there for debuging, you may remove all the exec stuff.
trace() { echo trace=; }
logdir=${TMPDIR:-/tmp}
progname=${0##*/}
progname=${progname%.*}
_KILL=kill _CP=cp _DEVNULL=/dev/null _TAIL=tail
_SHELL=${SHELL} _SUDO= _PS=ps
function timedssh
{
set +x; eval $(trace -q timedssh "$@")
typeset timeout=$1 host=$2
shift 2
typeset xtrace='-x'
[[ -o 'xtrace' ]] || xtrace='+x'
ssh ${host} '
set '"${xtrace}"'
exec 3>&1 4>&2
[[ -o xtrace ]] || exec > /dev/null
[[ -f /etc/profile ]] && . /etc/profile
if [[ -n ${BASH_VERSION} && -f ${HOME}/.bash_profile ]]; then
. ${HOME}/.bash_profile
elif [[ -f ${HOME}/.profile ]]; then
. ${HOME}/.profile
fi
coderet=4 tmpfile='"${logdir}/${progname}"'.$$
function onexit {
[[ -o xtrace ]] && exec 2>&4 4>&-
if [[ -n ${pid_tail} ]]; then
(( coderet == 4 )) && ${_KILL} ${pid_tail}
wait ${pid_tail}
fi
[[ -o xtrace ]] || exec 2>&4 4>&-
exec 1>&3 3>&-
exit ${coderet}
}
[[ -o xtrace ]] && typeset -ft onexit
pid_nohup= pid_tail=
trap "exit \${coderet}" HUP QUIT TERM; trap onexit EXIT
${_CP} "${_DEVNULL}" "${tmpfile}"
${_TAIL} -f "${tmpfile}" >&3 &
pid_tail=$!
nohup '"${_SHELL} -c \"${_SUDO} $*"'; rc=\$?; \
${_KILL} ${pid_tail}; echo; echo ${rc}" \
>> "${tmpfile}" 2>&1 3>&- 4>&- &
pid_nohup=$!
[[ -o xtrace ]] || exec 2> /dev/null
timeout='"${timeout}"' elapsetime=1
while (( timeout == 0 || elapsetime <= timeout )); do
sleep 1
if ${_PS} -p ${pid_nohup} > /dev/null 2>&4; then
(( elapsetime += 1 ))
# coderet=0
else
coderet=$(${_TAIL} -1 "${tmpfile}" 2>&4)
${_RM} -f "${tmpfile}" 2>&4
break
fi
done
exit ${coderet}
'
integer coderet=$?
set +x; eval ${trace}
exit ${coderet}
}
Regards,
Cyrille Lefevre
--
mailto:[email protected]
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users