HOW TO Check for process ending?

Hello,
I'm new of this forum and have very very old and rusty memories of shell scripting (my latest script dates back to about 20 years ago !).

My need is following:
I'm testing a software that make a backup, but at now is not implemented the email report (the sw is in beta).
I'm trying to write a script the extract job result and send me an email.
My problem is to check when the job is finished, in order to grep job result and email it. In other words, until the job is running do nothing; when job is ended,
proceed with the script.

Thanks to those who want to help me.
Davide

This is the script:
------------------------------------------------------------------------

#!/bin/bash
#
# Script to run Veeam backup and email job status (BackupJob3)
#This is the backup command
veeamconfig job start --id {ba6434ea-55cc-4529-a9d3-69ec868769e9}
JOBNAME=BackupJob3
export JOBNAME
TODAY=$(date +"%Y-%m-%d")
export TODAY
# this to check id job is running or not
JOBRUNNING=`pgrep veeamjobman`
export JOBRUNNING
#Here I need to check and wait for job ending, but the while cycle is not working
while [ -z "$JOBRUNNING" ]
do
        echo "job is running" >> /tmp/test/out.txt
done
# this is to check if the job is failed or not
FAILED=`veeamconfig session list | grep -e Failed | grep $JOBNAME | grep $TODAY`
export FAILED
if [ -z "$FAILED" ]
then
        echo "Job $JOBNAME Successfull" | mailx -s "VAL session $JOBNAME Successfull" my@email.it
else
        echo "Job $JOBNAME Failed" | mailx -s "VAL session $JOBNAME Failed" my@email.it
fi

------------------------------------------------------------------------

Not everybody (including me) is familiar with that backup tool. Please explain its behaviour: Does it do the backups itself, or does it send some job(s) into background? Does it start independent processes? Does it come back immediately after doing so?

The info "the while cycle is not working" is not necessarily something to work upon. Please supply way more detail: What doesn't work? How does it deviate from the expexted behaviour? Error messages?

Two comments to your above script:

  • the -z checks for zero-length-strings; this might be the opposite of what you want?
  • the while loop won't ever react on changing conditions unless you modify the conditions (i.e. the pgrep ) INSIDE the loop.