From 8b1e97c6559732bd2610e46929c2e9fb31374b45 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" Date: Mon, 22 May 2023 14:20:08 +0200 Subject: [PATCH] cronstatus v1.7 - show running jobs --- cronstatus.sh | 46 +++++++++++++++++++++++++++++++--- docs/30_Usage/30_Cronstatus.md | 20 +++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/cronstatus.sh b/cronstatus.sh index 5371d1a..6478212 100755 --- a/cronstatus.sh +++ b/cronstatus.sh @@ -12,9 +12,10 @@ # 2022-09-22 ahahn 1.4 add last output lines; add total status; exitstatus > 0 on error # 2022-10-27 ahahn 1.5 add 2 checks for hostname: is it a fqdn + filename matches hostname -f # 2023-01-31 ahahn 1.6 add param support; analyze a single log +# 2023-05-22 ahahn 1.7 show running jobs # ------------------------------------------------------------ -_version=1.6 +_version=1.7 LOGDIR=/var/tmp/cronlogs # outfile=/tmp/cronjob_status.$$.tmp @@ -47,6 +48,10 @@ function getLogfiles(){ ls -1t "$LOGDIR"/*log | grep -Fv "/__" } +# get logfiles of all cronwrapper cronjobs +function getRunningfiles(){ + ls -1t "$LOGDIR"/*log.running 2>/dev/null +} # show help # param string info or error message @@ -123,7 +128,7 @@ function showStatus(){ statusRc="${statusOK}" if [ $rc -ne 0 ]; then iErr+=1 - statusRc="${statusERROR}" + statusRc="<<<<<<<<<< ${statusERROR}" fi # ----- check ttl value @@ -157,7 +162,7 @@ function showStatus(){ # ----- check expire statusExpire="$(date -d @$iJobExpire '+%Y-%m-%d %H:%M:%S')" if [ $iJobExpire -lt $iMaxAge ]; then - statusExpire="${statusExpire} ${statusERROR}" + statusExpire="${statusExpire} <<<<<<<<<< ${statusERROR}" iErr+=1 else statusExpire="${statusExpire} ${statusOK}" @@ -189,6 +194,37 @@ function showStatus(){ fi } +# show running jobs +function showRunningJobs(){ + if getRunningfiles >/dev/null 2>&1 ; then + echo "____________________________________________________________________________________" + echo + echo "CURRENTLY RUNNING JOBS:" + + for logfile in $( getRunningfiles ) + do + sCmd=$(getLogValue SCRIPTNAME) + sLastStart=$(getLogValue SCRIPTSTARTTIME) + typeset -i iSince=($( date '+%s' )-$( echo "$sLastStart" | cut -f 2 -d ',' ))/60 + typeset -i iTTL=$(getLogValue 'SCRIPTTTL') + if [ $iTTL -lt $iSince ]; then + statusTtl="${statusERROR}: TTL=$iTTL min is lower than execution time of ${iSince} min" + iErr+=1 + fi + + echo + cw.cecho "head" "${sPre}--- for $iSince min - $logfile" + echo "${sPre}${sPre}command : ${sCmd}" + echo "${sPre}${sPre}last start: ${sLastStart}" + echo "${sPre}${sPre}ttl : ${iTTL}" + + done + else + echo + echo "There is no running job." + fi +} + # show total overview of all jobs function showTotalstatus(){ for logfile in $( getLogfiles ) @@ -196,9 +232,11 @@ function showTotalstatus(){ showStatus "$logfile" done + showRunningJobs + echo "____________________________________________________________________________________" - echo "JOBS: $(getLogfiles | wc -l ) .. ERRORS: $iErrJobs" + echo "JOBS: $(getLogfiles | wc -l ) .. RUNNING: $(getRunningfiles | wc -l ) .. ERRORS: $iErrJobs" echo } # ---------------------------------------------------------------------- diff --git a/docs/30_Usage/30_Cronstatus.md b/docs/30_Usage/30_Cronstatus.md index 7388fc9..76822db 100644 --- a/docs/30_Usage/30_Cronstatus.md +++ b/docs/30_Usage/30_Cronstatus.md @@ -47,7 +47,7 @@ In this example I have 2 cronjobs using the cronwrapper and both are OK. In that ____________________________________________________________________________________ CRONJOBS on [www.example.com] -______________________________________________________________________________/ v1.6 +______________________________________________________________________________/ v1.7 --- /var/tmp/cronlogs/www.example.com_scheduler.sh.log command : /opt/imlbackup/client/scheduler.sh @@ -66,9 +66,25 @@ ______________________________________________________________________________/ ttl : 1440 - 24 h OK expires : 1642047121 2022-01-13 05:12:01 OK CHECK OK + +There is no running job. +____________________________________________________________________________________ +JOBS: 1 .. RUNNING: 1 .. ERRORS: 0 + +``` + +If a job is currently running you get a shorter info block with the time how long it is running already: + +```txt +(...) ____________________________________________________________________________________ -JOBS: 2 .. ERRORS: 0 +CURRENTLY RUNNING JOBS: + --- for 2 min - /var/tmp/cronlogs/my-laptop_iml-backup.log.running + command : /home/axel/skripte/client/backup.sh + last start: 2023-05-22 13:44:40, 1684755880 + ttl : 1440 +(...) ``` ### Exitcode