[NTLUG:Discuss] More shell scripting madness

Chris Cox cjcox at acm.org
Thu Jul 5 12:42:18 CDT 2007


. Daniel wrote:
> I know it's hard to see, but I'm actually starting to learn this stuff 
> little by little.
> 
> Here's the purpose:
> 
> I upgraded the processor and video in my laptop.  Now it gets warmer than I 
> like, especially during game play, which slows down when the processor and 
> video heat up.  While I intend to improve the actual heatsink materials 
> (Copper is surprisingly hard to get in the form I need it... and EXPENSIVE) 
> I would like to keep the processor fans running.
> 
> As it turns out, the system is controlling the fans by itself based on 
> certain criteria.  But there is also a handy little utility to simply turn 
> on the fan!  It's called "i8kfan."  Run i8kfan without parameters and it 
> will simply return with two numbers indicating the mode of the fans at that 
> moment.  The values are 0, 1 and 2.  So if both fans are off, it will 
> return "0 0" and if both are on high speed it will return "2 2".
> 
> So I wrote a loop that checks the fan status every two seconds and if it's 
> not "2 2" it will set the fans to "2 2" and go back to sleep.  I then set 
> up an icon on my panel to start the process when I want to play a game so I 
> can prolong my "cool time."
> 
> Here's the problem though:  Gnome shortcuts no longer seem to want to 
> launch processes in terminal.  In earlier versions, there was a check box 
> to select this option.  It was a good option to use when you were debugging 
> because you could see error messages streaming by or whatever.  Now it's 
> gone and I can't run the script in a terminal window expect by hand... 
> which, of course, I don't wanna do.
> 
> So here's the script as simple as it is:
> 
> "i8kfanloop.sh"
> -----
> #!/bin/bash
> 
> loop=1
> 
> while [ loop ]
> do
>   status=`i8kfan`
>   if [ "$status" != "2 2" ]
>   then
>      i8kfan 2 2
>   fi
>   sleep 2
> done
> -----
> 
> That's some pretty simple bash code right?
> 
> Here's what I'd like to add to it.  I'd like to add some code that will 
> detect if a previous instance of this script is currently running and if it 
> is, kill it and exit.  If it's not running, just go into the loop.  So the 
> result would be, if I click the icon once, it runs the loop.  If I click 
> the icon again, it will stop the loop.
> 
> I imagine some code that will get the shell script's process ID number and 
> stores it somewhere, say "/tmp/$username_i8kfanloop.pid" or something like 
> that.  Then at the beginning of the script, it will look for said file and 
> compare the process ID recorded in the file with the process listing to see 
> if the process ID is there and actually the same script.  
> 
> If the filename doesn't exist, create the PID file and enter the loop.  
> If the filename exists but the process doesn't, it should erase the file, 
> create a new PID file and enter the loop.  
> If the filename exists and the process exists but the PID doesn't match up 
> with the script name, then erase the file, create a new PID file and enter 
> the loop.
> 
> I feel confident that if I messed with this for a few hours, I'd be able to 
> cobble up something that works.  But before I spend my work day on my 
> personal stuff, I'm willing to bet someone already has or knows about some 
> shell code out there that does this already.  And I'm hoping that someone 
> will be kind and generous enough to paste their code into a reply so that I 
> could learn and adapt my skills further. :)
> 

Consider a front end script called mydaemon-start.sh:

#!/bin/sh
if pid=$(pgrep mydaemon.sh); then
        kill -9 $pid
fi

exec /usr/local/bin/mydaemon.sh &

Where your script is what is inside of mydaemon.sh

You can always add a mydaemon-kill.sh that just does
the first part, or make the frontend understand
parameters so you can use the same script to 'start'
and 'stop' mydaemon.sh



More information about the Discuss mailing list