DEV Community

Stephano Kambeta
Stephano Kambeta

Posted on

Termux + Cron — Automating Repetitive Tasks

If you use Termux, you already know how powerful a terminal on your phone can be. But running the same commands by hand gets old fast. Cron brings automation. With cron, tasks run on schedule. Backups, log rotation, notifications, data collection. Let cron run them for you. This post shows practical, safe ways to use cron in Termux, with examples you can copy and adapt.

Why automate with cron in Termux?

  • Save time. Scripts run without your input.
  • Reduce errors. A scripted task is repeatable.
  • Maintain regular maintenance. Backups and updates become routine.
  • Run small projects reliably. Collect data, ping services, or rotate files.

Automation does not remove responsibility. If your tasks touch networks or sensitive data, protect them. See network and incident response posts for hardening advice and planning. For example, read about network security and small business incident response to build safer automation.

Related reading you might find helpful while planning automation:

What cron is and how it works

Cron is a scheduler. It reads a table of jobs, called a crontab, and runs commands at the times you specify. Each line in a crontab has six fields: minute, hour, day, month, weekday, and the command. Example line:

*/30 * * * * /data/data/com.termux/files/home/scripts/backup.sh

That runs the backup every 30 minutes. In Termux, crons are not enabled by default. You install a cron daemon and create a crontab. Below are clear, step by step instructions the way I prefer to explain them: short commands, screenshots not needed, and plenty of examples.

Install cron in Termux

There are two common approaches.

  1. Use cronie or dcron via pkg. This gives you a standard cron daemon.
  2. Use Termux's job scheduler or Termux:Widget when daemon style is not practical on your device.

Example install steps for cronie:

pkg update && pkg upgrade -y
pkg install cronie

Then start the daemon. On many phones you need to run the service manually or add it to a startup script:

crond
# or
crond -b  # run in background

If crond fails to start persistently, check the device settings. Some Android builds kill background daemons. In that case, use Termux:Widget for scheduled tasks or rely on the Termux job scheduler helper tools. If you want more stable remote access for scheduled tasks, use a tunnel like ngrok when appropriate. See guides on running web tools and ngrok for more context.

Create your first crontab

Use crontab -e to edit your crontab. If it is your first time, the editor will open. Add a line like this to run a script every day at 2:30 AM:

30 2 * * * /data/data/com.termux/files/home/scripts/daily-maintenance.sh >/data/data/com.termux/files/home/logs/daily.log 2>&1

Notes:

  • Use absolute paths. Cron does not load your interactive shell profile.
  • Redirect stdout and stderr to a log file. Debugging is easier.
  • Make scripts executable with chmod +x script.sh.

Practical scripts to automate

Here are real tasks I use or recommend. Each snippet shows a small, secure pattern.

Daily backup of a folder

# /data/data/com.termux/files/home/scripts/backup.sh
#!/data/data/com.termux/files/usr/bin/sh
tar -czf /sdcard/backups/home-$(date +%F).tar.gz /data/data/com.termux/files/home/mydata

Crontab entry:

0 3 * * * /data/data/com.termux/files/home/scripts/backup.sh >/data/data/com.termux/files/home/logs/backup.log 2>&1

Update packages weekly

# /data/data/com.termux/files/home/scripts/update-packages.sh
#!/data/data/com.termux/files/usr/bin/sh
pkg update -y && pkg upgrade -y
0 4 * * 0 /data/data/com.termux/files/home/scripts/update-packages.sh >/data/data/com.termux/files/home/logs/update.log 2>&1

Ping a server and notify on failure

# /data/data/com.termux/files/home/scripts/check-server.sh
#!/data/data/com.termux/files/usr/bin/sh
HOST=example.com
if ! ping -c 3 $HOST >/dev/null 2>&1; then
  echo "$(date): $HOST is down" >> /data/data/com.termux/files/home/logs/server-alerts.log
  # Optionally send a notification or email
fi

Automate this every 10 minutes:

*/10 * * * * /data/data/com.termux/files/home/scripts/check-server.sh >/dev/null 2>&1

Security notes and best practices

