LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-26-2005, 10:20 AM   #1
defa0009
Member
 
Registered: Jun 2003
Posts: 185

Rep: Reputation: 31
Shell script question...


I'm trying to write this script that checks if the Xvfb is already started... and if not starts it. The problem I am having is with the line:

PROCESS=ps -ef | grep Xvfb | grep -v grep

The way I understand it is that the command: ps -ef | grep Xvfb | grep -v grep should return 1 if it found a process named Xvfb but the variable PROCESS is aways empty and I get this message when I run the script...

-bash: -ef: command not found
Process is :
-bash: [: -eq: unary operator expected
Starting Xvfb...



Here is the script I wrote... any help would be appreciated!



# Start the X virtual frame buffer (Xvfb)
PROCESS=ps -ef | grep Xvfb | grep -v grep
echo "Process is : $PROCESS"
if [ $PROCESS -eq 1 ]; then
echo "Xvfb already started..."
else
echo "Starting Xvfb..."
if [ -f /usr/X11R6/bin/Xvfb ]; then
/usr/X11R6/bin/Xvfb :1 -fp /usr/X11R6/lib/X11/fonts/misc -screen 0 1280x1024x16 -fbdir /tmp &
else
echo "Cannot locate file /usr/X11R6/bin/Xvfb..."
fi
fi
 
Old 04-26-2005, 12:04 PM   #2
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 682

Rep: Reputation: 63
need the `` around the ps command... example:
Code:
PROCESS=`ps -ef | grep Xvfb | grep -v grep`
 
Old 04-26-2005, 12:38 PM   #3
defa0009
Member
 
Registered: Jun 2003
Posts: 185

Original Poster
Rep: Reputation: 31
OK I did that and now it outputs....


Process is : ps -ef | grep Xvfb | grep -v grep
-bash: [: too many arguments
Starting Xvfb...
 
Old 04-26-2005, 12:57 PM   #4
sm1else
Member
 
Registered: Oct 2004
Distribution: At home: Ubuntu, Debian Sarge
Posts: 56

Rep: Reputation: 15
I dont use bash scripting much but backticks return the stdout of the command (not its exit code).

In tcsh I would try this, it appears that the same code works in bash too:
Code:
ps -ef | grep Xvfb | grep -v grep
set exitcode=$?
Which results in $exitcode being 1 if Xvfb wasnt found and 0 if it was.
 
Old 04-26-2005, 01:16 PM   #5
Padma
Member
 
Registered: Aug 2003
Location: Omaha, NE, USA
Distribution: PCLinuxOS 2007
Posts: 808

Rep: Reputation: 30
defa0009: Did you use back-ticks, or just regular ticks. Back-ticks return the stdout of the command enclosed, regular ticks would give the output you showed.
 
Old 04-26-2005, 07:10 PM   #6
defa0009
Member
 
Registered: Jun 2003
Posts: 185

Original Poster
Rep: Reputation: 31
I am not aware of back-ticks or regular-ticks? I made the changes as sm1else suggested...

# Start the X virtual frame buffer (Xvfb)
ps -ef | grep Xvfb | grep -v grep
set exitcode=$?
echo "exitcode is : $exitcode"
if [ $exitcode -eq 0 ]; then
echo "Xvfb already started..."
else
echo "Starting Xvfb..."
if [ -f /usr/X11R6/bin/Xvfb ]; then
/usr/X11R6/bin/Xvfb :1 -fp /usr/X11R6/lib/X11/fonts/misc -screen 0 1280x1024x16 -fbdir /tmp &
else
echo "Cannot locate file /usr/X11R6/bin/Xvfb..."
fi
fi

and this was the output? Thanks again for your guys help!

root 3439 1 0 17:46 ? 00:00:00 /usr/X11R6/bin/Xvfb :1 -fp /usr/ X11R6/lib/X11/fonts/misc -screen 0 1280x1024x16 -fbdir /tmp
exitcode is :
-bash: [: -eq: unary operator expected
Starting Xvfb...
 
Old 04-26-2005, 07:57 PM   #7
demian
Member
 
Registered: Apr 2001
Location: Bremen, Germany
Distribution: Debian
Posts: 303

Rep: Reputation: 30
Code:
#!/bin/bash

PID=`pidof Xfvb`

if [ -n "$PID" ]
        then echo " Running already "
elif [ -z "$PID" ]
        then
        if [ -x /usr/X11R6/bin/Xfvb ]
                then echo "starting..."
                 /usr/X11R6/bin/Xvfb :1 -fp /usr/X11R6/lib/X11/fonts/misc -screen 0 1280x1024x16 -fbdir /tmp &
        else echo "Bummer! Can execute Xfvb. This sucks."; exit 1;
        fi
fi
This is a tick: '
This is a backtick: `
 
Old 04-26-2005, 08:16 PM   #8
defa0009
Member
 
Registered: Jun 2003
Posts: 185

Original Poster
Rep: Reputation: 31
Thanks guy's! Back ticks and regular ticks! Demian I used your pidof to get the one argument for the if statement...


# Start the X virtual frame buffer (Xvfb)
if [ -n `pidof Xvfb` ]; then
echo "Xvfb already started..."
else
echo "Starting Xvfb..."
if [ -f /usr/X11R6/bin/Xvfb ]; then
/usr/X11R6/bin/Xvfb :1 -fp /usr/X11R6/lib/X11/fonts/misc -screen 0 1280x1024x16 -fbdir /tmp &
else
echo "Cannot locate file /usr/X11R6/bin/Xvfb..."
fi
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Question abdul_zu Programming 7 10-06-2005 03:33 AM
shell script Question Whiteghost Programming 4 09-25-2005 10:05 PM
Shell Script Question. rvijay Linux - General 2 07-14-2005 06:41 PM
shell script question rrwhite Linux - General 3 09-15-2004 01:01 AM
shell script question Axion Programming 4 07-29-2004 09:51 PM


All times are GMT -5. The time now is 06:47 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration