Skip to content

Commit

Permalink
stdout+stderr of wrapped process are forwarded to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Nov 14, 2021
1 parent b67ab76 commit 6922b02
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
9 changes: 7 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## PRELIMINARY ##

### 1.0.0 ###

* Added: simple test scripts
* Changed: `bash` runtime required due to `pipefail` options
* Changed: stdout+stderr of wrapped process are forwarded to stdout
* Changed: command output is stored into temp file

### 0.6.0 ###

* Changed: removed hostname from journalctl output
Expand All @@ -11,5 +18,3 @@
### 0.4.0 ###

* Bugfix: Default exit code `0` in case task has been completed without errors


2 changes: 1 addition & 1 deletion package-monster.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taskrunner",
"version": "0.6.0",
"version": "0.7.0",
"package-monster": "0.2.0",
"description": "captures the command output and sends it via http post",
"license": "MPL-2.0",
Expand Down
44 changes: 24 additions & 20 deletions taskrunner
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/sh
#!/usr/bin/env bash

# ----------------------------------------------------------------------
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# --
# Copyright 2018-2020 Andi Dittrich <https://aenon-dynamics.com>
# Copyright 2018-2021 Andi Dittrich <https://aenon-dynamics.com>
# ----------------------------------------------------------------------

set -o pipefail

# taskrunner-sh https://github.com/AenonDynamics/taskrunner-sh
VERSION="0.5.0"
VERSION="1.0.0"

show_usage(){
cat << EOF
Expand Down Expand Up @@ -41,30 +43,34 @@ EOF
. /etc/taskrunner.conf

# extract task title
TASKNAME=$1
TASKNAME=${1}

# extract command
CMD=$2
CMD=${2}

# taskname given ?
[ -z "$TASKNAME" ] && { echo >&2 "taskname not set"; exit 1; }
[ -z "${TASKNAME}" ] && { echo >&2 "taskname not set"; exit 1; }

# command given ?
[ -z "$CMD" ] && { echo >&2 "executable not set"; exit 1; }
[ -z "${CMD}" ] && { echo >&2 "executable not set"; exit 1; }

# command valid ?
command -v $CMD >/dev/null 2>&1 || { echo >&2 "executable <$CMD> not found"; exit 1; }
command -v ${CMD} >/dev/null 2>&1 || { echo >&2 "executable <${CMD}> not found"; exit 1; }

# remove first+second argument from list and extract args
shift
shift
ARGS=$@

# create logfile
LOGFILE=$(mktemp)
trap "rm ${LOGFILE}" EXIT

# get command start time
T_START=$(date +%s)

# run command and capture output
OUTPUT=$($CMD $ARGS 2>&1)
${CMD} ${ARGS} 2>&1 | tee ${LOGFILE}

# store status code
TASK_STATUS=$?
Expand All @@ -82,26 +88,24 @@ curl \
--show-error \
--http1.1 \
--header "Expect: " \
--header "X-APIKEY: $API_KEY" \
--data-urlencode "title=$TASKNAME" \
--data-urlencode "status=$TASK_STATUS" \
--data-urlencode "t0=$T_START" \
--data-urlencode "t1=$T_STOP" \
--header "X-APIKEY: ${API_KEY}" \
--data-urlencode "title=${TASKNAME}" \
--data-urlencode "status=${TASK_STATUS}" \
--data-urlencode "t0=${T_START}" \
--data-urlencode "t1=${T_STOP}" \
--data-urlencode "content@-" \
--request POST \
"$API_URL" \
> /dev/null <<CURL_DATA
${OUTPUT}
CURL_DATA
"${API_URL}" \
> /dev/null < ${LOGFILE}

# store status code
LOG_STATUS=$?

# curl operation success?
[ $LOG_STATUS -ne 0 ] && { echo >&2 "tasklogging failed"; }
[ ${LOG_STATUS} -ne 0 ] && { echo >&2 "tasklogging failed"; }

# command success ?
[ $TASK_STATUS -ne 0 ] && { echo >&2 "operation failed"; exit 1; }
[ ${TASK_STATUS} -ne 0 ] && { echo >&2 "operation failed"; exit 1; }

# default exit code
exit 0

0 comments on commit 6922b02

Please sign in to comment.