How just spotted here the problem is that you have to wait that the programs that you run from your script finish their jobs.
If in your script you run program in background you can try something more.
In general a call to sync
before you exit allows to flush file system buffers and can help a little.
If in the script you start some programs in background (&
), you can wait that they finish before you exit from the script. To have an idea about how it can function you can see below
#!/bin/bash
#... some stuffs ...
program_1 & # here you start a program 1 in background
PID_PROGRAM_1=${!} # here you remember its PID
#... some other stuffs ...
program_2 & # here you start a program 2 in background
wait ${!} # You wait it finish not really useful here
#... some other stuffs ...
daemon_1 & # We will not wait it will finish
program_3 & # here you start a program 1 in background
PID_PROGRAM_3=${!} # here you remember its PID
#... last other stuffs ...
sync
wait $PID_PROGRAM_1
wait $PID_PROGRAM_3 # program 2 is just ended
# ...
Since wait
works with jobs as well as with PID
numbers a lazy solution should be to put at the end of the script
for job in `jobs -p`
do
wait $job
done
More difficult is the situation if you run something that run something else in background because you have to search and wait (if it is the case) the end of all the child process: for example if you run a daemon probably it is not the case to wait it finishes :-).
Note:
wait ${!} means "wait till the last background process is completed" where $!
is the PID of the last background process. So to put wait ${!}
just after program_2 &
is equivalent to execute directly program_2
without sending it in background with &
From the help of wait
:
Syntax
wait [n ...]
Key
n A process ID or a job specification