On linux bash I want to ensure my command execution takes 10 seconds or longer so if it finishes early I need to add some sleep so the overall execution would take more than 10 seconds.
If you are wondering why it is to ensure a third party background daemon job (running every 10 seconds) will pick it up.
What I have is:
sleep 10; my_command.sh
but this will add 10 seconds to overall execution so if the command itself takes 10 seconds or more it will still add another 10 seconds. Is there a way to only sleep if the command is taking less than 10 seconds?
EDIT:
I also want the exit code of my_command.sh
to be returned upon end of execution. So the whole command including sleep should return the same exit code as my_command.sh
.
You can run sleep 10
at the background and wait
for it to complete after my_command.sh
finishes.
sleep 10 & my_command.sh; wait
If there are other background jobs this will wait for them too, though. In that case you can either run it in a subshell, e.g:
( sleep 10 & my_command.sh; wait )
or keep sleep 10
's PID in a variable and wait for it, thus my_command.sh
will be run in current execution environment, e.g:
sleep 10 & pid=$!; my_command.sh; wait "$pid"