LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 04-11-2008, 05:18 AM   #1
stu_mueller
Member
 
Registered: Aug 2006
Location: England
Distribution: Slackware, Zenwalk
Posts: 114

Rep: Reputation: 15
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
 
Old 04-11-2008, 05:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
pidof kbluetooth
empty string and exit status 1, if the kbluetooth process it's not running, PID and exit status 0 otherwise.
 
Old 04-11-2008, 02:54 PM   #3
iiv
Member
 
Registered: Jun 2007
Location: Russia, Moscow Region
Distribution: Slackware
Posts: 167

Rep: Reputation: 30
Quote:
Originally Posted by stu_mueller View Post
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
 
Old 04-13-2008, 04:48 PM   #4
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 15
Posts: 546

Rep: Reputation: 43
This way you can get rid of grep output
Code:
ps ax | grep [k]bluetooth
Hope this helps
Ciao
 
Old 04-15-2008, 02:09 PM   #5
rg3
Member
 
Registered: Jul 2007
Distribution: Fedora
Posts: 527

Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting help jgtg32a Programming 5 09-06-2005 09:38 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
BASH Scripting akilles Linux - Newbie 5 11-10-2004 07:26 PM
Bash Scripting Help Aioth Linux - Newbie 8 09-21-2004 11:21 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration