How to exit a program by bash on a soft way and if this was not successful do it on a hard way.
Its known how to open a programm by bash and how to get the PID of the programm. The follow sample open the editor XED:
xed & PID=$!
Its known how to echo the PID of the program what was opened:
echo $PID
12345 # sample output of a PID
Its known how to try to close a program on a soft way and how to select the program by PID for this:
kill -15 $PID # select a program by PID and tray to kill it on a soft way
Its known how to close a program on a hard way and how to select the program by PID for this:
kill -9 $PID # select a program by PID and kill it on a hard way
Put all together what I have, a still missing something:
xed & PID=$! # open the program xed and get the pid
echo PID of programm: $PID
sleep 5 # lets run the xed 5 seconds
kill -15 $PID # select the xed by PID and try to kill it on soft way
# check based on variant 1, 2, 3 or other, the xed was sucessfull killed on soft way or not
# if the xed still running, kill it on follow way
kill -9 $PID # select the xed by PID and kill it on hard way
Based one Answer of Eddy763, the follow Logically better fitting variant:
xed & PID=$! # Start XED and get PID
sleep 2
# kill -15 $PID # Soft Kill, activate or deactivate for testing
echo
echo
sleep 2
if ! ps -p $PID
then
echo "The PID/program are closed successful by Soft Kill"
else
echo "The PID/program didn't be killed successful by Soft Kill active, so it will be killed now on hard way"
kill -9 $PID # Hard Kill
fi
echo
echo