Automation can expose sensitive data if you are not careful. Follow these basics:

  • Do not store plain passwords in scripts. Use encrypted storage or environment variables where possible.
  • Limit permissions. Scripts should run with the minimum required rights.
  • Keep logs private. Logs can reveal configuration and credentials.
  • Use a VPN when automation touches public networks. See the VPN posts for practical options.

If your automation touches business systems, map it to a simple security plan. That could mean updating inventory, reviewing access, or logging changes. For longer projects, align automation with an incident response plan and threat model. See the posts on cyber security planning and what threat intelligence is to shape safer workflows.

Related security posts:

Avoiding common pitfalls

Here are problems I see often and how to fix them.

Commands run manually but fail under cron

Cause: cron runs a minimal environment. Fix: use full paths and source any needed profiles at the top of the script. Example:

#!/data/data/com.termux/files/usr/bin/sh
. /data/data/com.termux/files/usr/etc/profile
# rest of script

Daemon stops after a reboot or Android kills it

Some devices aggressively shut down background processes. Options:

  • Use Termux:Widget to run tasks on demand or schedule with Android-friendly tools.
  • Run tasks remotely via a small server you control. Use ngrok or a secure tunnel if needed. See the ngrok post for details.
  • Use a remote machine or VPS for mission critical cron jobs.

Scripts use network tools that require extra setup

If your scripts use tools like nmap, netcat, or custom utilities, test them in a simple shell first. You can find guides on installing and using these tools in Termux. If you rely on network scanning, make sure you have authorization. For safer automation, focus on monitoring and notification rather than active scanning unless you own the targets.

Useful tool guides:

Example project ideas you can automate

Below are quick Termux projects that benefit from cron scheduling.

  • Automatic nightly backup to external storage. Compress and copy a few key folders to your SD card.
  • Daily system snapshot. Save a list of installed packages and config files. Useful for recovery.
  • Periodic website uptime checks. Ping and log response times, alert on downtime.
  • Collecting public data. Fetch RSS feeds or public APIs and store summaries.
  • Log rotation and pruning. Delete logs older than X days to save storage.

If you want short project prompts, see the quick termux projects post for ideas you can complete in a day. For advanced automation that interacts with systems, pair your cron scripts with a simple incident response checklist and network policies so automation does not become a liability.

Related inspiration:

When you might not want cron

Cron is great for simple schedules. It is not a full scheduler for complex workflows. If you need dependencies, retries, parallel jobs, or a visual interface, consider a more advanced orchestration approach or run cron jobs inside a small container or VPS where you can install full tooling.

Also, avoid automating tasks that change production systems without review. Automation should reduce risk, not increase it. Always test on a noncritical system first.

Wrapping up

Termux plus cron gives you a lot of power. Start small. Make one script. Schedule it. Check the logs. Iterate. Use secure patterns: absolute paths, minimal permissions, no plain passwords, and a VPN for public networks. When automation grows, fold it into a simple security plan and incident response playbook.

If you want a sample crontab with a few common tasks and logging already wired, tell me the tasks you want automated and I will write the exact scripts and crontab lines in the style you use on your blog.

Further reading and useful links:

Keep it simple. Automate the boring stuff first. Then build from there.

Top comments (2)

Collapse
 
elizabeth_jake_f9d28ceef6 profile image
Elizabeth Jake

Good day everyone, My name is Jake, I'm here to share a good news. If you've ever fallen victim to a crypto scam or lost access to your wallet, DAREK can help you recover your crypto back. They just recovered my money back to me and I'm supper grateful to them If you have similar issues kindly send them a direct message on . [ recoverydarek @ gmail com] .

Collapse
 
mary_elizabeth_c1094f2ee5 profile image
Mary Elizabeth

Hello everyone, I'm Elizabeth I recently lost $650k BTC to NASDAQ investment company while I was trying to double my income through online investment I soon realized I was scammed, It was too much to take in. After months of depression a friend recommended I contact this Crypto Recovery Company on ( recoverydarek @gmail. com ) and within 48 hours of working I got back my stolen bitcoin. I can't still believe it till this day, I am super grateful.