You are not logged in.

#1 2012-10-23 18:36:44

Prezioso
Member
Registered: 2011-01-17
Posts: 83

sh script toggle eclimd

Hi everyone,

I want to create a shell script in order to toggle eclimd on/off use that in my conkybar.
My pseudo code looks like this:

  1. Check if eclimd is running.

  2. If false: start eclimd echo ON to conky.

  3. else stop eclimd echo OFF to conky

So far my code is:

#!/bin/sh
SERVICE='/usr/share/eclipse/eclimd' #make it easy to reuse this and the next step could be to grep the filename part after toggle e.g toggleEclimd.sh and the service would be eclimd
fuser $SERVICE > /dev/null 2>&1
if [ $? -eq 0 ]; then
    $SERVICE
else
 sudo killall $SERVICE # Here I would like to actually grep the PID and just kill PID. because killall just terminate the program and not the same as CTRL + c (in xmonad mod+shift+c)

This doesnt work. If I switch the starting and the killing. Then it works, but this doesnt make much sense to me. Isnt it checing to see fuser finds a running program and returns true if it does?

If I make the starting and killing echo ON or OFF, how is this displayed to conky? Because in my head ${exec toggleEclimd.sh} will execute it again and again? or? :S

Offline

#2 2012-10-24 02:59:16

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: sh script toggle eclimd

If you just want conky to display if your program is running, put

${if_running eclimd}Eclimd ON${else}Eclimd OFF

somewhere in the TEXT section of your .conkyrc

Then use your script to toggle eclimd.

In your script, it would probably be better to use 'pidof' or 'pgrep' to determine if eclimd
is running instead of 'fuser'.

if pid=$(pgrep $SERVICE); then
	kill -SIGINT $pid
else
	$SERVICE
fi

Edit: To answer your question.

If a command works in bash, it returns 0.
So in your script, if the 'fuser $SERVICE' is successful, it returns 0 into $?.

Last edited by rockin turtle (2012-10-24 03:12:01)

Offline

#3 2012-10-24 11:48:15

Prezioso
Member
Registered: 2011-01-17
Posts: 83

Re: sh script toggle eclimd

Oh I new the conky problem was easy. Should have read the man page better. But thanks.

I can see that the update you provide to my script is better, but it doesnt kill it. If I type kill pid(the actual pid) its killed the way it should.

Offline

#4 2012-10-24 17:31:54

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: sh script toggle eclimd

I probably got that wrong. Perhaps it should instead be:

 kill -SIGTERM $pid

I never remember which signal corresponds to C-c.  Anyway, I hope you get everything working as you like.

Offline

Board footer

Powered by FluxBB