I am trying to run a background-job that should send an email after it terminates. The background-job can run several hours and should run independent of the Shell it was started in (because SSH will disconnect). In my current script below the mail command does not wait for the BACKGROUNDJOB to terminate. How can I achieve this?
nohup BACKGROUNDJOB -l $corefolder/$filename >/dev/nul 2> $corefolder/$filenameerr &
mail -s "JOB DONE" -A $corefolder/$filename -A $corefolder/$filenameerr abc123@gmail.com <<< "job finished on $HOSTENAME on $(date)"
The mail should attach 2 output files from the BACKGROUNDJOB, which are only available after it has terminated.
Or generally:
#Script.sh
Process 1 (e.g. run rsync) [this could take 10h and only then rsync.log is available]
Process 2 (send email with rsync.log as attachment)
#end script
AND the entire script should put itself in the background to run independent of the shell and continue running even when ssh disconnects.
BACKGROUND JOB
,wait
(seeman $SHELL
about "Job Control"), then send the mail.nohup
that script. – waltinator Mar 21 '23 at 05:13BACKGROUND_JOB
in the background (remove trailing&
) andnohup the_script &
. Put the whole script in the background as a simgle blob – waltinator Mar 21 '23 at 06:40