If you like my work, please consider donating!

Vixie-CRON Task Scheduler

Background

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.

How to use Vixie-CRON

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.

Since Vixie-CRON is a daemon and therefore constantly runs periodically (typically checking for new operations once a minute), it will cause a slight drain on your battery. For that reason, unless /data/cron/systab exists (i.e., unless CRON actually has something to do), Vixie-CRON will NOT be started automatically at boot. Vixie-CRON will only start up on boot when the system CRONTAB file is present.

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!

How to Make a CRONTAB

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:

  • [minute]: a number from 0-60 - minute to run the command on
  • [hour]: a number from 0-23 - hour to run the command on
  • [day]: a number from 1-31 - day of the month to run the command on
  • [month]: a number from 1-12 - month to run the command on
  • [week]: a number from 0-6 (0=Sunday) - day of the week to run the command on
  • [user]: a valid username - user to run the command as (probably going to be “root” for all useful purposes on android)
  • [command]: a valid command - the shell command to run

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:

  • [minute]: 30 - run on the 30th minute of the hour (since we're going for 2:30 a.m.)
  • [hour]: 2 - run on the 2nd hour of the day (once again going for 2:30 a.m.)
  • [day]: * - run every day of the month (we will be limiting the day of the week, not day of the month)
  • [month]: * - run every month of the year
  • [week]: 1 - run on Monday
  • [user]: root - we will need root privileges to perform the zipalign
  • [command]: zipalign_apks - our command

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

For security purposes, cron will only run a systab if the file permissions are “safe”. This means that the file cannot be executable, and group/other cannot have write permission. Also, the file must be owned by root. These permissions are setup to guarantee that there is no outside tampering to this file without root access.

Cron itself runs as root, and as such, can run other commands as root, bypassing Superuser whitelists. For that reason, cron won't run unless the systab permissions are secure, and you should verify you understand what is in that systab before allowing cron to run.

Now just reboot your phone, and cron should start up and being scheduling your tasks!

 
documentation/vixie-cron.txt · Last modified: 2010/07/23 06:35 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki