1

Os : debian9.
Create a log file.

touch $HOME/logout.log

Add a command in .bash_logout.

vim .bash_logout
echo  $(date)  >>  $HOME/logout.log

Now login with account name test,and create /home/test/logout.log.

I want to write date info in /home/test/logout.log,why no date info after reboot?
Nothing in /home/test/logout.log after reboot.

  • 1
    You need to be logged in to a user, then when you logout this file gets executed. Hence the name bash_logout. Can you specify exactly when you want echo $(date) to be executed? Also you can just do date >> $HOME/logout.log – Hunter.S.Thompson Jan 31 '18 at 13:30
  • At reboot to write time info into $HOME/logout.log. – scrapy Jan 31 '18 at 13:35
  • date >> $HOME/logout.log is enough. – Tomasz Jan 31 '18 at 13:42
3

first

echo $(date) >> $HOME/logout.log

can be replace by

date >> $HOME/logout.log

next,

.bash_logout is run when you disconnect, not when there is a reboot.

  • A reboot is when you restart host, effectively killing (more or less nicely) all it process, database, webserver and so on.

  • A logout is when you (or any user) disconnect from interactive session, the host stay up, as well as database and the like.

If you want script to be executed during a schedule shutdown, place then in /etc/init.d , then have a link like K01-trace-logout in /etc/rcX.d where X is your run-level (result of who -r).

finaly

Be aware that unexpected shutdown (power outage, disk I/O freezing) will leave no trace in log file.

  • I have a question, would $HOME in this case be /root or would it be the the home directory of the user who initiates the reboot, assuming the script is in /etc/rc6.d. – Hunter.S.Thompson Jan 31 '18 at 14:03
  • @Hunter.S.Thompson No, unless you have way to trap which user start shutdown (by way of sudo for instance)... – Archemar Jan 31 '18 at 14:06
1

You need to run bash as login shell. You need to login as the user and when you log-out from the interactive login shell - the .bash_logout script will be executed.

  • Command in .bash_logout execute in login shell ,most of time i'm in non-login shell (graphic mode). – scrapy Jan 31 '18 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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