For anyone who doesn't know, CRON is a low-level task scheduler that runs in the background, and running tasks provided in a configuration file known as a CRONTAB. Vixie-CRON is a tiny, yet extremely efficient CRON daemon written “back in the day” (1987) by Paul Vixie. It is essentially THE de-facto CRON and therefore was chosen to be ported by me to android.
CRON has the ability to schedule multiple tasks to run at various times, and (unlike the Android system) is EXTREMELY RELIABLE about preforming those scheduled tasks.
As Vixie-CRON is included with Sapphire, AND is set to start up by default, there is really only one thing that needs to be done in order to operate it, and that is to set up a CRONTAB!
Vixie-CRON will look for it's system CRONTAB in /data/cron/systab, which must be created as it is not included by default.
First you must set up your cron directory (from adb shell or a terminal emulator):
busybox mkdir -p /data/cron
Next is to create a crontab, and then push it to the systab location:
adb push mycrontab /data/cron/systab
Now to get the daemon started, simply reboot the phone!
A CRONTAB is nothing more than a text file that describes what tasks should be run at what times and what intervals.
Below is a sample crontab (taken from a Gentoo linux install):
0 * * * * root rm -f /var/spool/cron/lastrun/cron.hourly 1 3 * * * root rm -f /var/spool/cron/lastrun/cron.daily 15 4 * * 6 root rm -f /var/spool/cron/lastrun/cron.weekly 30 5 1 * * root rm -f /var/spool/cron/lastrun/cron.monthly */10 * * * * root test -x /usr/sbin/run-crons && /usr/sbin/run-crons
So what do all those numbers and asterisks mean? Well you're in luck because I'm about to explain it! Basically every line in a crontab defines a command to be run at a given interval. In general, the layout is this:
[minute] [hour] [day] [month] [week] [user] [command]
Each of these elements mean the following:
You may also place an asterisk ('*') in any of these fields, to denote that the command should be run for any value in that field (e.g. every day of the week). You may also place division as done in the above example ('*/10' in the above example denotes that the command will be run every 10th minute).
So to look at the example above, the following line:
30 5 1 * * root rm -f /var/spool/cron/lastrun/cron.monthly
Will cause CRON to remove the file/directory /var/spool/cron/lastrun/cron.monthly on the first day of every month, at 05:30 (5:30 a.m.).
Now let's try building our own CRONTAB. Sapphire includes a handy tool that auto-zipaligns every APK on the system (zipalign_apks). Let's create a cron task that will do that weekly so that we don't have to ever worry about it! We'll set it up to run every Monday morning at 2:30 a.m. Sound good? Good!
Here are our values we will pick for the various options:
Putting it all together, we get a CRONTAB with one line that looks like this:
30 2 * * 1 root zipalign_apks
Next you need to push that file to the phone, and give it proper permissions:
adb push systab /data/cron/systab adb shell chown 0.0 /data/cron/systab adb shell chmod 644 /data/cron/systab
Now just reboot your phone, and cron should start up and being scheduling your tasks!