Advertisement
Guest User

Untitled

a guest
Jul 20th, 2015
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. # Separate Command Histories for Restored Terminal Sessions
  2.  
  3. # Terminal assigns each terminal session a unique identifier and
  4. # communicates it via the TERM_SESSION_ID environment variable so that
  5. # programs running in a terminal can save/restore application-specific
  6. # state when quitting and restarting Terminal with Resume enabled.
  7.  
  8. # The following script saves and restores the bash command history
  9. # independently for each restored terminal session. It also merges
  10. # commands into the global history for new sessions. Because of this
  11. # it is recommended that you set HISTSIZE and HISTFILESIZE to larger
  12. # values (their default value is 500). Old sessions are periodically
  13. # deleted.
  14.  
  15. if [ -z "$BASH_SESSION" ] && [ -n "$TERM_SESSION_ID" ]; then
  16.  
  17. # Only perform this setup once per shell session (which shouldn't
  18. # happen unless the user's ~/.bash_profile executes /etc/profile,
  19. # which is normally redundant).
  20. BASH_SESSION=1
  21.  
  22. # Set up the session directory/file.
  23. if [ -z "$BASH_SESSION_DIR" ]; then
  24. BASH_SESSION_DIR="$HOME/.bash_sessions"
  25. BASH_SESSION_FILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.session"
  26. elif [ -z "$BASH_SESSION_FILE" ]; then
  27. BASH_SESSION_FILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.session"
  28. fi
  29. mkdir -p "$BASH_SESSION_DIR"
  30.  
  31. # Arrange for session-specific shell command history. Users can
  32. # disable this via the existence of the following file.
  33. [ -f ~/.bash_session_no_history ] && BASH_SESSION_HISTORY=0 || BASH_SESSION_HISTORY=1
  34. if [ $BASH_SESSION_HISTORY -eq 1 ]; then
  35. BASH_SHARED_HISTFILE="$HISTFILE"
  36. BASH_SESSION_HISTFILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.history"
  37. BASH_SESSION_HISTFILE_NEW="$BASH_SESSION_DIR/$TERM_SESSION_ID.historynew"
  38. # If the session history doesn't exist, copy the shared history
  39. if [ -f "$BASH_SHARED_HISTFILE" ] && [ ! -f "$BASH_SESSION_HISTFILE" ]; then
  40. cp "$BASH_SHARED_HISTFILE" "$BASH_SESSION_HISTFILE"
  41. else
  42. # Ensure the file exists and doesn't get expired.
  43. touch "$BASH_SESSION_HISTFILE"
  44. fi
  45. history -r "$BASH_SESSION_HISTFILE"
  46. : >| "$BASH_SESSION_HISTFILE_NEW"
  47. HISTFILE="$BASH_SESSION_HISTFILE_NEW"
  48. fi
  49.  
  50. if [ "$SHLVL" -eq 1 ]; then
  51. # Restore previous session state.
  52. if [ -f "$BASH_SESSION_FILE" ]; then
  53. . "$BASH_SESSION_FILE"
  54. rm "$BASH_SESSION_FILE"
  55. fi
  56.  
  57. # Save the current state.
  58. bash_save_session_state() {
  59. if [ "$SHLVL" -eq 1 ] && [ -n "$BASH_SESSION_FILE" ]; then
  60. echo -n Saving session...
  61. echo echo Restored session: $(date) > "$BASH_SESSION_FILE"
  62.  
  63. # Users can add custom state by defining the following
  64. # function. e.g., to save an environment variable:
  65. # bash_session_save_state() { echo MY_VAR="'$MY_VAR'" >> "$BASH_SESSION_FILE"; }
  66. declare -F bash_session_save_state >/dev/null && bash_session_save_state
  67.  
  68. # Save new history commands.
  69. if [ $BASH_SESSION_HISTORY -eq 1 ]; then
  70. history -a
  71. cat "$BASH_SESSION_HISTFILE_NEW" >> "$BASH_SHARED_HISTFILE"
  72. cat "$BASH_SESSION_HISTFILE_NEW" >> "$BASH_SESSION_HISTFILE"
  73. # Empty this session's history file to keep track of
  74. # which commands have already been copied.
  75. : >| "$BASH_SESSION_HISTFILE_NEW"
  76. # Read/write the files via the history command so they
  77. # are truncated as appropriate.
  78. history -r "$BASH_SHARED_HISTFILE"
  79. history -w "$BASH_SHARED_HISTFILE"
  80. history -r "$BASH_SESSION_HISTFILE"
  81. history -w "$BASH_SESSION_HISTFILE"
  82. fi
  83.  
  84. echo completed.
  85. fi
  86. }
  87.  
  88. # Delete old session files. Do not do this more frequently
  89. # than once a day.
  90. BASH_SESSION_TIMESTAMP_FILE="$BASH_SESSION_DIR/_expiration_check_timestamp"
  91. bash_delete_expired_session_state() {
  92. if ([ ! -f "$BASH_SESSION_TIMESTAMP_FILE" ] || [ -z $(find "$BASH_SESSION_TIMESTAMP_FILE" -mtime -1d) ]); then
  93. local bash_session_lock_file="$BASH_SESSION_DIR/_expiration_lockfile"
  94. if shlock -f "$bash_session_lock_file" -p $$; then
  95. echo -n Deleting expired sessions...
  96. local delete_count=$(find "$BASH_SESSION_DIR" -type f -mtime +2w -print -delete | wc -l)
  97. [ "$delete_count" -gt 0 ] && echo $delete_count completed. || echo none found.
  98. touch "$BASH_SESSION_TIMESTAMP_FILE"
  99. rm "$bash_session_lock_file"
  100. fi
  101. fi
  102. }
  103.  
  104. # Update saved session state when exiting.
  105. bash_update_session_state() {
  106. bash_save_session_state && bash_delete_expired_session_state
  107. }
  108. trap bash_update_session_state EXIT
  109. fi
  110. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement