... and should run independent of the Shell it was started in
It can't run independent of the Shell it was started in ... It needs the shell to interpret its commands all the way through until its end/exit.
(because SSH will disconnect)
Disconnecting SSH will result in disconnecting the user's terminal which will in turn result in a SIGHUP
(hang-up) signal and the latter will cause the shell to terminate as the shell is merely a child process of the parent user's terminal ... In bash
, you can tell your shell to ignore the SIGHUB
signal with the job control builtin disown
In my current script below the mail command does not wait for the
BACKGROUNDJOB to terminate. How can I achieve this?
If you nest the commands in order e.g. comman1; command2; ...
without sending any preceding commands to the background like e.g. so:
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)"
Then the latter command will wait for the former command to complete execution first.
the entire script should put itself in the background to run
independent of the "shell"(you mean parent terminal) and continue running even when ssh
disconnects.
You can achieve this like so (the sub-shell syntax (...)
might not be always needed here but doesn't harm to use in all cases):
(/path/to/scriptfile) & disown
Or if you like to nest the commands in order instead, like so (the sub-shell syntax (...)
is needed here):
(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)") & disown