Jump to content

Proper way to shutdown over the console?


NLS

Recommended Posts

Here is a script I wrote to do a graceful poweroff

 

root@unraid:/sbin# more powerdown 
#!/bin/bash

alias logger="/usr/bin/logger -is -plocal7.info -tpowerdown" 

logger "Powerdown initiated"

if [ -f /var/run/powerdown.pid ]
   then logger "Powerdown already active, this one is exiting"
        exit
   else echo $$ > /var/run/powerdown.pid
fi

trap "rm -f /var/run/powerdown.pid" EXIT HUP INT QUIT

logger "Shutting down Samba"
/etc/rc.d/rc.samba stop

logger "Syncing the drives"
/bin/sync

logger "Killing active pids on the array drives"
for fs in /mnt/user /mnt/disk*
do 
    if [ ! -d ${fs} ] ; then continue ; fi
    for pid in $(fuser -cu $fs 2>/dev/null)
    do  ps --no-headers -fp ${pid}
        kill -TERM ${pid}
        # sleep 1
        #if kill -0 ${pid} 2>/dev/null
        #   then kill -9 ${pid}
        #fi
    done
done 2>&1 | logger 

logger "Umounting the drives"
for disk in /mnt/disk*
do  /bin/umount ${disk}
done

logger "Stopping the Array"
/root/mdcmd stop

logger "Saving current syslog to /boot/var/log/syslog.txt"
if [ ! -d /boot/var/log ]; then mkdir -p /boot/var/log; fi
todos < /var/log/syslog > /boot/var/log/syslog.txt
chmod a-x /boot/var/log/syslog

# /sbin/poweroff
logger "Initiating Shutdown with Halt"
/sbin/shutdown -t5 -h now

 

Here is a script to install and hook this into the CTRL-ALT-DEL sequence.

Install it into your go script.

 

Note. It expects the script to reside in /boot/poweroff.

Change appropriately for your environment.

 

#!/bin/bash

COMMAND=/sbin/powerdown
INSTALL=1

if [ ${DEBUG:=0} -gt 0 ]
   then set -x -v
fi

if [ -f /boot/powerdown -a ! -f /sbin/powerdown -a "${INSTALL:=0}" -gt 0 ]
   then fromdos < /boot/powerdown > /sbin/powerdown
        chmod u=rwx /sbin/powerdown
fi

if [ -x ${COMMAND} ];then 
   if ! grep "${COMMAND}" /etc/inittab > /dev/null ; then 
      grep -v 'ca::ctrlaltdel:/sbin/shutdown' < /etc/inittab > /etc/inittab.tmp
      cat <<-EOF >> /etc/inittab.tmp
ca::ctrlaltdel:/sbin/powerdown
EOF
   mv /etc/inittab.tmp /etc/inittab
   /sbin/telinit q
   fi
fi

Link to comment

thanks!

 

two things:

 

- there is a "stop" script (by Tom) AFAIK... does it do the same?

 

- so I put the first script in /boot and the second inside go (after the last line) AS IS? (except the possible path edits)

 

...I trust you add those things to wiki too? :D

 

 

Link to comment

> so I put the first script in /boot and the second inside go (after the last line) AS IS? (except the possible path edits)

 

The first script "powerdown" goes where ever you store your installed programs and scripts on /boot

The second script, if you choose to use, is called from go and there is a path reference to the first script that needs to be adjusted.

 

I would not put the script itself inside go.

I would save the script as something you call from go, but not put all those lines in go.

 

My configuration is:

 

root@unraid:/boot# ls -l /boot/powerdown /boot/config/rc.local/S30-inittab-powerdown

-rwxr-xr-x 1 root root  557 Mar  8 02:25 /boot/config/rc.local/S30-inittab-powerdown*

-rwxr-xr-x 1 root root 1148 Mar 19 09:39 /boot/powerdown*

 

and I add a line in the go script to call /boot/config/rc.local/S30-inittab-powerdown as

 

fromdos < /boot/config/rc.local/S30-inittab-powerdown | sh

 

Actually I have a whole startup/shutdown infrastructure see end of this message for details.

 

> there is a "stop" script (by Tom) AFAIK... does it do the same?

I do not see it on later versions, however my script was devised from that.

 

> I trust you add those things to wiki too?

Not until there is a dedicated infrastructure for boot scripts.

Everyone seems to do it differently.

You may choose the way that suits you best.

 

I have a whole infrastructure of /boot/config/rc.local scripts that do all of the supplementary boot tasks.

 

