LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script question... (https://www.linuxquestions.org/questions/linux-general-1/shell-script-question-317322/)

defa0009 04-26-2005 10:20 AM

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

Blinker_Fluid 04-26-2005 12:04 PM

need the `` around the ps command... example:
Code:

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

defa0009 04-26-2005 12:38 PM

OK I did that and now it outputs....


Process is : ps -ef | grep Xvfb | grep -v grep
-bash: [: too many arguments
Starting Xvfb...

sm1else 04-26-2005 12:57 PM

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.

Padma 04-26-2005 01:16 PM

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.

defa0009 04-26-2005 07:10 PM

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...

demian 04-26-2005 07:57 PM

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: `

defa0009 04-26-2005 08:16 PM

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


All times are GMT -5. The time now is 07:45 PM.