When/how is $HOSTNAME populated ?


 
Thread Tools Search this Thread
Operating Systems AIX When/how is $HOSTNAME populated ?
# 1  
Old 07-06-2009
Question When/how is $HOSTNAME populated ?

When and how is the variable $HOSTNAME populated ?

I am asking because I had a weird situation that just happened.

I have created backup scripts that do backups on different servers. Each servers has different folders to be backed up. So I had included a check on $HOSTNAME to find out which folders to back up.

But tonight, when I started the backup on one server, the backup did not start because it said I was on the wrong server (my coding). I turned out aht the content of the $HOSTNAME was empty. All the other servers had no problems starting the same exact backup script as it $HOSTNAME was containing the right servename.

Here are the steps I did immediatlly after:
A) Checking the content of the $HOSTNAME variable:
- I logged with my own operator userid and did "echo $HOSTNAME". It had the servername in it
- I did a "sudo echo $HOSTNAME" and the content was empty
- I did a "sudo su -" then a "echo $HOSTNAME and the variable had the server name in it.
B) I opened up another window and did the same steps as in A) and all 3 steps showed that $HOSTNAME had a servername content.

I had to resort doing a "sudo su -" and start the backup. Otherwise, I would have had to change the script as a quick workaround.

Somehow, on that specific xterm window I had opened, the content of the $HOSTNAME variable was not populated in all cases. I want to know how this happened and why as it may happen again with another operator (they don't know how to script).
# 2  
Old 07-06-2009
Any variable in the shell comes into existence by your shell's startup scripts...

I would start there. Not sure about ksh, but in bash it would be /etc/bashrc....start there, and that file should lead you to other files that might get "sourced" (processed) within that file. Then there is your local user shell startup file....for instance, "~/.bashrc"

If you know the files, you could "grep -n HOSTNAME" on those files, to show the line numbers where the string HOSTNAME occurs.
# 3  
Old 07-07-2009
I don't know what happened but maybe an alternative might be to use in your script:
Code:
MY_HOSTNAME=`hostname`

#or

MY_HOSTNAME=`uname -n`

Might be more safe instead of hoping that $HOSTNAME is set.
# 4  
Old 07-07-2009
I would use $(hostname) instead of $HOSTNAME

I have systems too where $HOSTNAME is not set, but the hostname command works always
# 5  
Old 07-07-2009
funksen and zaxxon are both correct. Never depend on any variables set outside of a script but "bring with you what you need to work with".

The number 1 problem (by a large margin) with cron-scripts is: "i have written a script which ran fine in my shell, then i put it into crontab and now it doesn't work any more. It still runs when i test it." The reason is that your login shell inherits all kinds of environment variables but scripts started by cron don't. If you implicitly rely on - for instance - $PATH being set the same way it is set in your interactive shell you are in for a bad surprise.

Several variables are set in your "~/.profile" and in your "~/.kshrc", but some system-wide variables are set in "/etc/environment". All these files do NOT apply to scripts started by cron.

This is the reason why i have ALL my scripts use their own environment, regardless of them being run in cron or not. The beginning of my scripts always looks like this:

Code:
#! /bin/ksh

if [ -z "$DEVELOP" ] ; then                      # set environment
     . /usr/local/lib/ksh/f_env
else
     . ~/lib/f_env
fi

typeset SCRIPT_INTERN_VAR1=""
typeset SCRIPT_INTERN_VAR2=""

... etc., etc. ...

where "/usr/local/lib/ksh/f_env" is a ksh function which declares all the variables common to all the scripts. It looks like this:

Code:
     unset ENV                                   # clear the environment

     #---------------------------------------------------- set basic environment

     typeset -x OS=$(uname -a | cut -d' ' -f1)   # find out the OS
                                                 # read in standard environment
     case "$OS" in
          AIX)
               . /etc/environment
               ;;

          Linux)
               . /etc/profile
               ;;

          *)
               . /etc/environment
               ;;
     esac

                                                 # set default TERM variable
     case "$OS" in
          AIX)
               TERMDEF="$(termdef)"
               ;;

          Linux)
               TERMDEF="$TERM"
               ;;

          *)
               TERMDEF="$TERM"
               ;;

     esac
     typeset -x TERM=${TERMDEF:-'wyse60'}        # set default TERM variable
     typeset -x LANG=C                           # default language environment
     typeset -x EDITOR=vi                        # what else ? ;-))
     typeset -x VISUAL=$EDITOR

     typeset -x PATH="/usr/bin"                  # set the path
                PATH="$PATH:/bin"
                PATH="$PATH:/etc"
                PATH="$PATH:/usr/sbin"
                PATH="$PATH:/usr/ucb"
                PATH="$PATH:/sbin"
                PATH="$PATH:/usr/bin/X11"
                PATH="$PATH:/usr/local/bin"      # tools, home for scripts
                PATH="$PATH:/usr/local/sbin"     # -"-

     typeset -x chTmpDir=""                      # path for temporary files

