When I reboot my server over SSH, the session just hangs instead of nicely logging me out, so that I can continue to use the current terminal. I tried the following to fix this: https://bbs.archlinux.org/viewtopic.php?id=50089 But then my bash history isn't saved before rebooting.

Is there a way to nicely log out all users (so that their bash history is saved) before a reboot/halt command? i.e. save bash history and end user sessions before reboot/halt?

OS: Ubuntu Server 11.04

link|improve this question
4  
Put your solution as an answer and mark it as such. Shouldn't be in the question area. – Corey S. May 22 '11 at 11:56
How do I do that? – KIAaze May 22 '11 at 15:24
Ok, done, after the 8 hour wait period was over. – KIAaze May 24 '11 at 23:35
feedback

2 Answers

You need to execute something like history -ain each user session. I can't think of a "nice" way to do this but using PROMPT_COMMAND in bash would work, PROMPT_COMMAND is an env variable for a command that is execute every time bash returns to a prompt, i.e. it automatically flushes the history to .bash_history on each command execute. Works but probably not the best solution. Put this in the .bashrc or in a profile.d file (if you are on profile.d compatible distro).

export PROMPT_COMMAND='history -a'
link|improve this answer
I found "history -w" while looking for something similar. So I compared the 2 out of curiosity. "history -a" is definitely better because it avoids history duplication in ~/.bash_history. cf linux.die.net/man/1/bash (search for "history -c")(grr, why can't I presss enter in this comment form?!) – KIAaze May 22 '11 at 11:40
feedback
up vote 0 down vote accepted

I found a solution:

I simply needed to use "killall -u" instead of the "skill -KILL -u". :) As indicated here: http://www.cyberciti.biz/tips/howto-linux-kill-and-logout-users.html , the command "skill" seems to be obsolete and others like pkill and killall should be used instead. pkill didn't do what I want, but killall did. :)

So here's the full solution:

1) Create a script /etc/init.d/killusers containing the following:

#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#

# Get function from functions library
#. /etc/init.d/functions
. /lib/lsb/init-functions

# Start the service FOO
start() {
        #initlog -c "echo -n Starting FOO server: "
        #who | cut -d " " -f1 | uniq | xargs killall -u
        #who | cut -d " " -f1 | uniq | xargs skill -KILL -u
        #success $"FOO server startup"
        echo "Do nothing"
}

# Restart the service FOO
stop() {
        #initlog -c "echo -n Stopping FOO server: "
        who | cut -d " " -f1 | uniq | xargs killall -u
        #who | cut -d " " -f1 | uniq | xargs skill -KILL -u
        #who | cut -d " " -f1 | uniq | xargs pkill -STOP -u
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status FOO
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0

( script based on http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html )

2) Make it executable:

sudo chmod +x /etc/init.d/killusers

3) Add it to the shutdown scripts:

sudo update-rc.d killusers defaults

Note 1: I think runlevel 6 should be enough, but I went for defaults just to be sure. The script could probably also only contain the kill command if it is just run during shutdown.

Note 2: To save the bash history immediately, you can use (thanks to HampusLi):

history -a

So you could also just run:

history -a && sudo reboot

But I wanted it to work for any user and without having to create an alias, script or similar.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.