From: jgreco@solaria.mil.wi.us (Joe Greco)
Subject: Re: Can single user mode be entered and exited from a script?
Keywords: single-user backup dump
Organization: Solaria Public Access UNIX - Milwaukee, WI
Date: Sat, 19 Dec 1992 08:18:46 GMT

In comp.sys.sun.admin article <BzH5JL.Mz5@netnews.jhuapl.edu>, humesdg1@netnews.jhuapl.edu (Dave Humes) wrote:
:
:Is there any way to bring a SunOS system to single user mode from a shell
:script, and have the script resume multi-user mode?  I have operators do our 
:backups in the evening by logging into an account that starts a script to
:do the backup.  It works fine, but files can be open if people are still on.
:I'd rather not have to give them the root password or root priviliges just to
:get the system into single user mode.

Ah, yes.  I've used this horrible trick for about three years, originally on
IBM RT 6152 workstations where I had some problem where I had to reboot from
remote.  This was something I devised when I was still a little wet behind 
the ears dealing with singleuser mode, but it's simple, relatively secure 
if you want it to be, and easily adapted to other uses.

I add the following to root's /.profile:
------------------------------------------------------------------------------
if [ -f /backup.script ]; then
	echo ""
	echo Executing /backup.script
	date
	rm -f /backup.exe
	mv /backup.script /backup.exe
	sh -c /backup.exe
	echo Done
	date
	rm -f /backup.exe
	exit 0
fi
if [ -f /backup.exe ]; then
	echo ""
	echo WARNING: A backup script has failed, see /backup.exe!
	echo ""
fi
------------------------------------------------------------------------------

I run the following from cron in the wee hours to do my incrementals.
------------------------------------------------------------------------------
#! /bin/sh -

PATH=/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/local/adm:/usr/lib; export PATH

/bin/sh - << END_OF_CRON 2>&1 | cronmail sroot "Daily Backup Cron Report"

device1=/dev/rmt8
script=/backup.script

####################
# MUST check for a "sense key= not ready"!!!
###################
if (2>&1 mt -f ${device1} status > /dev/null); then
	sleep 1
else
	logger -p daemon.err -t backup.daily "Tape not loaded."
	echo "Tape not loaded."
	exit 1
fi

cat - << EOF > ${script}
#! /bin/sh -

PATH=/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/lib; export PATH

# do the backups here
mt -f ${device1} fsf 2
# do the backups here

mt -f ${device1} rewind
mt -f ${device1} offline

exit 0
EOF

chmod 755 ${script}

shutdown +30 "System going down for daily backups"

2>&1 mt -f ${device1} rewind > /dev/null

sleep 1860

echo "Major malfunction, system failed to shut down for backups!"

END_OF_CRON

exit 0
------------------------------------------------------------------------------

I use a little script called "cronmail" to add subjects to my cron reports
and mail them to selective users, and there's a wrapper to set the env the
way I need it.  So the real meat is between the /bin/sh - << END_OF_CRON and
the END_OF_CRON.  When this runs, it creates a file in the root directory,
containing a shell script of things to be done in singleuser mode.  It then
does a regular shutdown, which should land the system in singleuser mode in
30 minutes.  It takes advantage of this time to rewind the tape...

At 30 minutes, the system shuts down, init starts a sh on console, reading
the booby-trapped .profile which instead meanders off to do my script.  NOTE
that the file-"shuffling" is *VERY* important, if the file is not renamed it
will be somewhat difficult to get into singleuser mode again.

The system restarts when the script terminates.  Bingo.  Automated 4am
singleuser mode backups while-u-sleep.

... Joe

-------------------------------------------------------------------------------
Joe Greco - System Administrator		       jgreco@solaria.mil.wi.us
Solaria Public Access UNIX - Milwaukee, WI			   414/321-9287
Newsgroups: comp.sys.sun.admin