... etc., etc. ...

I never have to worry about a script being run in cron or not, being run by a specific user or even on a specific system - it won't fail because of the environment being set wrongly.

I hope this helps.

bakunin
# 6  
Old 07-17-2009
Maybe it's a question of the shell you're using? Some shells use $HOST (eg. tcsh) others (eg. bash) use $HOSTNAME

mbs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

How to get input text boxes populated / perl cgi?

Hi, I know how to populate a combo box or a drop down list box which I do as under : Query that gets the data for the drop down box : $qry = "select \"EmployeeID\" AS empid, \"FirstName\"::text || ' ' ||\"LastName\"::text as name from \"Employees\""; $qq =... (1 Reply)
Discussion started by: terrykhatri531
1 Replies

2. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

3. AIX

Hard Drive stuck in populated state?

Hi all, I ended up replacing a failed disk drive on my power 7. I had a few issues with unmirroring as it had a stale logical volume. I ended up finally getting it to unmirror the rootvg. I had an empty space in slot 3 so I ended up putting the new disk in slot 3. It configured to hdisk0 with no... (2 Replies)
Discussion started by: AlexanderIII
2 Replies

4. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

5. SCO

Possible hostname issue?

from the hostname command I get this: scosysv.mycompany0 when I ping scosysv.mycompany0 i get 64.158.xx.xx which fails with 100% packet loss. /etc/hosts shows 199.199.199.1 scosysv scosysv.mycompany0 scosysv.mycompany0.com when I ping the hostname value isn't the local IP 199.199.199.1... (6 Replies)
Discussion started by: bbxguy
6 Replies

6. UNIX for Dummies Questions & Answers

Log file not getting populated when using BTEQ

Hi, I am trying to run some SQL scripts under the UNIX server using BTEQ. When I try to create a log file, the file gets populated only to the point where I log into BTEQ. The log file for the running of the actual script does not seem to be stored. Would any one know ehy this could be... (3 Replies)
Discussion started by: zsrinathz
3 Replies

7. UNIX for Dummies Questions & Answers

Printing hostname

Hi, I am using hostname environment variable in my shell script to print the host name but it's printing nothing.Can you please help me on this. Please find the part of the code pasted here: llist= llist=$(ps -ef | grep -v grep | grep -c "$1") echo "Hostname is ${hostname}" (7 Replies)
Discussion started by: gbiswal
7 Replies

8. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

9. IP Networking

looking up hostname

Using Solaris 8 (or WINXP). I am trying to look up a specific DNS hostname, but I don't know which DNS server houses that entry. How can I find the hostname? nslookup gives me the following: C:\>nslookup hostname Server: dnsserver Address: x.x.x.x *** dnsserver can't find hostname:... (2 Replies)
Discussion started by: dangral
2 Replies

10. UNIX for Dummies Questions & Answers

Hostname

Hello, I am installing redhat linux 6.2 on an intel based system. Whether i want to know any naming conventions should i follow. ie Any convention to follow to name a linux machine(To give hostname). Simillarly for domain name also. Please suggest in this regard (1 Reply)
Discussion started by: bache_gowda
1 Replies
Login or Register to Ask a Question