~/.bash_logout — User-Specific Logout Cleanup Script
What You Will Learn
- Understand the purpose of
~/.bash_logoutand when it executes. - Learn how Bash uses this file during session termination.
- Distinguish
.bash_logoutfrom other shell startup files like.bashrcand.bash_profile. - Configure automated cleanup and reporting routines for WordPress VPS environments.
- Apply best practices for safe session exit and logging.
1. What is ~/.bash_logout
~/.bash_logout is a user-specific script that executes automatically whenever you exit a login shell.
It is Bash’s way of performing “end-of-session” tasks, similar to how .bash_profile handles startup initialization.
The file is optional — if it doesn’t exist, Bash simply ignores it.
Typical tasks performed here include:
- Clearing the terminal screen.
- Logging the logout time or session data.
- Deleting temporary files.
- Unmounting remote file systems.
2. When It Executes
| Event | .bash_logout Trigger |
|---|---|
| SSH session ends | ✅ Executes before disconnecting |
| Console session (TTY) ends | ✅ Executes |
| Interactive subshell closed | ❌ Not executed |
| Cron or background script ends | ❌ Not executed |
In other words, .bash_logout runs only for login shells — for example, when you exit an SSH session. |
3. Relationship with Other Bash Files
| File | Stage | Description |
|---|---|---|
~/.bash_profile | Login start | Initializes environment variables, PATH, and sources .bashrc |
~/.bashrc | Interactive mid-session | Sets aliases, functions, and prompt customization |
~/.bash_logout | Logout end | Executes cleanup or post-session commands |
So .bash_logout complements the startup process by ensuring the shell finishes cleanly. |
4. Where It Is Located and How to Access It
| User | Path |
|---|---|
| root | /root/.bash_logout |
| ubuntu | /home/ubuntu/.bash_logout |
| admin | /home/admin/.bash_logout |
| Typical file structure: |
/home/
├── ubuntu/
│ ├── .bash_profile
│ ├── .bashrc
│ ├── .bash_aliases
│ └── .bash_logout
└── admin/
├── .bash_profile
├── .bashrc
├── .bash_aliases
└── .bash_logout
To create or edit:
cd ~
nano ~/.bash_logout
5. Core Syntax & Example
A .bash_logout file contains regular Bash commands executed at session termination.
Example 1: Clear the screen
clear
Behavior: The terminal clears automatically upon logout.
Example 2: Write session end log
echo "Session closed by $(whoami) at $(date)" >> ~/.session.log
Behavior: Records each logout event with username and timestamp.
Example 3: Clean up temporary files
rm -rf /tmp/wp-maintenance-*
Behavior: Removes leftover temporary files from WordPress maintenance tasks.
Example 4: Unmount remote backup drive
umount /mnt/wpbackup 2>/dev/null
Behavior: Safely unmounts backup drives if they were mounted during the session.
Example 5: Combine multiple cleanup steps
# ~/.bash_logout
echo "Goodbye $(whoami)! Logging out at $(date)" >> ~/.session.log
rm -rf /tmp/*.lock /tmp/*.tmp
clear
Result upon logout:
Goodbye root! Logging out at Sun Oct 12 22:48:31 UTC 2025
6. Why Use .bash_logout
| Reason | Description |
|---|---|
| Clean exit | Ensures temporary or sensitive files are removed. |
| Security | Prevents residual session files from persisting on shared systems. |
| Logging | Maintains a simple audit trail of session activity. |
| Automation | Useful for cleanup scripts in managed WordPress servers. |
| Consistency | Guarantees predictable behavior after every SSH exit. |
7. WordPress VPS Practical Use Cases
| Task | Example Command | Purpose |
|---|---|---|
| Remove temporary deployment files | rm -f /tmp/wp-deploy-* | Prevents leftover temp data from plugins/themes. |
| Clear WP-CLI cache | wp cache flush --allow-root | Forces clean cache on exit. |
| Write activity log | echo "Session ended by root" >> /var/log/wp-session.log | Keeps record of administrative activity. |
| Unmount remote storage | umount /mnt/backupdrive | Prevents stale mounts after backups. |
| Clear screen | clear | Keeps console clean before logout. |
8. Integration Example
Complete working sample for a WordPress administrator:
# ~/.bash_logout — WordPress VPS Session Cleanup
echo "Session closed for $(whoami) on $(hostname) at $(date)" >> /root/session.log
# Clean up old maintenance and lock files
rm -f /tmp/wp-maintenance-*
rm -f /tmp/*.lock
# Clear WP-CLI cache to reduce disk usage
if command -v wp >/dev/null 2>&1; then
wp cache flush --allow-root
fi
# Clear terminal
clear
This setup ensures every SSH session ends cleanly and logs are retained for auditing.
9. Best Practices
- Keep
.bash_logoutsimple — avoid long-running commands. - Use absolute paths for reliability (e.g.,
/tmp/,/root/). - Redirect errors silently:
command 2>/dev/null
- Avoid interactive commands that require user input.
- Log important activity using timestamps.
- Test the script manually with:
bash ~/.bash_logout
- Backup before major edits:
cp ~/.bash_logout ~/.bash_logout.bak
10. Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
.bash_logout not executed | Not a login shell | Ensure SSH session runs Bash as login shell |
| Script hangs during logout | Contains interactive commands | Remove read, pause, or user prompts |
| Cleanup commands fail silently | Missing permissions | Run with correct user privileges or fix file ownership |
| Logs not generated | Output path invalid | Use absolute paths like /root/session.log |
11. Quick Lab
Objective: Create a simple logout procedure that clears temporary files and records logout events.
- Edit the file:
nano ~/.bash_logout
- Add:
echo "Logout event: $(whoami) at $(date)" >> /root/session.log
rm -rf /tmp/wp-*
clear
- Save and exit.
- Log out and reconnect via SSH.
- Verify log entry:
cat /root/session.log
Expected result:
Logout event: root at Sun Oct 12 22:50:41 UTC 2025
12. Glossary
| Term | Definition |
|---|---|
| Login shell | The shell invoked when logging into a system (e.g., SSH). |
| Cleanup routine | Automated removal or reset process executed at logout. |
| Sourcing | Running a file’s contents within the current shell. |
| Non-interactive shell | A shell that runs scripts without user input. |
| Audit log | File used to track administrative activity or events. |
13. Mini-Quiz
- When does
.bash_logoutexecute? - Can
.bash_logoutrun automatically in cron? - What kind of tasks are suitable for
.bash_logout? - Why should you avoid prompts or interactive commands in it?
- How do you confirm whether
.bash_logoutran successfully?
14. Summary
~/.bash_logoutexecutes once when a login shell ends, usually during SSH logout.- It is ideal for cleanup, logging, and secure session termination.
- Does not run for non-login shells or cron jobs.
- Keep the file lightweight, silent, and safe for automation contexts.
- For WordPress VPS administrators, it’s valuable for removing temporary files, clearing caches, and logging session exits.
Would you like me to continue next with “System-wide configuration: /etc/profile”, following the same structured, WordPress VPS–aligned format?