You are not logged in.

#1 2017-02-27 20:58

JimW
Member
Registered: 2015-12-08
Posts: 400

Help needed on bash script to check status of program

I am trying to write a script that will tell me the status of a program. I just want to know if it is running or not, so there may be an easier way than what I am trying.

if [pgrep lighttpd]; then status="ON"; else status="OFF"; fi

That does not work. I have tried with "pgrep lighttpd' with (pgrep lighttpd) and 'pgrep lighttpd' even `pgrep lighttpd` (back quote) but have not been able to get the correct response.

Can any of you set me straight or am I hopless? smile

Offline

#2 2017-02-27 21:40

Dai_trying
Member
From: UK
Registered: 2015-12-14
Posts: 2,992

Re: Help needed on bash script to check status of program

i would use pidof for that.

if [ "$(pidof lighttpd)" ] 
then
    status="ON"
else
    status="OFF"
fi

untested but should work.

Offline

#3 2017-02-27 23:16

JimW
Member
Registered: 2015-12-08
Posts: 400

Re: Help needed on bash script to check status of program

Dai_trying wrote:

i would use pidof for that.

if [ "$(pidof lighttpd)" ] 
then
    status="ON"
else
    status="OFF"
fi

untested but should work.

Works great! Thanks Dai!
I had a kdialog that I wanted to put the status of lighttpd into the title, and this does it.
(I had forgotten about pidof - plus not sure on where to put the brackets/quotes/$ etc. - this helps)

Offline

#4 2017-02-27 23:34

Dai_trying
Member
From: UK
Registered: 2015-12-14
Posts: 2,992

Re: Help needed on bash script to check status of program

You are welcome JimW, You could also shorten it to a one-liner with...

[ "$(pidof lighttpd)" ] && status="ON" || status="OFF"

Offline

Board footer

Powered by FluxBB