Skip to content

Commit

Permalink
Merge pull request #13 from axelhahn/update-status-2
Browse files Browse the repository at this point in the history
cronstatus v1.7 - show running jobs
  • Loading branch information
axelhahn authored May 22, 2023
2 parents df5e0e8 + 8b1e97c commit 0e8e0a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
46 changes: 42 additions & 4 deletions cronstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +128,7 @@ function showStatus(){
statusRc="${statusOK}"
if [ $rc -ne 0 ]; then
iErr+=1
statusRc="${statusERROR}"
statusRc="<<<<<<<<<< ${statusERROR}"
fi

# ----- check ttl value
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -189,16 +194,49 @@ 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 )
do
showStatus "$logfile"
done

showRunningJobs

echo "____________________________________________________________________________________"

echo "JOBS: $(getLogfiles | wc -l ) .. ERRORS: $iErrJobs"
echo "JOBS: $(getLogfiles | wc -l ) .. RUNNING: $(getRunningfiles | wc -l ) .. ERRORS: $iErrJobs"
echo
}
# ----------------------------------------------------------------------
Expand Down
20 changes: 18 additions & 2 deletions docs/30_Usage/30_Cronstatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0e8e0a6

Please sign in to comment.