Operating System - HP-UX
1833747 Members
2574 Online
110063 Solutions
New Discussion

detect the run level from a shell script?

 
SOLVED
Go to solution
Fred Martin_1
Valued Contributor

detect the run level from a shell script?

I've added a few lines to /etc/profile which do not apply to single-user mode. Is there a way that the script can detect the run level so I can skip those lines, if the thing is in single-user mode?

Note: In single user, /usr/bin is unavailable, etc. so the solution needs to work only with what is mounted in single-user.
fmartin@applicatorssales.com
16 REPLIES 16
RikTytgat
Honored Contributor
Solution

Re: detect the run level from a shell script?

Hi,

Execute:

who -r | awk '{print $3}'

Bye,
Rik
Justo Exposito
Esteemed Contributor

Re: detect the run level from a shell script?

Hi Fred,

The who -r command give you this information. Read the man page for who command.

Regards,

Justo.
Help is a Beatiful word
Pete Randall
Outstanding Contributor

Re: detect the run level from a shell script?

Fred,

You could add a rc2 script to the startup that touched a file, the presence of which would tell /etc/profile that you were not in single user mode.

Pete

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: detect the run level from a shell script?

RS=$(who -r | awk '{print $3}')
if [ ${RS} -eq 3 ]
then
echo "I'm in run-level 3"
else
echo "I ain't"
fi
If it ain't broke, I can fix that.
Bill McNAMARA_1
Honored Contributor

Re: detect the run level from a shell script?

well, in single user, not sure if who works:
You can't even cp it to sbin in it's current state:

mawenzi - /usr/sbin # chatr /usr/bin/who
/usr/bin/who:
shared executable
shared library dynamic path search:
SHLIB_PATH disabled second
embedded path disabled first Not Defined
shared library list:
dynamic /usr/lib/libc.2
shared library binding:
deferred
global hash table disabled
plabel caching disabled
shared vtable support disabled
static branch prediction disabled
executable from stack: D (default)
kernel assisted branch prediction enabled
lazy swap allocation disabled
text segment locking disabled
data segment locking disabled
third quadrant private data space disabled
fourth quadrant private data space disabled
data page size: D (default)
instruction page size: D (default)
mawenzi - /usr/sbin # chatr /sbin/vgchange
/sbin/vgchange:
shared executable
static branch prediction disabled
executable from stack: D (default)
kernel assisted branch prediction enabled
lazy swap allocation disabled
text segment locking disabled
data segment locking disabled
third quadrant private data space disabled
fourth quadrant private data space disabled
data page size: D (default)
instruction page size: D (default)

I'm sure chatr chan be used to make a static version, I don't know how though..

Later,
Bill
It works for me (tm)
PIYUSH D. PATEL
Honored Contributor

Re: detect the run level from a shell script?

Hi,

who -r command gives u the run level.

Piyush
Fred Martin_1
Valued Contributor

Re: detect the run level from a shell script?

All good, thanks! Assuming run level 1 is single user, this should work:

if [ "`/sbin/who -r |
/sbin/awk '{print $3}'`" != "1" ]
then
# this is not single user
# my lines can go here
fi

By the way, I can't seem to find a list of run level values or defaults anywhere; have checked man on init, inittab, rc.
fmartin@applicatorssales.com
James R. Ferguson
Acclaimed Contributor

Re: detect the run level from a shell script?

Hi Fred:

Definition by run-level:

0 - halted
S - single user mode
1 - minimal system configuration
2 - multi-user mode
3 - exported file systems
4 - graphical interface managers

Have a look here for more information:

http://docs.hp.com/hpux/onlinedocs/os/startup.pdf

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: detect the run level from a shell script?

Well int init man pages are about as close as you are going to get for run level values; the default run level is set in the 'initdefault' entry in /etc/inittab.
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: detect the run level from a shell script?

0=Halted/reboot level, All kill and start scripts in /sbin/rc0.d are executed.
S=Single user state. All kill and start scripts in /sbin/rc0.d are executed.
1=Boot time system configuration. Necessary system configuration is done at this state.(Example: hostname defined, FS mounted). All kill and start scripts on /sbin/rc1.d are executed.
2=Multi-user state. Users are allowed to access the system. All kill and start scripts in /sbin/rc2.d are executed.
3=Networked multi-user state. NFS file systems can be exported. All kill and start scripts in /sbin/rc3.d are executed.
4=HP-VUE activated here. All kill and start scripts in /sbin/rc4.d are executed.
5=Not defined.
6=Not defined
Pete Randall
Outstanding Contributor

Re: detect the run level from a shell script?

Am I losing it? My systems don't have who in /sbin (this sounds like the beginning of an Abbott and Costello routine).
$ uname -a
HP-UX tsws1 B.11.11 U 9000/785 2006482480 unlimited-user license
$ ll /sbin/who
/sbin/who not found
$ whence who
/usr/bin/who
yukon(297)root# uname -a
HP-UX yukon B.11.00 U 9000/800 650349313 unlimited-user license
yukon(298)root# ll /sbin/who
/sbin/who not found
yukon(299)root# whence who
/usr/bin/who

Ahhh!, I did find a 10.20 system where it's located in /sbin.

Fred, what release are you running and how forward compatible do you want to be?

Pete

Pete
Fred Martin_1
Valued Contributor

Re: detect the run level from a shell script?

Pete,

I'm on 11.0 but upgraded from 10.20 - I do have /sbin/who, and it may have been left there from 10.20?

Anyway I've got plenty if info now to get me into trouble, thanks folks.
fmartin@applicatorssales.com
Pete Randall
Outstanding Contributor

Re: detect the run level from a shell script?

I guess it must be a leftover from 10.20. I'd just be leary of using a solution that's probably going to end up not working when you go to the next release - like 11i.

In any case, good luck,
Pete

Pete
Fred Martin_1
Valued Contributor

Re: detect the run level from a shell script?

I'd probably use this rather than what I wrote above, it's the compliment:

if [ "`/sbin/who -r |
/sbin/awk '{print $3}'`" = "3" ]
then
# my lines can go here
fi

And yeah, I suspect playing with /etc/profile is risky anyway.

The deal is, I need to check users that are logging in, to see if they are -not- coming in from my network (i.e. the internet) and want to 1) deliver a special message to them 2) check against an /etc/group to see if they are in the group of users that are allowed in.

If I could do those things without playing with a system file, I would.
fmartin@applicatorssales.com
Bill Hassell
Honored Contributor

Re: detect the run level from a shell script?

Here's the real story on single user mode detection:

First, init (despite the man page descriptions) cannot take you to true singleuser mode. init is not the parent of every process and thus, init s (or S) will not remove all the unnecessary kernel process (including networking). init 1 is not single user mode either. The only reliable way to get to singleuser mode is an interrupted bootup.

At 10.20, /sbin/who is there, but at 11.0 and higher, it was replaced with the program /sbin/getrunlvl. Unfortunately, neither program reliably reports singleuser mode! At 10.20, who often reports the last run level (ie, 3) even when in singleuser mode. This may be related to using shutdown 0 to get to single user mode rather than an interrupted reboot. getrunlvl also has reliablility problems for single user mode.

I had the same requirement for /etc/profile: detect singleuser mode and set a flag so that commands that are in /usr will be bypassed in the code. I finally concluded that the only way to detect this state is to determine if /usr is mounted. If it is, then we're not in true single user mode where only / and possibly /stand are mounted. Here is a code snippet from my /etc/profile:

USRMOUNTED=$(/sbin/mount -l | /sbin/awk '/^\/usr\ /')
if [ -z "$USRMOUNTED" ]
then
SINGLEUSERMODE=/sbin/true
else
SINGLEUSERMODE=/sbin/false
fi

Now you can do something like this in /etc/profile:

if $SINGLEUSERMODE
then
do_something
else
do_another_something
fi

A similar construct in /etc/profile is to determine if /etc/profile is being run in a batch job (ie, part of an su - user_name script). Here's the code snippet:

case $0 in
-* ) export LOGINSHELL=/sbin/true;;
* ) export LOGINSHELL=/sbin/false;;
esac

# Interactive test

if [ -o interactive ]
then
export INTERACTIVESHELL=/sbin/true
else
export INTERACTIVESHELL=/sbin/false
fi

So the variable $LOGINSHELL indicates that this script is being run at the lowest level (because there's a - in front of the shell's name), and the variable $INTERACTIVE menas that there is someone at a keyboard (as opposed to a cron job or batch script). This way, you can eliminate "not a typewriter" messages as in:

if $INTERACTIVE
then
eval $(/sbin/ttytype -s)
fi



Bill Hassell, sysadmin
Fred Martin_1
Valued Contributor

Re: detect the run level from a shell script?

Wow too bad you can't get 20 points :)

Thanks very much.
fmartin@applicatorssales.com