|
| 1 | +#!@TERMUX_PREFIX@/bin/bash |
| 2 | +set -e -u -f |
| 3 | + |
| 4 | +SCRIPTNAME=termux-cron |
| 5 | + |
| 6 | +show_usage () { |
| 7 | + echo "Usage: $SCRIPTNAME [options]" |
| 8 | + echo "Cron-like job scheduler." |
| 9 | + echo " -l/--list list cron jobs and exit" |
| 10 | + echo " -i/--info id print details about job" |
| 11 | + echo " -r/--reschedule reschedule all alarms" |
| 12 | + echo " --delete-all delete all cron jobs and exit" |
| 13 | + echo " --delete id delete cron job by id and exit" |
| 14 | + echo "Options for adding a job:" |
| 15 | + echo " -c/--cron expression cron expression when the job will run" |
| 16 | + echo " -s/--script path path to the script to be called" |
| 17 | + echo " --exact boolean schedule exact (false - needs permission)" |
| 18 | + echo " --network text run only when this type of network available (NOT_REQUIRED) |
| 19 | + possible values: NOT_REQUIRED|CONNECTED|UNMETERED|NOT_ROAMING|METERED" |
| 20 | + echo " --battery-not-low boolean run only when battery is not low (false)" |
| 21 | + echo " --charging boolean run only when charging (false)" |
| 22 | + echo " --idle boolean run only when idle (false)" |
| 23 | + echo " --storage-not-low boolean run only when storage is not low (false)" |
| 24 | + echo " --timeout int timeout in seconds for constraints to be fullfilled (0=disabled) (60)" |
| 25 | + echo " --grace-period int grace period in milliseconds between SIGTERM and SIGKILL if job is terminated (5000)" |
| 26 | + echo " --continue boolean continue job execution if constraints fail (false)" |
| 27 | + echo " --max-runtime int maximal runtime in seconds for job to finish (0=disabled) (3600)" |
| 28 | + exit 0 |
| 29 | +} |
| 30 | + |
| 31 | +OPT_LIST="" |
| 32 | +OPT_INFO="" |
| 33 | +OPT_RESCHEDULE="" |
| 34 | +OPT_DELETE="" |
| 35 | +OPT_DELETE_ALL="" |
| 36 | + |
| 37 | +OPT_CRON="" |
| 38 | +OPT_SCRIPT="" |
| 39 | + |
| 40 | +OPT_EXACT="" |
| 41 | +OPT_NETWORK="" |
| 42 | +OPT_BATTERY_NOT_LOW="" |
| 43 | +OPT_CHARGING="" |
| 44 | +OPT_IDLE="" |
| 45 | +OPT_STORAGE_NOT_LOW="" |
| 46 | +OPT_TIMEOUT="" |
| 47 | +OPT_GRACE_PERIOD="" |
| 48 | +OPT_CONTINUE="" |
| 49 | +OPT_RUNTIME="" |
| 50 | + |
| 51 | +TEMP=`getopt \ |
| 52 | + -n $SCRIPTNAME \ |
| 53 | + -o hlri:c:s: \ |
| 54 | + --long \ |
| 55 | +info:,delete-all,delete:,\ |
| 56 | +reschedule,cron:,script:,\ |
| 57 | +exact:,network:,\ |
| 58 | +battery-not-low:,charging:,\ |
| 59 | +idle:,storage-not-low:,\ |
| 60 | +timeout:,grace-period:,\ |
| 61 | +continue:,max-runtime: \ |
| 62 | + -s bash \ |
| 63 | + -- "$@"` |
| 64 | +eval set -- "$TEMP" |
| 65 | + |
| 66 | +while true; do |
| 67 | + case "$1" in |
| 68 | + -l | --list) OPT_LIST=1; shift;; |
| 69 | + -i | --info) OPT_INFO="$2"; shift 2;; |
| 70 | + -r | --reschedule) OPT_RESCHEDULE=1; shift;; |
| 71 | + --delete-all) OPT_DELETE_ALL=1; shift;; |
| 72 | + --delete) OPT_DELETE="$2"; shift 2;; |
| 73 | + -c | --cron) OPT_CRON="$2"; shift 2;; |
| 74 | + -s | --script) OPT_SCRIPT="$2"; shift 2;; |
| 75 | + --exact) OPT_EXACT="$2"; shift 2;; |
| 76 | + --network) OPT_NETWORK="$2"; shift 2;; |
| 77 | + --battery-not-low) OPT_BATTERY_NOT_LOW="$2"; shift 2;; |
| 78 | + --charging) OPT_CHARGING="$2"; shift 2;; |
| 79 | + --idle) OPT_IDLE="$2"; shift 2;; |
| 80 | + --storage-not-low) OPT_STORAGE_NOT_LOW="$2"; shift 2;; |
| 81 | + --timeout) OPT_TIMEOUT="$2"; shift 2;; |
| 82 | + --grace-period) OPT_GRACE_PERIOD="$2"; shift 2;; |
| 83 | + --continue) OPT_CONTINUE="$2"; shift 2;; |
| 84 | + --max-runtime) OPT_RUNTIME="$2"; shift 2;; |
| 85 | + -h | --help) show_usage;; |
| 86 | + --) shift; break ;; |
| 87 | + esac |
| 88 | +done |
| 89 | + |
| 90 | +if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi |
| 91 | + |
| 92 | +set -- |
| 93 | +if [ -n "$OPT_LIST" ]; then set -- "$@" --ez list "$OPT_LIST"; fi |
| 94 | +if [ -n "$OPT_INFO" ]; then set -- "$@" --ei info "$OPT_INFO"; fi |
| 95 | +if [ -n "$OPT_RESCHEDULE" ]; then set -- "$@" --ez reschedule "$OPT_RESCHEDULE"; fi |
| 96 | + |
| 97 | +if [ -n "$OPT_DELETE_ALL" ]; then set -- "$@" --ez delete_all "$OPT_DELETE_ALL"; fi |
| 98 | +if [ -n "$OPT_DELETE" ]; then set -- "$@" --ei delete "$OPT_DELETE"; fi |
| 99 | + |
| 100 | +if [ -n "$OPT_CRON" ]; then set -- "$@" --es cron "$OPT_CRON"; fi |
| 101 | +if [ -n "$OPT_SCRIPT" ]; then set -- "$@" --es script "$OPT_SCRIPT" --es path "$(pwd)"; fi |
| 102 | +if [ -n "$OPT_EXACT" ]; then set -- "$@" --ez exact "$OPT_EXACT"; fi |
| 103 | +if [ -n "$OPT_NETWORK" ]; then set -- "$@" --es network "$OPT_NETWORK"; fi |
| 104 | +if [ -n "$OPT_BATTERY_NOT_LOW" ]; then set -- "$@" --ez battery_not_low "$OPT_BATTERY_NOT_LOW"; fi |
| 105 | +if [ -n "$OPT_CHARGING" ]; then set -- "$@" --ez charging "$OPT_CHARGING"; fi |
| 106 | +if [ -n "$OPT_IDLE" ]; then set -- "$@" --ez idle "$OPT_IDLE"; fi |
| 107 | +if [ -n "$OPT_STORAGE_NOT_LOW" ]; then set -- "$@" --ez storage_not_low "$OPT_STORAGE_NOT_LOW"; fi |
| 108 | +if [ -n "$OPT_TIMEOUT" ]; then set -- "$@" --ei constraint_timeout "$OPT_TIMEOUT"; fi |
| 109 | +if [ -n "$OPT_GRACE_PERIOD" ]; then set -- "$@" --ei grace_period "$OPT_GRACE_PERIOD"; fi |
| 110 | +if [ -n "$OPT_CONTINUE" ]; then set -- "$@" --ez constraint_continue "$OPT_CONTINUE"; fi |
| 111 | +if [ -n "$OPT_RUNTIME" ]; then set -- "$@" --ei max_runtime "$OPT_RUNTIME"; fi |
| 112 | + |
| 113 | +@TERMUX_PREFIX@/libexec/termux-api Cron "$@" |
0 commit comments