Enabling Stock Cron
The Patch webOS GPS Tracking guide had a nice bit about enabling crond on the Pre. It looks like its already pre-installed, just not set up to start. You will need to edit /etc/event.d/mod-crond (don't modify the existing crond startup-script) to enable cron to start. The settings should look like this:
# -*- mode: conf; -*- # jobfile for busybox crond, with pre-start commands to create the # directory and files it wants by default. That there's not 'start # on' stanza is intentional: it's for development use only. We can # turn it on later if we need it, e.g. to keep the clock synced. It # does work. # below did not work on 188.8.131.52 #start on stopped configure #stop on started start_update # If the above do not work with WebOS above 1.3, try these... # The above doesn't start service since webos 1.3 # below worked on 184.108.40.206 start on stopped finish stop on runlevel [!2] respawn exec /usr/sbin/crond -f -L /var/log/crond pre-start script mkdir -p /var/spool/cron/crontabs ln -sf /etc/cron/crontabs/root /var/spool/cron/crontabs/root end script
A few notes here. Unlike the Dropbear and other scripts I make my upstart scripts so that essential services don't stop when the device exists runlevel 2. This seems to shutdown these services whenever the device locks. I prefer that my upstart services stop only when the updater runs. Also I have made cron log to a separate file under /var/log so that it can be monitored better.
One thing to note about the standard script for cron on the Pre is that the /var/spool/cron/crontabs directory is created every time the process runs and a symlink to /etc/cron/crontabs/root is created. To my surprise this file does not exist. So we need to create it:
sudo mkdir -p /etc/cron/crontabs sudo sh -c 'echo > /etc/cron/crontabs/root'
Now to start up crond:
sudo -i initctl start crond
- Note for clarification: I attempted this and it all works OK, but I can't figure out how to actually schedule jobs. I modified /etc/cron/crontabs/root, but nothing seems to happen. I also tried crontab -e, and that doesn't seem to do anything either. I suggest adding a note here clarifying what needs to be done to actually add a cron job. -irwinr
- Clarification: Edit /etc/cron/crontabs/root with test content something like this:
# format: # minute(0-59) hour(0-23) day(1-31) month(1-12) weekday(0-6) command * * * * * echo "CronTest -" `date` >> /var/crondtest
Then restart crond like this:
sudo -i initctl stop crond sudo -i initctl start crond
This test will result in a test file at /var/crondtest that grows at the rate of one line per minute. You can count the lines and output the end of the file with the following commands. Also try a reboot to make sure everything is starting up properly. If everything is working make sure to remove or comment out the CronTest task and restart crond.
grep -c "" /var/crondtest tail /var/crondtest
Alternative (ipkg-opt) Method
- Another request for clarification: It's not clear which of these two methods are preferred. This method appears to not work when the screen is off (rendering cron pretty useless), the above method appears to delete the cron entries every time the cron daemon starts. Has anyone worked out either of these issues? -irwinr.
crond is a system that allows command to be run at specified intervals.
Do not use the built in crontab -e as it is overwritten on each boot.
Optware has cron available as an installable package, and using /opt/etc/cron.d/ for cron files will not conflict with any Palm files.
sudo -i mount -o remount,rw / ipkg-opt update ipkg-opt install cron /opt/bin/crontab -e # Add script and intervals here mount -o remount,ro /
This does not seem to work when the phone's screen turns off. I tried it plugged in for two minutes then unplugged, and turned the screen off for two minutes. I ran
echo `date` >> sleep.log
# cat sleep.log Wed Jul 1 18:27:01 CDT 2009 Wed Jul 1 18:28:01 CDT 2009 Wed Jul 1 18:30:16 CDT 2009 ## Turned the screen back on. It seems to play catch up. Wed Jul 1 18:30:16 CDT 2009
Alternative to Crond Using Only Upstart
The following method is an alternate way to repeat a command at set time interval, but not if you need to run a command at a specific time. I developed this solution with the hope to get around the inconsistent behavior of cron when the phone falls asleep. Unfortunately, this way really doesn't work any better, but I currently use this method on my own Pre with some success. In this example, I setup a script to execute every 10 minutes.
Add a new upstart script file to the /etc/event.d directory, and name it whatever you want the upstart service to be called:
start on stopped finish stop on runlevel [!2] console none respawn exec /var/home/root/Every10Minutes.sh
This upstart script will run the Every10Minutes.sh file and respawn it when it dies, so the trick is that a sleep command must be added to the end of the Every10Minutes.sh file:
#!/bin/sh #Command to run every 10 minutes goes here sleep 600
I recommend rebooting the Pre at this point to make sure the new upstart script takes effect, although you should be able to just do "start <servicename>" once the upstart script is added to the event.d directory. In this case, "sleep 600" keeps the script alive for 10 minutes, but without it doing anything. When the script dies after the sleep concludes, upstart respawns it and the command is performed again.
Forcing Device to Awaken Out of Sleep Mode to Perform Cron (or Upstart) Jobs
I think I just discovered a method for waking the device periodically, thus allowing it to perform cron jobs or other tasks even when not attached to a charger and not awake. I can't believe I didn't think of it before. If you do anything to cause a notification to appear on the phone, it will wake-up to display the notification. Once it's awake, it will execute any pending cron jobs, upstart jobs, or continue executing any already-executing script files.
I can imagine configuring a desktop computer to periodically awaken the device by sending it an email or a text message. I can't think of anything that can be configured on the device itself to cause notifications to get generated at a set interval, but that would be ideal, of course.
Even better, I just discovered the excellent homebrew app Nodoze. It appears to do exactly what I've been longing for: keeps the device awake without an open card and without the screen remaining lit. I tested it while Govnah was running in Screenstate mode, and even though Nodoze was running and apparently keeping the device awake, the screenstate was off and Govnah was showing the CPU speed running in slow (250MHz in my case) mode. I've had it running for a couple of days, and haven't noticed any dramatic effect on battery life.