How to Use Termux to Take Screenshots Automatically
How to Use Termux to Take Screenshots Automatically
Do you want to capture your phone screen without touching it? Imagine setting up a background routine that grabs your display state on a strict schedule. That is entirely possible with the Linux environment on your Android device. Whether you are monitoring system activity, archiving daily information, or building a remote automation workflow, setting up automatic screen captures changes how you use your phone.
This comprehensive guide covers everything you need to know about setting up an automated script, troubleshooting common hiccups—such as when the termux-screenshot command not working—and managing your files efficiently. Let us dive right into the complete process.
What is Termux Screenshot Automation?
Termux screenshot automation refers to the programmatic process of capturing an Android device's display output at designated times or intervals using terminal scripts and built-in API tools. Unlike manual screen captures that require physical button presses or quick-settings toggles, this method relies on background execution, shell scripting, and scheduling utilities like cron or loops to handle the task hands-free.
Quick Summary: Automatic Screenshot Setup at a Glance
| Step | Action Required | Tool / Command |
|---|---|---|
| 1 | Install core API package | pkg install termux-api |
| 2 | Install companion app | Termux:API Add-on (F-Droid / Google Play) |
| 3 | Grant storage permissions | termux-setup-storage |
| 4 | Create and run the script | Bash script utilizing termux-screenshot |
Prerequisites for Termux Auto Screenshot
Before you run any scripts, you need to prepare your environment. Termux cannot access your device hardware or storage straight out of the box without the right bridges. Follow these initial setup steps carefully.
- Update your packages: Open Termux and run
pkg update && pkg upgrade -yto ensure all repositories and existing tools are current. - Install the Termux-API package: Run
pkg install termux-apiin your terminal. This package acts as the communication layer between command-line scripts and Android operating system features. - Install the Termux:API Android app: You must download the companion app named "Termux:API" from F-Droid or an equivalent source. Without this companion application installed on your Android system, your terminal commands cannot trigger hardware actions like capturing your display.
- Grant storage permissions: Execute
termux-setup-storageto allow Termux to read and write files to your shared device storage. Accept the permission prompt on your screen.
Full Working Script (Copy-Paste Ready)
Below is a robust, production-ready bash script designed to capture your screen, timestamp the file name to prevent overwriting, and save it directly into your Pictures directory. You can copy and paste this code directly into your Termux terminal.
#!/data/data/com.termux/files/usr/bin/bash
# ==========================================
# Script Name: auto_screenshot.sh
# Description: Takes a timestamped screenshot
# and saves it to shared storage.
# ==========================================
# Define target directory in shared storage
TARGET_DIR="/storage/emulated/0/Pictures/TermuxScreenshots"
# Create the directory if it does not exist
mkdir -p "$TARGET_DIR"
# Generate a unique timestamp for the file name
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
FILENAME="screenshot_$TIMESTAMP.png"
FILEPATH="$TARGET_DIR/$FILENAME"
echo "Initiating screen capture..."
# Execute the capture command and save to path
termux-screenshot > "$FILEPATH"
# Verify if the file was successfully created
if [ -f "$FILEPATH" ]; then
echo "Success! Screenshot saved to: $FILEPATH"
else
echo "Error: Failed to capture screenshot. Check permissions."
exit 1
fi
Step-by-Step Explanation of the Script
Understanding what each line of code does helps you customize the script to fit your exact workflow. Let us break down the script line by line:
#!/data/data/com.termux/files/usr/bin/bash: This is the shebang line. It tells the system which interpreter to use when executing the file, which in this case is the Bash shell inside Termux.TARGET_DIR="...": This variable sets the destination folder path on your device's shared storage. We use the publicPicturesdirectory so you can easily view your captures in your phone gallery app.mkdir -p "$TARGET_DIR": This command creates the target folder if it is missing. The-pflag ensures no error is thrown if the folder already exists.TIMESTAMP=$(date +"%Y%m%d_%H%M%S"): This generates a unique string based on the current year, month, day, hour, minute, and second. This prevents newer screenshots from accidentally overwriting older ones.termux-screenshot > "$FILEPATH": This is the core engine. It triggers the Android screenshot API and pipes the output image data into your designated file path.if [ -f "$FILEPATH" ]; then ... fi: This conditional check verifies whether the file was actually written to disk, providing immediate feedback in your terminal window.
How to Auto-Run Your Screenshot Script
Manually running a script defeats the purpose of automation. To make your device capture screenshots automatically without manual intervention, you can use a continuous loop or a background process.
If you want the script to run at regular intervals—for instance, every 60 seconds—you can modify your script or wrap it in a simple loop:
while true; do
bash /path/to/auto_screenshot.sh
sleep 60
done
To run this process in the background so you can close your terminal session or use other apps, append an ampersand to the execution command:
nohup bash /path/to/auto_screenshot.sh > /dev/null 2>&1 &
Note: Battery optimization settings on modern Android versions may kill background loops after a while. To ensure uninterrupted execution, disable battery optimization for the Termux application in your Android system settings.
Troubleshooting: Why is the Termux-Screenshot Command Not Working?
If you run into errors or your script fails to produce an image, do not panic. The termux-screenshot command not working issue is a common hurdle, usually caused by permission blocks, missing packages, or Android security restrictions. Review these troubleshooting steps to fix it:
- Missing Termux:API App: If your terminal hangs or throws a null error, verify that you installed the separate Termux:API companion app from F-Droid. Having just the command-line package is not enough.
- Storage Permission Denial: Run
termux-setup-storageagain. If Android denies storage access, the script cannot write image files to your shared directories. - Android Security Restrictions (Scoped Storage): Modern Android versions restrict background apps from capturing screens or writing to protected directories arbitrarily. Ensure your script saves files inside public directories like
Picturesor inside Termux's private internal storage directory (~/.termux/). - Headless / Locked State Limitations: On some custom ROMs or heavily restricted Android builds, the system API refuses to capture screenshots while the device display is completely locked or turned off due to security policies. Test the command while your screen is actively awake.
Safety and Legal Considerations
Automating screen captures on mobile devices comes with significant privacy responsibilities. Always keep the following safety and legal guidelines in mind:
- Personal Data Protection: Automated screenshots may inadvertently capture sensitive information, such as passwords, banking details, private messages, or authentication tokens. Secure the folder where you store your captures.
- Consent and Surveillance: Never use automated capture tools to record other individuals' devices, private conversations, or secure application screens without explicit, informed consent. Respect local privacy laws and platform terms of service.
Best Practices for Termux Automation
Follow these expert tips to keep your automated workflows running smoothly over time:
- Implement Cleanup Routines: Automated screenshots consume device storage rapidly. Add a cleanup command to your script, such as
find "$TARGET_DIR" -type f -mtime +7 -delete, which automatically deletes captured images older than 7 days. - Monitor Battery Drain: Frequent background execution wakes your device CPU and drains battery life. Adjust your intervals sensibly based on your actual use case.
- Test Interactively First: Always run
termux-screenshotmanually in your terminal before wrapping it inside complex automation loops or background daemons.
Common Mistakes to Avoid
Avoid these frequent pitfalls when building your automated setup:
- Forgetting to make your script executable. Always run
chmod +x auto_screenshot.shbefore attempting to execute it. - Hardcoding static file names like
screenshot.png, which causes every new capture to overwrite the previous one. - Ignoring Android's aggressive background app killing behavior, which will silently terminate your loops unless battery optimizations are disabled for Termux.
Frequently Asked Questions (FAQs)
Can I take screenshots in Termux without root access?
Yes. The termux-screenshot command utilizes the official Termux:API plugin framework, which interacts with Android system permissions rather than requiring root privileges.
Where are the screenshots saved by default?
If you use the default command without a specified path, the image output is generally directed to your home directory or standard storage output depending on your API version. Using an explicit path like the one in our script ensures you know exactly where your files go.
Can I schedule screenshots to run at specific times daily?
Yes. You can install a cron scheduler package in Termux (such as cronie) or use simple shell script loops with sleep timers to trigger your captures at predetermined hours.
Conclusion
Automating screen captures inside your terminal opens up powerful workflow possibilities for tracking information, monitoring tasks, and building custom Android tools. By following the installation steps, utilizing our copy-paste ready script, and keeping troubleshooting steps in mind, you can set up a reliable automation routine in minutes.
Ready to explore more advanced terminal automations? Check out our other in-depth guides on Termux backup scripts and system monitoring workflows to supercharge your command-line experience today.
Join the conversation