Table of Content
- Introduction
- Requirements
- Bash Script: Explanation and Workflow
- Implementation Steps
- Tips and Troubleshooting
- Conclusion
1. Introduction
Taking automatic screenshots daily can help monitor activity or save important screen views. This article explains the steps to set up auto screenshots on Xubuntu using a bash script that runs automatically each time you log in.
2. Requirements
Before starting, make sure you have:
- Xubuntu or a system based on XFCE Desktop Environment.
- The
xfce4-screenshooter
package installed. - A text editor such as
nano
orvim
. - Terminal access with user permissions.
3. Bash Script: Explanation and Workflow
Here is the bash script to take automatic screenshots every 60 seconds:
#!/bin/bash
# Root directory for storing screenshots
ROOT_DIR="/home/adb/Pictures/daily-screenshots"
# Lock file to prevent duplication
LOCK_FILE="/tmp/auto_screenshot.lock"
# Ensure only one instance is running
exec 200>"$LOCK_FILE"
flock -n 200 || exit 1
# Ensure the root storage directory exists
mkdir -p "$ROOT_DIR"
# Environment variables for graphical display
export DISPLAY=:0
export XDG_RUNTIME_DIR=/run/user/$(id -u)
# Log file
LOG_FILE="/home/adb/Pictures/screenshot_log.txt"
# Delete folders older than 7 days (executed once at login)
find "$ROOT_DIR" -mindepth 1 -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \;
# Function to handle script termination
cleanup() {
echo "Stopping auto_screenshot.sh" >> "$LOG_FILE"
rm -f "$LOCK_FILE" # Remove lock file
exit 0
}
# Capture termination signals (SIGTERM, SIGINT)
trap cleanup SIGTERM SIGINT
# Infinite loop to take screenshots every 60 seconds
while true; do
# Date for folder name (format: DD MMM YYYY)
DATE_FOLDER=$(date "+%d %b %Y")
OUTPUT_DIR="$ROOT_DIR/$DATE_FOLDER"
# Ensure the date storage directory exists
mkdir -p "$OUTPUT_DIR"
# Screenshot file name format (example: 19 Jan 2025, 22:30.png)
TIMESTAMP=$(date "+%d %b %Y, %H:%M")
OUTPUT_FILE="$OUTPUT_DIR/$TIMESTAMP.png"
# Take a screenshot
xfce4-screenshooter -f -s "$OUTPUT_FILE" >> "$LOG_FILE" 2>&1
# Wait 60 seconds before taking the next screenshot
sleep 60
done
Workflow
- Screenshot Storage: Screenshots are saved in
/home/adb/Pictures/daily-screenshots
with sub-folders based on the date. - Automatic Cleanup: Folders older than 7 days are deleted to save storage space.
- Duplication Prevention: A lock file is used to prevent the script from running twice.
- Infinite Loop: Screenshots are taken every 60 seconds continuously.
4. Implementation Steps
1. Create the Bash Script
Open a terminal and create a new file:
nano ~/Pictures/auto_screenshot.sh
Copy and paste the script above into the file.
Save the file with CTRL + O
, then exit with CTRL + X
.
2. Grant Execution Permission
To make the script executable, change its permissions:
chmod +x ~/Pictures/auto_screenshot.sh
3. Add Script to Autostart Session
Use Xubuntu’s autostart session feature to run the script automatically at login:
-
Open Settings Manager in Xubuntu.
-
Go to Session and Startup > Application Autostart Tab.
-
Click Add to create a new entry.
-
Fill out the form as follows:
- Name: Auto Screenshot
- Command:
/home/adb/Pictures/auto_screenshot.sh
- Description: Script to take automatic screenshots every 60 seconds.
-
Click OK to save changes.
5. Tips and Troubleshooting
- Check Logs: If the script is not working, check the logs in
/home/adb/Pictures/screenshot_log.txt
for errors. - Notifications: Enable notifications by adding the
notify-send
command if you want status updates. - Permissions: Ensure the script has permission to create files in the target directory.
6. Conclusion
With a bash script and autostart session configuration on Xubuntu, you can automatically take daily screenshots without manual intervention. This system is highly useful for documentation or monitoring computer activity.
Hope this guide is helpful! 🎉
Data Penggunaan Harian
Tanggal | Ukuran File |
---|---|
19 Januari | 10 MB |
20 Januari | 40 MB |
21 Januari | 15 MB |
22 Januari | 1 GB |


