LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to kill the Bash process without clearing the history? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-kill-the-bash-process-without-clearing-the-history-928222/)

kevinx 02-07-2012 09:46 PM

How to kill the Bash process without clearing the history?
 
I'm using Fedora and taking a Intro to Linux course. My teacher requests that we submit screen shots of our output so she can verify we are doing everything correctly. Sometimes, when the commands are long, or the information doesn't really stay on the page, she requests that we submit a screen shot of the history command.

Something is wrong, the history is gone, and she marked me off. She says the commands should be there. I've searched the internet, but can't really find anything that answers this.

Here's what's going on. I open the terminal and log in as root. I enter a few commands around displaying process (like, ps -eF | grep bash). Then I enter a series of kill commands, 3, 15, and 9.

Nothing seems to happen when I use 3 or 15 (I'm assuming they aren't "strong" enough to kill themselves ;), but when I enter the command "kill -9 PID", the current terminal closes, and I am logged back in as my user name.

All of this makes sense to me, except that when then I log back in to root and run a history command. Nothing I entered from the previous session is there. It only displays commands from previous sessions.

So, I have two questions. First, is the teacher wrong? and second, is it possible to kill the terminal with option 9 and retain the history?

Thanks,
Kevin

bigrigdriver 02-07-2012 09:59 PM

history for the root user is stored separately from the normal user's history. If issuing commands as root, then run history command as root to see the root user's history.

kevinx 02-07-2012 10:01 PM

Thanks bigrigdriver, but I am logging back in as root.

bigrigdriver 02-07-2012 10:12 PM

My bad. Try adding this to your user's and root user's .bashrc:

Code:

# append to the history file, don't overwrite it
shopt -s histappend


unSpawn 02-07-2012 11:29 PM

FWIW I actually use
Code:

shopt -s histappend
export PROMPT_COMMAND='history -a'


kevinx 02-08-2012 02:29 AM

Thanks for your replies. So I'm assuming that the command I was using is working correctly and that the teacher is having a brain fart or something. I'll have to look up what the command shopt does, but the rest I understand. Thanks again!

catkin 02-08-2012 04:01 AM

kill -9 kills the named process immediately; if the process is a bash shell, it has no chance to flush its history buffer to the history file or anything else.

unSpawn's technique works because it flushes the history buffer to the history file every time bash generates a command prompt.


All times are GMT -5. The time now is 07:49 AM.