How to Run a Bash Script via Cron in Termux: A Quick Guide to Termux Crontab

termux crontab square app icon

If you’re looking to automate tasks on your Android device using Termux, the termux crontab setup is your go-to solution. Cron is a powerful tool that schedules scripts and commands to run automatically at specified intervals, making it perfect for repetitive tasks. In this blog, we’ll dive into the basics of setting up cron jobs in Termux, explore some use cases, and provide a quick introduction to how cron works.

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. Users schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. It’s a powerful utility for automating maintenance and administration tasks.

Basic Cron Syntax

The cron syntax is straightforward once you get the hang of it. A crontab file contains one job per line, with the following structure:

* * * * * command_to_execute
| | | | |
| | | | +-- Day of the week (0 - 7) (Sunday=0 or 7)
| | | +---- Month (1 - 12)
| | +------ Day of the month (1 - 31)
| +-------- Hour (0 - 23)
+---------- Minute (0 - 59)

Each asterisk (*) represents a time field and can be replaced with specific values to define when the command should run. For example, if you want a command to run every day at 2:30 AM, you would use:

30 2 * * * command_to_execute

Setting Up Cron in Termux

1. Install Cron in Termux: If you haven’t already, you’ll need to install the cron package in Termux. You can do this with the following command:

$ pkg install cronie X

After installation, start the cron service:

$ crond

Create a Crontab File: To schedule tasks, you’ll need to edit your crontab file. You can create or edit your crontab by typing:

$ crontab -e 

This will open an editor where you can define your cron jobs.

0 5 * * * /path/to/script.sh 

Understanding Cron Syntax: Cron jobs follow a specific syntax:

* * * * * command/to/run 


Each asterisk represents a time unit (minute, hour, day of month, month, day of week). For example:
This runs script.sh every day at 5:00 AM.

Running a Bash Script Every 5 Minutes: If you want to run a script every 5 minutes, you can add the following line to your crontab:

*/5 * * * * /path/to/script.sh

Running a Script at a Specific Time: To run a script at a specific time, such as 2:30 PM every day:

30 14 * * * /path/to/script.sh

Use Case Examples:

Sync Files Automatically: Use rsync to sync directories between your Android device and a server every hour.

0 * * * * rsync -av /source/directory/ /destination/directory

Backup Important Files: Create a backup of critical files every night at midnight.

0 0 * * * tar -czf /backup/location/backup.tar.gz /important/files/

Check for Software Updates: Automatically check for and install package updates daily.

0 2 * * * pkg update && pkg upgrade -y

Conclusion

Setting up termux crontab allows you to leverage the full potential of task automation on your Android device. Whether you’re looking to sync files, backup data, or run routine maintenance tasks, cron in Termux makes it simple and efficient. Start automating your tasks today and experience the power of cron right on your mobile device!

Leave a Reply

Your email address will not be published. Required fields are marked *