How to Use Cron Jobs in Termux for Task Scheduling
How to Use Cron Jobs in Termux for Task Scheduling
Your phone is a powerhouse. With Termux bringing a Linux environment right to your Android device, you can do way more than just type random commands. But what happens when you want your phone to run scripts, back up files, or fetch data automatically while you're asleep? That's where automated task scheduling comes in. If you've ever wondered how to make your device work for you around the clock, learning how to set up a termux cron job is your next step.
By the end of this guide, you'll know how to install the necessary packages, write your first schedule, and fix the most frustrating background errors.
What is a Termux Cron Job?
A termux cron job is a scheduled task that runs automatically in the background of your Android device at specified times or intervals. On standard Linux systems, the cron daemon handles this. Termux doesn't ship with cron enabled by default, so we install a package called Cronie to get the exact same functionality — no root access needed.
Quick Answer: How to Automate Tasks in Termux
- Update your package repositories with
pkg update. - Install the background scheduler package (Cronie).
- Start the background service daemon.
- Open the scheduler configuration file with
crontab -e. - Add your time rules and script paths, then save the file.
Installation and Setup Guide
Before you can schedule anything, you need the scheduling software on your device. Unlike desktop Linux distributions, Termux requires a specific setup because of how Android handles background processes. If your Termux install itself is out of date, start with how to update and upgrade packages in Termux.
The Install Command Block
Open your Termux app and run these commands in order:
pkg update && pkg upgrade
pkg install cronie
pkg install termux-services
The cronie package brings the cron daemon to your system, while termux-services manages background daemons properly within the Android ecosystem. If either install fails with a missing-package error, check how to fix "unable to locate package" in Termux.
Starting the Service
Enable the service so it starts automatically with new Termux sessions:
sv-enable crond
To start it immediately in your current session without waiting for a restart, run:
sv up crond
If you skip this step, your schedules will never trigger — the daemon has to actually be running before crontab entries do anything.
How to Write Cron Syntax
Cron uses a five-field time pattern combined with a file path to know when and what to run:
* * * * * /path/to/your/script.sh
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7, where 0 and 7 are Sunday)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
For example, to run a backup script every day at 3:30 AM, your crontab line would look like this:
30 3 * * * /data/data/com.termux/files/home/backup.sh
Real Use-Case Example: Automated Daily File Backup
Let's walk through a real scenario. Imagine you keep a journal or important text files in a folder inside Termux, and you want to back them up to shared storage every day at midnight.
Step 1: Create a simple backup script.
Create a file named daily_backup.sh using a text editor (see the Nano and Vim editor guide if you need a refresher) or a quick echo command:
echo "tar -zcvf /sdcard/Download/termux_backup.tar.gz ~/my_files" > ~/daily_backup.sh
chmod +x ~/daily_backup.sh
Step 2: Add it to your crontab.
Open the crontab editor by typing:
crontab -e
Add the following line to the bottom of the file to run it every day at 12:00 AM:
0 0 * * * /data/data/com.termux/files/home/daily_backup.sh
Save and close the file. Your phone will now handle this task automatically every night, provided Termux has permission to access shared storage — run termux-setup-storage first if you haven't already, and see the full Termux storage guide if the backup path gives you trouble.
Troubleshooting: Common Errors
Setting up automation on a mobile OS can be tricky. If your tasks aren't firing, check these first.
1. The Termux "crond not running" Fix
This is the most common issue users face: you set up your jobs, but nothing happens, because the background daemon crashed or never started.
How to fix it:
- Check if the process is active:
ps aux | grep crond - If nothing shows up, start it manually:
crond - If you're using
termux-services, confirm it's running viasv status crond, and start it withsv up crondif needed. - Android's aggressive battery optimization can kill background tasks — exclude Termux from battery saver in your Android system settings, and consider
termux-wake-lock(covered in the FAQ below) for reliability.
2. Environment Path Issues
Cron runs in a minimal shell environment and often doesn't load your .bashrc or .zshrc, so commands like python or node may not be found.
How to fix it: Always use the absolute path to your binaries in scripts and crontab entries. Instead of python script.py, use /data/data/com.termux/files/usr/bin/python /data/data/com.termux/files/home/script.py.
3. File Permission Denied Errors
If your script runs fine manually but fails when cron triggers it silently, it's usually a permissions problem.
How to fix it: Make sure the script has execute permission: chmod +x /path/to/script.sh.
Comparison Note: Cron vs. Alternative Tools
| Feature | Termux Cron (Cronie) | Bash While-Loops | Android Automation (Tasker + Termux Plugin) |
|---|---|---|---|
| Resource Usage | Very low (sleeps until triggered) | High (keeps CPU awake continuously) | Low to Moderate |
| Precision | Exact minute/hour scheduling | Drifts over time | Event or time-based triggers |
| Root Required | No | No | No |
| Best For | Complex Linux-style recurring tasks | Quick, temporary background waits | Deep Android system integration |
A simple bash loop like while true; do ...; sleep 3600; done might look easier, but it drains your battery faster because it keeps the process continuously active. Cron is far more efficient for scheduled intervals — and if you're already comfortable writing loops, the Bash scripting guide for Termux covers that approach too.
Frequently Asked Questions
Do I need to root my phone to use cron jobs in Termux?
No. Cronie runs entirely within the user-space environment of Termux, so you can schedule and run tasks on unrooted Android devices.
Why do my cron jobs stop working when my phone screen turns off?
Android puts apps to sleep to save battery, and Termux can freeze along with them. Acquire a wake lock with termux-wake-lock before starting crond, and whitelist Termux from your phone's battery optimization settings. For jobs that must survive a reboot, pair this with the Termux:Boot app so your wake lock and crond start automatically on startup.
How do I view the output or logs of my cron jobs?
Cron jobs run in the background without an active terminal, so output is lost unless you redirect it. Log it to a file in your crontab entry like this: 0 0 * * * /path/to/script.sh >> /path/to/logfile.log 2>&1.
What if I need to run commands remotely instead of just locally?
If your automation needs to reach another machine or server, pair cron with SSH in Termux to trigger remote commands on a schedule.
Conclusion
Automating your terminal environment opens up real productivity from your mobile device. By installing Cronie, starting the daemon correctly, and keeping Android's battery management in check, a steady termux cron job can handle backups, scripts, and data fetching while your phone sits in your pocket. Take a few minutes to set yours up today, and let your phone do the heavy lifting.
New to Termux itself? Brush up on the basic Termux commands for beginners before diving deeper into automation.
Join the conversation