root@unraid:/boot/config/rc.local# ls -l
total 176
-rwxr-xr-x 1 root root  148 Feb 20 15:29 S00-syslog-update*
-rwxr-xr-x 1 root root  263 Feb 21 14:29 S01-blockdev*
-rwxr-xr-x 1 root root  529 Feb 22 03:25 S02-sync-etc*
-rwxr-xr-x 1 root root  189 Apr  6 19:34 S03-cpufreq*
-rwxr-xr-x 1 root root  151 Feb 20 17:19 S10-installpkg*
-rwxr-xr-x 1 root root  464 Feb 21 14:05 S20-init.identd*
-rwxr-xr-x 1 root root 2224 Mar  8 20:10 S20-init.mt-daapd*
-rwxr-xr-x 1 root root  851 Feb 22 04:36 S20-init.proftpd*
-rwxr-xr-x 1 root root  229 Feb 21 12:52 S20-init.rsyncd*
-rwxr-xr-x 1 root root  365 Feb 22 03:28 S20-init.sshd*
-rwxr-xr-x 1 root root  608 Mar  8 00:23 S30-inittab-additions*
-rwxr-xr-x 1 root root  557 Mar  8 02:25 S30-inittab-powerdown*
-rwxr-xr-x 1 root root  554 Feb 22 04:39 S75-useradds*
-rwxr-xr-x 1 root root  811 Feb 20 15:49 S80-update_hosts*
-rwxr-xr-x 1 root root  129 Apr 13 11:35 S81-rsync_boot_cron*
-rwxr-xr-x 1 root root 1460 Mar  9 19:53 S90-smb-shares*
-rwxr-xr-x 1 root root  899 Apr 16 23:45 S90-tmpfs*
-rwxr-xr-x 1 root root   79 Apr 13 11:28 S91-init.vfs_cache*
-rwxr-xr-x 1 root root  276 Apr 13 11:30 S92-init.slocate*
-rwxr-xr-x 1 root root 2050 Apr  7 21:53 S99-syslog-save*
-rwxr-xr-x 1 root root 2097 Feb 25 06:09 rc.local_shutdown*
-rwxr-xr-x 1 root root  917 Feb 21 14:40 rc.local_startup*

 

Here is my simple go script

 

root@unraid:/boot/config# more go
#!/bin/bash
# Start the Management Utility
echo trace > /proc/mdcmd;sleep 1
/usr/local/sbin/emhttp &
fromdos < /boot/config/rc.local/rc.local_startup | sh

 

and my local startup script

 

root@unraid:/boot/config# more /boot/config/rc.local/rc.local_startup  
#!/bin/bash
logger -trc.local_startup -plocal7.info -is "Initiating Local Custom Startup."

for script in /boot/config/rc.local/*
do scriptbase=${script##*/}      # Strip pathname
   if [ $scriptbase = "rc.local_startup"  ] ;then continue; fi 
   if [ $scriptbase = "rc.local_shutdown" ] ;then continue; fi 
   ( echo "Processing $script" 
     fromdos < $script | sh
   ) 2>&1 | logger -t$scriptbase -plocal7.info -is

   # Old way not used.
   # fromdos < $script > /tmp/$scriptbase
   # chmod u+x /tmp/$scriptbase
   # /tmp/$scriptbase
   # rm /tmp/$scriptbase
done

# Setup shutdown script

RCFILE=rc.local_shutdown
if ! grep /boot/config/rc.local/$RCFILE /etc/rc.d/$RCFILE > /dev/null 2>&1
   then echo /boot/config/rc.local/$RCFILE >> /etc/rc.d/$RCFILE
fi

if [ ! -x /etc/rc.d/$RCFILE ]
   then chmod u+x /etc/rc.d/$RCFILE
fi

logger -trc.local_startup -plocal7.info -is "Local Custom Startup Complete."

Link to comment
> there is a "stop" script (by Tom) AFAIK... does it do the same?

I do not see it on later versions, however my script was devised from that.

 

I found it in /root, which is not in the path.  Tom may want to update it though, as it does not umount any drives past Disk 13, or the Cache drive.  A umount loop seems a good choice.

 

Link to comment
Here is a script I wrote to do a graceful poweroff

 

I do like your updated powerdown script, with added logging, single-instance enforcement, and disk-in-use detection!  I'll be taking advantage of it.  Thank you, great work as always.

 

A very minor point, for newer users, I prefer a log path like /boot/logs over /boot/var/log.  The /var seems like an IT persons choice, and although I don't want to hide the Linux foundation here, I don't think it is necessary for new users.  I like the way Tom has set the system up, easy for a highly Windows-centric user base, but open to others.  When a user is ready for more advanced topics, then they will be immersed in it soon enough, and that is fine.  Perhaps an if-elif section at the top of your scripts, detecting something specific to your environment, that sets variables for log path and similar?  Or comment/uncomment lines...  No need to change anything if you don't want, just expressing my thought.

 

Link to comment

Hows this for the top part?


if [ -d /boot/logs ]
   then LOGDIR="/boot/logs"
   else if [ -d /boot/var/log ]
           then LOGDIR="/boot/var/log"
           else mkdir /boot/logs
                LOGDIR="/boot/logs"
        fi
fi

 

The respective change at the bottom would be

 

logger "Saving current syslog to ${LOGDIR}/syslog.txt"
todos < /var/log/syslog > ${LOGDIR}/syslog.txt
chmod a-x ${LOGDIR}/syslog.txt

 

Although could just define SYSLOG_FILE at the top, I want the top section to be easy to cut and paste to other scripts.

 

Thoughts?

 

 

Any thoughts on a drop in inftrastructure on user supplied scripts?

(Perhaps we shoudl start a separate thread on that).

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...