Register
It is currently Thu Oct 23, 2014 2:35 pm

How to track and later kill a process in a script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Feb 25, 2010 11:43 pm   

Joined: Sat Feb 20, 2010 10:09 pm
Posts: 1
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


Top
 Profile  
 PostPosted: Sat Feb 27, 2010 1:11 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Hey, that's actually quite good. Very nicely done.

You could always check for the file at the beginning before you do anything else, and rm the file at the end.

Code:
(
if [ -e /tmp/sleep.pid ] ; then
    echo "Process is already running."
    exit -1
else
    touch /tmp/sleep.pid
fi
sleep $STIME &
PID=$!
wait $PID
sson
rm /tmp/sleep.pid
exit 0
) &


Although, I would watch out for two things with this strategy:
1. The script fails before the call to rm, blocking a restart
2. /tmp might be cleaned on a regular (say, nightly) basis

- thobbs


Top
 Profile  
 PostPosted: Mon Mar 01, 2010 10:39 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 25
Location: Try to guess!
Are you using Ubuntu by any chance? In that case inhibiting the screensaver is as easy as invoking
Code:
$gnome-screensaver-command -i

Just my 2c.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP