I have written a script that takes commands from user and runs through a huge list of servers and writes to a single file. Once writing has completed it triggers an email. My requirement is: the script should run in background and when it has finished, send an email.
I have tried
wait $PROC_ID & - but writing is happening in background and the email is sent immediately with blank data. Is there any possibility to wait in background and trigger mail once data writing in file completed?
Output of code:
The output can be monitored in /tmp/Servers_Output_list.csv .....................Please Be Patient Mail will get triggered once completed
Code is below and I mention one function of my script
for i in `cat $file_name` do ssh -q -o "StrictHostKeyChecking no" -o "NumberOfPasswordPrompts 0" -o ConnectTimeout=2 $i "echo -n $i;echo -n ",";$command3" 2>>/dev/null & done >> $file_save & PROC_ID=$! echo " " echo " The output can be monitored in $file_save....................Please Be Patient" echo "Mail will get triggered once completed" echo " " wait $PROC_ID while true; do if [ -s $PROC_ID ];then echo "$PROC_ID process running Please check" else mailx -s "Servers Command Output" -a $file_save <my firstname.lastname@example.org> break; fi done & exit 0; fi