LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2011, 09:39 AM   #1
the98
Member
 
Registered: Sep 2010
Posts: 49

Rep: Reputation: 9
Question BASH: Can't check if xScreenSaver is running


Hi guys, how are you today?

I'm having trouble trying to make a script. What I want to do is check if xScreenSaver is running in my user account. If not, run it. If it's running, kill it. So this is the script I've made:

Code:
#!/bin/bash

if [ -n $(pgrep -u $(whoami) xscreensaver) ]
  then
	kill $(pgrep -u $(whoami) xscreensaver) &
	echo "XScreenSaver: Off"
  else
	xscreensaver -no-splash &
	echo "XScreenSaver: On"
fi
The problem is that I've echoed the output of $(pgrep -u $(whoami) xscreensaver) and it always seems to add 4 numbers to the pid, even if the pid doesn't exist. What do I mean by "doesn't exist"? That no xscreensaver is running in my user, and if I run pgrep -u $(whoami) xscreensaver in bash, I get not output, but if I run the command though the script, I get (for instance) 4050. If I run it again, I get 4054, and again 4058... etc. What the hell is going on with that?

Thanks in advance for your answers.

Cheers.
 
Old 02-26-2011, 09:56 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Maybe something like:
if [ -n $(pgrep -u $(whoami) xscreensaver 2> /dev/null) ]

kill $(pgrep -u $(whoami) xscreensaver 1> /dev/null 2> /dev/null

Last edited by gnashley; 02-28-2011 at 02:10 AM.
 
Old 02-27-2011, 03:26 PM   #3
the98
Member
 
Registered: Sep 2010
Posts: 49

Original Poster
Rep: Reputation: 9
Great! This solved it. I do have some questions, though...

What does "2 > /dev/null" do in this case and why do we use it twice in the kill command? I've read that "2 > /dev/null" sends errors to /dev/null (STDERR), but I don't think that the output I was getting before were errors...

Anyway, it works, so I'm happy.

Thanks a lot!

PS: This is what the script looks now:

Quote:
#!/bin/bash

if [[ $(pgrep -u $(whoami) xscreensaver 2> /dev/null) != "" ]]
then
kill $(pgrep -u $(whoami) xscreensaver 2> /dev/null 2> /dev/null) &
echo "Matando XScreenSaver"
else
xscreensaver -no-splash &
echo "Iniciando XScreenSaver"
fi
Without double [] I had some error and it didn't work properly with -n, so I've change to != ""
 
Old 02-28-2011, 02:23 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
That should have been:
kill $(pgrep -u $(whoami) xscreensaver 1> /dev/null 2> /dev/null) &

2> directs any error output and 1> redirects normal output of the command. The pid number that was bothering you comes from '1'. If you run kill on the command line you'll see what I mean.
Also run your pgrep command from the command line -in a case where it succeeds and one where it fails by giving it a false path:

pgrep -u $(whoami) bash ; echo $?

pgrep -u $(whoami) non-existent ; echo $?

The following '; echo $?' shows you the exit status of the command. Since you are using 'if' and test([]), 0(zero) means true and 1(one) means false. Every process you run will return at least $?(exit status) and optionally $1 and/or $2. Remember $1 is standard output, $2 is standard error. Always try the command out from the command line so you can see what is happening both when the command is successful and when not.
 
Old 03-01-2011, 08:05 AM   #5
the98
Member
 
Registered: Sep 2010
Posts: 49

Original Poster
Rep: Reputation: 9
The thing I've tried the commands on CLI but I had no output. pgrep only had output when I run the script.

Anyway, you've been really helpful. Thanks a lot!
 
  


Reply

Tags
bash, check, program, running



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: Check if a program is running naimslim89 Programming 12 05-08-2012 10:24 PM
Can bash check if screensaver is running? zensunni Linux - Newbie 4 11-19-2010 02:31 PM
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 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