termux-api-package/scripts/termux-job-scheduler
2019-08-11 22:40:48 +02:00

90 lines
3.7 KiB
Bash
Executable File

#!/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 script to run at specified intervals."
echo " -p/--pending list pending jobs and exit"
echo " --cancel-all cancel all pending jobs and exit"
echo " --cancel cancel given job-id and exit"
echo "Options for scheduling:"
echo " -s/--script path path to the script to be called"
echo " --job-id int job id (will overwrite any previous job with the same id)"
echo " --period-ms int schedule job approximately every period-ms milliseconds (default 0 means once)."
echo " Note that since Android N, the minimum period is 900.000ms (15 minutes)."
echo " --network text run only when this type of network available (default any): any|unmetered|cellular|not_roaming|none"
echo " --battery-not-low boolean run only when battery is not low, default true (at least Android O)"
echo " --storage-not-low boolean run only when storage is not low, default false (at least Android 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_CANCEL=""
OPT_CANCEL_ALL=""
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,\
cancel,cancel-all,\
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=1; shift;;
--cancel) OPT_CANCEL=1; shift;;
--cancel-all) OPT_CANCEL_ALL=1; shift;;
--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_CANCEL" ]; then set -- "$@" --ez cancel "$OPT_CANCEL"; fi
if [ -n "$OPT_CANCEL_ALL" ]; then set -- "$@" --ez cancel_all "$OPT_CANCEL_ALL"; 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 "$@"