Skip to content

Instantly share code, notes, and snippets.

@supersonictw
Created October 1, 2025 09:42
Show Gist options
  • Save supersonictw/868f2506299875a272a50c1ad3999803 to your computer and use it in GitHub Desktop.
Save supersonictw/868f2506299875a272a50c1ad3999803 to your computer and use it in GitHub Desktop.
#!/bin/sh
# cron-exec - Execute a command from cron, capture output and exit status, log to target
# SPDX-License-Identifier: MIT (https://ncurl.xyz/s/Kkn2DQsNR)
LOG_TARGET="/dev/fd/1" # Log output destination
TASK_DATETIME="$(date -R)" # Current date/time in RFC-2822 format
TASK_CMD="$@" # Command to execute, passed as arguments
# Execute the command, capture both stdout and stderr
TASK_RESULT="$(sh -c "$TASK_CMD" 2>&1)"
# Capture the exit status of the command
TASK_SIGNAL="$?"
# Format the log entry
TASK_LOG="[cron-exec] date: $TASK_DATETIME signal: $TASK_SIGNAL cmd: $TASK_CMD result: $TASK_RESULT"
# Write the log entry to the log target
echo "$TASK_LOG" >>"$LOG_TARGET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment