Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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: 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

share|improve this question
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

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'
share|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 (search for "history -c")(grr, why can't I presss enter in this comment form?!) – KIAaze May 22 '11 at 11:40
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: , 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:

# 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
        status FOO
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1

exit 0

( script based on )

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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