Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
grep(1) offers exit status which you can use in your script to determine if the outcome was successful versus not.
When using the grep exit status I always recommend checking for success because that is a zero, whereas "not found" is non-zero, but also error is a different non-zero value. Instead check for an affirmative found and then have an else case if you need to do something for the "not found & error" cases.
You check a command's exit status using $? within a script.
I'd enable "set -xv" within your script to ensure that you're looking at what you expect. Finger doesn't show that it provides exit status at all, which makes sense since it is a "report" type of command. But just make sure you don't get bit using a combination or a piped command and end up not looking at the exit status you intend to look at.
Just a side note: the ":1" isn't always true. It can be any number depending on the system... Each time a user logs in with concurrent users the GUI the number will be incremented (right now, I have a :2 display, my wife has the :0, :1 was mine, until I locked it up and manually killed it) and every ssh login that forwards windows can have other numbers (by default, starting with 10, but that is up to the configuration of sshd).
SERVICE='firefox'
if /usr/bin/pgrep $SERVICE > /dev/null; then echo "$SERVICE is running..."; else echo "Write code here to here to make it run"; exit 1; fi
speweth:
firefox is running...
finger is not reliable as a service test. Better to test the {finger} port, (using nc, or telnet....)
Distribution: CentOS 6.6, RHEL Server release 5.5 (Tikanga)
Posts: 58
Original Poster
Rep:
Quote:
Originally Posted by jpollard
Just a side note: the ":1" isn't always true. It can be any number depending on the system... Each time a user logs in with concurrent users the GUI the number will be incremented (right now, I have a :2 display, my wife has the :0, :1 was mine, until I locked it up and manually killed it) and every ssh login that forwards windows can have other numbers (by default, starting with 10, but that is up to the configuration of sshd).
In some causes yes, But I am from the IT Department, when we VNC into one of our locations, we are (:1), and that's the way we configured it. Now is I ssh, I would be (192.168.x.x), however (:1) will still show up on the finger list since that is the VNC server.
Distribution: CentOS 6.6, RHEL Server release 5.5 (Tikanga)
Posts: 58
Original Poster
Rep:
All good now.
Code:
if finger | grep -q "(:1)"; then
echo -e "\n VNC Server Is Not Fully Closed Yet, Try again in 5 seconds"
else
echo -e "\n VNC Server has been killed. You may now start it back up"
fi ;;
Not sure where my other reply went, but @jpollard, I know what you are saying, but I am from the IT Department (at Corporate) and the connection we use in VNC is "192.168.x.x:1". The VNC server confirguration is set to make a ":1" spot for us to log into. Only the IT department has that. So the Code I build kills and starts that ":1". It will remain ":1" for us all the time.
Now if we SSH into the server, and we will, because we need to in order to kill and restart it, we will be listed on finger as (192.168.x.x).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.