Bash script as a "switch"

Bash script as a "switch"

am 05.11.2007 19:47:02 von Michael DeBusk

In the following thread on Ubuntu Forums:
http://ubuntuforums.org/showthread.php?t=599061

....the OP asks for a script that will kill a particular program (conky,
in his case) if it's running and start it if it isn't.His original
effort was as follows:

#!/bin/bash
if ps ax | grep -v grep | grep conky
then
killall conky &&
else
conky
fi

He said it doesn't work.

Misunderstanding what he wanted, I offered:
#!/bin/bash
pkill conky
conky

After he corrected my misperceptions, I offered:
#!/bin/bash
if pgrep conky; then
pkill conky && exit
else
conky
fi
exit

That stopped conky but didn't start it. I tried:
#!/bin/bash
if pgrep conky; then
pkill conky
exit
fi
conky

Again the OP said it failed. I'm lost. This is so simple, but I'm not
seeing the problem. What am I doing incorrectly?

Thank you for your time and expertise.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.

Re: Bash script as a "switch"

am 05.11.2007 19:55:44 von Scott McMillan

On Mon, 05 Nov 2007 18:47:02 -0000, Michael DeBusk
wrote:

>In the following thread on Ubuntu Forums:
>http://ubuntuforums.org/showthread.php?t=599061
>
>...the OP asks for a script that will kill a particular program (conky,
>in his case) if it's running and start it if it isn't.His original
>effort was as follows:
>
>#!/bin/bash
>if ps ax | grep -v grep | grep conky
>then
>killall conky &&
>else
>conky
>fi
>
>He said it doesn't work.
>
>Misunderstanding what he wanted, I offered:
>#!/bin/bash
>pkill conky
>conky
>
>After he corrected my misperceptions, I offered:
>#!/bin/bash
>if pgrep conky; then
> pkill conky && exit
>else
> conky
>fi
>exit
>
>That stopped conky but didn't start it. I tried:
>#!/bin/bash
>if pgrep conky; then
> pkill conky
> exit
>fi
>conky
>
>Again the OP said it failed. I'm lost. This is so simple, but I'm not
>seeing the problem. What am I doing incorrectly?
>
>Thank you for your time and expertise.

Perhaps conky isn't in the user's PATH? Try /path/to/conky...


Scott McMillan

Re: Bash script as a "switch"

am 05.11.2007 20:12:30 von Bill Marcum

On 2007-11-05, Michael DeBusk wrote:
> In the following thread on Ubuntu Forums:
> http://ubuntuforums.org/showthread.php?t=599061
>
> ...the OP asks for a script that will kill a particular program (conky,
> in his case) if it's running and start it if it isn't.His original
> effort was as follows:
>
> #!/bin/bash
> if ps ax | grep -v grep | grep conky
> then
> killall conky &&
> else
> conky
> fi
>
> He said it doesn't work.
>
'&&' inside an if-then-else doesn't make sense. Maybe it should be
'&'. You need '&' if conky is supposed to keep running while the 'if'
command finishes. Maybe there is a reason conky wasn't running, and it
dies when you try to start it again.
'killall conky' should work even without an 'if' to see if conky is
running. Try this:
killall conky 2>/dev/null || conky &

Note that you can't kill conky unless you started it or you are root.
Also, if conky isn't in your $PATH you have to use the pathname.

Re: Bash script as a "switch"

am 06.11.2007 05:11:36 von Michael DeBusk

On Mon, 05 Nov 2007 13:55:44 -0500, Scott McMillan
wrote:

> Perhaps conky isn't in the user's PATH? Try /path/to/conky...

He hasn't mentioned any trouble starting it except with the script. I'm
not sure how he's starting it, though. I'll ask.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.

Re: Bash script as a "switch"

am 06.11.2007 05:47:02 von Michael DeBusk

On Mon, 5 Nov 2007 14:12:30 -0500, Bill Marcum wrote:

> '&&' inside an if-then-else doesn't make sense.

Really? I put it in because I wanted "exit" to execute only if conky had
been killed. Now that I think about it, that's incorrect, though.

> Maybe there is a reason conky wasn't running, and it
> dies when you try to start it again.

He wants to kill it if it's running and to start it if it isn't.

> 'killall conky' should work even without an 'if' to see if conky is
> running. Try this:
> killall conky 2>/dev/null || conky &

Please help me understand. This says "kill conky or start conky" but it
doesn't make the condition explicit. Does that matter?

(I'm not running conky or I'd test it myself.)

Thanks for your help.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.

Re: Bash script as a "switch"

am 06.11.2007 09:10:14 von Bill Marcum

On 2007-11-06, Michael DeBusk wrote:
> On Mon, 5 Nov 2007 14:12:30 -0500, Bill Marcum wrote:
>
>> '&&' inside an if-then-else doesn't make sense.
>
> Really? I put it in because I wanted "exit" to execute only if conky had
> been killed. Now that I think about it, that's incorrect, though.
>
>> Maybe there is a reason conky wasn't running, and it
>> dies when you try to start it again.
>
> He wants to kill it if it's running and to start it if it isn't.
>
>> 'killall conky' should work even without an 'if' to see if conky is
>> running. Try this:
>> killall conky 2>/dev/null || conky &
>
> Please help me understand. This says "kill conky or start conky" but it
> doesn't make the condition explicit. Does that matter?
>
If conky isn't running, the kill command fails and the command after ||
is executed.

> (I'm not running conky or I'd test it myself.)
>
> Thanks for your help.
>

Re: Bash script as a "switch"

am 07.11.2007 05:23:23 von Michael DeBusk

On Tue, 6 Nov 2007 03:10:14 -0500, Bill Marcum wrote:

> If conky isn't running, the kill command fails and the command
> after || is executed.

Thanks. Now I get it. I've posted it on Ubuntuforums and will see how it
goes. If that doesn't work I don't know what to tell him.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.