Hello,
I am trying to write a script that turns off the screensaver for a certain period of time, then come back on. I have had it up and running for a while, but then I decided to refactor it a bit for my family members that are less computer savvy.
I am starting a subshell for the "meat" of the off script
Code:
(sleep $STIME ; sson ;exit) &
I want to be able to find this sleep command and kill the command. This allows the finishing of the script calling my sson script and then exiting.
I could always do a "ps ux|grep sleep|awk '{print $2}'|xargs kill", but if I happen to any other script with a sleep running that is not associated with sleeping the screesaver, this will kill that one too.
I was hoping to use the $! ability of bash on the subshell and kill that, but is many of you probably already knew, killing the bash doesn't kill the sleep. As I'm typing this, I thought of this:
Code:
(
sleep $STIME &
PID=$!
if [ ! -f /tmp/sleep.pid ] ; then
touch /tmp/sleep.pid
else
echo $PID >> /tmp/sleep.pid
fi
wait $PID
sson
exit
) &
I do the whole touch and append thing because I would like to keep a log of the pids and kill any that might have been run (or kill all previous before) in case my ssoff script is invoked twice.
Plus, I just want to do an exercise for my own learning on PID tracking.
Is this the way it should be done, or is there a better way of coding it?
With thanks,
Narnie