From d171f23b5f77c9a27eb450b4e4522f62df494063 Mon Sep 17 00:00:00 2001 From: Ben Moran Date: Thu, 3 Jan 2019 10:05:15 +0000 Subject: [PATCH] termux-job-scheduler --- scripts/termux-job-scheduler | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 scripts/termux-job-scheduler diff --git a/scripts/termux-job-scheduler b/scripts/termux-job-scheduler new file mode 100755 index 0000000..764ef7d --- /dev/null +++ b/scripts/termux-job-scheduler @@ -0,0 +1,77 @@ +#!/data/data/com.termux/files/usr/bin/bash +set -e -u -f + +SCRIPTNAME=termux-job-scheduler + +show_usage () { + echo "Usage: termux-job-scheduler [options]" + echo "Schedule a Termux script to run later, or periodically" + echo " --script path to the Termux script to be called" + echo " --job-id int job id (will overwrite any previous job with the same id)" + echo " --pending boolean list pending jobs only (default false)" + echo " --period_ms int schedule job approximately every period_ms milliseconds (default 0 means once)" + echo " --network run only when this type of network available, default none (any|unmetered|cellular|not_roaming|none)" + echo " --battery-not-low boolean run only when battery is not low, default true (at least Androi O)" + echo " --storage-not-low boolean run only when storage is not low, default false (at least Androi O)" + echo " --charging boolean run only when charging, default false" + echo " --trigger-content-uri text (at least Android N)" + echo " --trigger-content-flag int default 1, (at least Android N)" + exit 0 +} + +OPT_SCRIPT="" +OPT_JOB_ID="" +OPT_PENDING="" +OPT_PERIOD_MS="" +OPT_NETWORK="" +OPT_BATTERY_NOT_LOW="" +OPT_STORAGE_NOT_LOW="" +OPT_CHARGING="" +OPT_TRIGGER_CONTENT_URI="" +OPT_TRIGGER_CONTENT_FLAG="" + +TEMP=`busybox getopt \ + -n $SCRIPTNAME \ + -o hs:p \ + --long script:,\ +job-id:,pending:,\ +period-ms:,network:,\ +battery-not-low:,storage-not-low:,\ +charging:,help,\ +trigger_content_flag:,trigger-content-uri: \ + -s bash \ + -- "$@"` +eval set -- "$TEMP" + +while true; do + case "$1" in + -s | --script) OPT_SCRIPT="$2"; shift 2;; + --job-id) OPT_JOB_ID="$2"; shift 2;; + -p | --pending) OPT_PENDING="$2"; shift 2;; + --period-ms) OPT_PERIOD_MS="$2"; shift 2;; + --network) OPT_NETWORK="$2"; shift 2;; + --battery-not-low) OPT_BATTERY_NOT_LOW="$2"; shift 2;; + --storage-not-low) OPT_STORAGE_NOT_LOW="$2"; shift 2;; + --charging) OPT_CHARGING="$2"; shift 2;; + --trigger-content-flag) OPT_TRIGGER_CONTENT_FLAG="$2"; shift 2;; + --trigger-content-uri) OPT_TRIGGER_CONTENT_URI="$2"; shift 2;; + -h | --help) show_usage;; + --) shift; break ;; + esac +done + +if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi + +set -- +if [ -n "$OPT_SCRIPT" ]; then set -- "$@" --es script "$OPT_SCRIPT"; fi +if [ -n "$OPT_JOB_ID" ]; then set -- "$@" --ei job_id "$OPT_JOB_ID"; fi +if [ -n "$OPT_PENDING" ]; then set -- "$@" --ez pending "$OPT_PENDING"; fi +if [ -n "$OPT_PERIOD_MS" ]; then set -- "$@" --ei period_ms "$OPT_PERIOD_MS"; fi +if [ -n "$OPT_NETWORK" ]; then set -- "$@" --es network "$OPT_NETWORK"; fi +if [ -n "$OPT_BATTERY_NOT_LOW" ]; then set -- "$@" --ez battery_not_low "$OPT_BATTERY_NOT_LOW"; fi +if [ -n "$OPT_STORAGE_NOT_LOW" ]; then set -- "$@" --ez storage_not_low "$OPT_STORAGE_NOT_LOW"; fi +if [ -n "$OPT_CHARGING" ]; then set -- "$@" --ez charging "$OPT_CHARGING"; fi +if [ -n "$OPT_TRIGGER_CONTENT_URI" ]; then set -- "$@" --es trigger_content_uri "$OPT_TRIGGER_CONTENT_URI"; fi +if [ -n "$OPT_TRIGGER_CONTENT_FLAG" ]; then set -- "$@" --ez trigger_content_flag "$OPT_TRIGGER_CONTENT_FLAG"; fi + +/data/data/com.termux/files/usr/libexec/termux-api JobScheduler "$@"