Scheduled Tasks

From WebOS Internals
Jump to navigation Jump to search

How to schedule shell tasks on webOS reliable

Here I describe a method to schedule shell script tasks on webOS reliable like a cron daemon would on a normal linux system. There is a tutorial how to enable Crond but the daemon does not work correctly. This is because the system goes into sleep mode when pushing the power button and the CPU is being stopped completely and it will not wake up when a cron job is overdue.

Simple timed tasks

My first try was the following implementation:

#!/bin/sh
while true; do
    script.sh
    sleep 900 # 15 minutes
done

Normally this should trigger the script every 15 minutes. But the sleep mode of the CPU prevents even running programs like sleep from being executed. So it continues to run and to count down the time only when the device is awake again. This is not the intended behaviour.

Reliable timed tasks

I ended up with the following script for running a command every 15 minutes.

#!/bin/sh

LASTCHECK=`date -S`

# set the first wakeup
luna-send -n 1 palm://com.palm.power/timeout/set \
	"{'wakeup': true, 'key': 'cmb', 'uri': 'palm://com.palm.power/com/palm/power/activityStart', 'params': '{\"id\": \"com.company.script.check-1\", \"duration_ms\": 30000}', 'in': '00:15:00'}'"
# first time start of the script, optional
script.sh

while true; do
    CURRENT=`date -S`
    DELTA=$((CURRENT-LASTCHECK))

    # Test if the last check is older than 15 minutes
    # If delta is smaller than -10 the clock time has been changed!
    if [ $DELTA -ge 900 ] || [ $DELTA -lt -10 ]; then
        LASTCHECK=$CURRENT
        # Set the next alarm in 15 minutes. Must be more or equal the check time.
        # In worst case it will react 10 seconds late (see sleep time at the bottom)
        # Set wakeup duration to 30 seconds to have some time left for script.sh
        luna-send -n 1 palm://com.palm.power/timeout/set \
            "{'wakeup': true, 'key': 'cmb', 'uri': 'palm://com.palm.power/com/palm/power/activityStart', 'params': '{\"id\": \"com.company.script.check-1\", \"duration_ms\": 30000}', 'in': '00:15:00'}'"

        # the actual script
        script.sh
    fi

    sleep 20
done

The main loop does look every 20 seconds if the time interval since the last execution is greater than 15 minutes. If so the script will be executed if not it just sleeps for another 20 seconds. Using the palm wakeup call the system will be awake every 15 minutes for 30 seconds. This is long enough to run the check at least once and will trigger the script. So the palm wakeup system is being used to get the device awake and then the loop will trigger the actual script because the time intervall will be greater than 15 minutes.

The scheduler can be run with nohup or can be started by an upstart script. I tested it now over several days and it worked reliably. It also is energy efficient as the system will be only awake when the script is targeted to run. The timing can be adjusted for personal needs.