LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   bash scripting (https://www.linuxquestions.org/questions/slackware-14/bash-scripting-634620/)

stu_mueller 04-11-2008 05:18 AM

bash scripting
 
Hi,

I am trying to write a script to turn on and off bluetooth.

At the moment I have two seperate scripts to do this, but I am trying to combine them into one script.

I want to check for the kbluetooth process, if this is running then I want to kill it and run rc.bluetooth stop

if it isn't running i want to run rc.bluetooth start and the start kbluetooth.

I have found this on the net that could check for the existence of hte kbluetooth process:

Code:

if ps ax | grep -v grep | grep kbluetooth > /dev/null
but I wanted to know if there was a better way of doing it, by checking PID's and how would I find the pid of kbluetooth?

Stuart

colucix 04-11-2008 05:51 AM

Code:

pidof kbluetooth
empty string and exit status 1, if the kbluetooth process it's not running, PID and exit status 0 otherwise.

iiv 04-11-2008 02:54 PM

Quote:

Originally Posted by stu_mueller (Post 3117810)
but I wanted to know if there was a better way of doing it, by checking PID's and how would I find the pid of kbluetooth?

Stuart

two variants: 1)
Code:

ps -C kbluetooth -o pid=
or 2)
Code:

pgrep kbluetooth

urka58 04-13-2008 04:48 PM

This way you can get rid of grep output
Code:

ps ax | grep [k]bluetooth
Hope this helps
Ciao

rg3 04-15-2008 02:09 PM

I recommend the method mentioned by iiv, but adding the -x parameter:

Code:

pgrep -x kbluetooth
Moreover, the pkill command does something similar but sending a signal to the processes it finds, so something like:

Code:

pkill -x kbluetooth && this_runs_if_it_was_found
Should be what you need.


All times are GMT -5. The time now is 11:23 PM.