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 |
need the `` around the ps command... example:
Code:
PROCESS=`ps -ef | grep Xvfb | grep -v grep` |
OK I did that and now it outputs....
Process is : ps -ef | grep Xvfb | grep -v grep -bash: [: too many arguments Starting Xvfb... |
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 |
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.
|
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... |
Code:
#!/bin/bash This is a backtick: ` |
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. |