The problem is that all commands are executed from the first line to the last in order. So as @Scott Stensland
mentoined you have to put it into background via &
(ampersand) to make the timer start.
And furthermore I think getting the PID of your app via searching through process names is a dangerous practice since you might accidentally kill a program which contains the string app
in its name. So a safer way is to use the variable !
to get the PID . So your modified script should now looks like this :
while (true); do
./app &
app_pid=$!
sleep 3600
kill $app_pid
sleep 60
done
When you put a process into background via &
it goes into the job list of the parent bash process , and via !
you can get the PID of the last job.So it's safe.
./app &
or else the script will never reach seeing the sleep step until after the app exits$!
to get the PID ofapp
instead of getting it via searching through the process names with pkill. But first you have to put it into background to be able to get it's pid via$!
.