LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-26-2011, 01:19 PM   #1
maddog.
LQ Newbie
 
Registered: Mar 2011
Location: Pittsburgh, PA
Posts: 5

Rep: Reputation: 0
Question Getting Hostname of machine you are 'physically' on using Bash & RH 5.4


I was not sure if "newbie" was the right forum for this. Yet as I
found no Forum for Bash or even shells, I am putting it here.
[BTW, I recommend you create a forum for such a topic, you seem to get these all over.]

I admit I am a little green with the "Bourne Again Shell" (Bash),
even though I was always not as proficient with the Bourne or Korn
shell as with C Shell. From recent research the public sway is to
Bash over tcsh. I can see I have been away from Linux/UNIX way to
long.

To my problem: Running a network of workstations (upto 6) and
servers (2) running RH Enterprise Linux 5.4 plus in the same
network is an old DEC Alpha running Tru64 UNIX (don't know which
version): I looking for a method to get the hostname I am logged
in on to use in a Bash (or if necessary sh) script. This is to
be added to an auto_start shell script start up our application
on Linux workstation and servers.

Any suggestions would be appreciated.

maddog.
 
Old 03-26-2011, 01:38 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
Well you can use
Code:
$ hostname
regardless of what shell you're using.
 
Old 03-26-2011, 01:41 PM   #3
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,687
Blog Entries: 23

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Hi,

Interesting question, simple answer:

Quote:
uname -a
in the console, dunnow what you'll get in a remote session, but it could well be worth the "whack"!

It gives me a ton of info...hope the same for you, manpage in the net

http://www.linuxmanpages.com/man1/uname.1.php

Wellness to ya

Thor
 
Old 03-27-2011, 12:28 PM   #4
maddog.
LQ Newbie
 
Registered: Mar 2011
Location: Pittsburgh, PA
Posts: 5

Original Poster
Rep: Reputation: 0
Question Update: Autostarting programs on server/wkstn w/o root access

I am retitling my thread as to what I am actually attempting to do.

I realize not long after I posted this question that I also found a method even though yours is better.
What I figured out was

env | grep HOST

produced

HOSTNAME=machine.domain.com

So the command 'hostname' definitely works better. However short of what I wanted. I now realize
that I asked the wrong question. What I actually need is to automatically start up with a script are
programs to run on both servers and workstations without needing of root access. Workstations started up
with logon running X11 and the Motif window manager. So currently a script is added to the X11 file
.xsessions. This does the job. Currently this script is configured to startup the workstation(s) and
server on the same machine. My goal is to redo this to account for multiple workstation(s) and
server(s). At the moment SSH keys are distributed for users accross machines.

Here is my idea so far. Let me know if this will work. Any thoughts.

I continue to startup the workstation as normal in .xsessions.

For the server I add the following

ssh account@server_n
"if program not running"
startup program <-- startup is our autostart shell script

I just found a portion in our script that may be what I am wanting. I don't know what it is doing.

if [ -e ${HOME}/tmp/pgm ]; then
pgm_pid=`ps -e -o "pid,command,user" | grep "${USER}" | grep "pgm" | grep -v <... missing ...> grep | cut -b 1-7`
if [ -z "$pgm_pid}" ]; then
...
fi
fi

I know in the second line if the variable 'pgm_pid' is used from the line before.
Does the text '-e $(HOME)/tmp/pgm' mean whether pgm exists in home/tmp directory ?
What does text '-z $shell_var' mean where 'shell_var' is some shell variable ?

Hmmm... Playing with Cygwin Bash shell I get

ps -e|grep bash|cut -b 66-70
bash

Is this what this line is doing... ?

Thoughts... :-)

maddog.

Last edited by maddog.; 03-27-2011 at 12:31 PM.
 
Old 03-30-2011, 02:16 AM   #5
zjoske
LQ Newbie
 
Registered: Dec 2009
Posts: 18

Rep: Reputation: 2
Hi Maddog

ps -e shows al processes, the option -o "pid,command,user" shows only the process id, the command and the user name.
The output is piped to a grep, which filters out the user's ($(USER)) processes, skipping processes of others.
Grep displays lines containing the given pattern ("pgm" or your user name indicated by the variable $(USER) ).

The switch "-v" in the grep displays all lines except the pattern you entered. (What you often see is "ps -ef|grep -v grep" which shows all processes except the grep command itself).

Eventually you end up with one line containing the process id, username and command you are looking for.

The cut -b 1-7 takes the first 7 bytes of that line.
The result is stored in a variable called "pgm_pid"

the next line checks if the lenght of the string in variable pgm_id is 0 ("is empty").
You may find that the "cut -b 1-7" has some side effect, it may take the first characters from the command too, especially when your process ID is low, i.e. 42 or 373 or ...

Better is to replace the "cut -b 1-7" with something like "awk '{print $1}'" which gives you the proper process ID
in case you need it.

HTH

Jos

Quote:
Originally Posted by maddog. View Post
I just found a portion in our script that may be what I am wanting. I don't know what it is doing.

if [ -e ${HOME}/tmp/pgm ]; then
pgm_pid=`ps -e -o "pid,command,user" | grep "${USER}" | grep "pgm" | grep -v <... missing ...> grep | cut -b 1-7`
if [ -z "$pgm_pid}" ]; then
...
fi
fi
maddog.
 
Old 03-30-2011, 08:25 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
When you say 'start a program at startup', do you mean start it at at boot time or when someone logs in? Either is possible, but would use different solns.
 
Old 03-30-2011, 10:06 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Another option to set pgm_pid is:
Code:
pgm_pid=$(pgrep -u $USER pgm)
 
Old 04-13-2011, 07:33 AM   #8
maddog.
LQ Newbie
 
Registered: Mar 2011
Location: Pittsburgh, PA
Posts: 5

Original Poster
Rep: Reputation: 0
Talking New things found

When we were "starting program" it was during login. The desire was to incorporate it with starting
of X11. So I put the start within .xsession. This works.

BTW I found even simpler way to get the Hostname w/o extra stuff.

hostname -s

I like it.

maddog.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot find the Linux machine by hostname ufmale Linux - Networking 3 03-18-2008 01:51 PM
How do i use a hostname given by No-IP as my machine name? z-vet Linux - Networking 3 10-22-2004 09:06 PM
Sendmail not working between two users on physically same machine rylan76 Linux - Networking 1 04-20-2004 04:30 PM
Getting hostname of remote machine with gethostbyaddr() thomjakub Linux - Networking 4 03-06-2004 10:08 AM
Can't ping linux machine by hostname jdklein1975 Linux - Networking 3 02-26-2003 02:49 PM


All times are GMT -5. The time now is 05:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration