这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@benmoran
Copy link
Contributor

@benmoran benmoran commented Jan 3, 2019

Wrapper script as requested in termux/termux-api#235 (comment)

@aussarapong

This comment has been minimized.

@theARE
Copy link

theARE commented Jan 10, 2019

So this uses android's native scheduler rather than relying on a crond running in termux?

What's the syntax for using this?

If, for example I wanted ~/foo.sh executed hourly, how would that be set up?

@benmoran
Copy link
Contributor Author

@theARE This should do it:
termux-job-scheduler --script $HOME/foo.sh --period_ms 3600000

Unlike cron, it won't guarantee to run exactly hourly. But it won't keep your phone awake, and it will let you specify other conditions like "only when the phone is charging and the network is available".

Specify that this job should recur with the provided interval, not more than once per period. You have no control over when within this interval this job will be executed, only the guarantee that it will be executed at most once within this interval."

See Android docs.

@theARE
Copy link

theARE commented Jan 10, 2019

Cheers, I will have a play
Presumably I can pair this with Termux:boot to reinitialize the job after reboot

Just discovered termux yesterday & loving it

Been so frustrated installing propriety tasks to perform simple jobs when there's a perfectly good linux subsystem just sitting there

Why did it take me so long to find this? Doh!

@theARE
Copy link

theARE commented Jan 10, 2019

Ah, shame
This isn't yet available in the F-droid version yet which seems to be a bit behind

Hopefully it'll shoe up in the next release

@fornwall fornwall merged commit a26a6a1 into termux:master Jan 10, 2019
@fornwall
Copy link
Member

Nice work! This is now available in version 0.37 of the termux-api package, which can be updated to with pkg up.

Note that this requires version 0.29 of the Termux:API app, which should be available on Google Play in a couple of hours and on F-Droid within a day or so.

@theARE
Copy link

theARE commented Jan 11, 2019

Cheers, upgrade pulled it in & I'm running it now

One small correction, the example I was given, along with the "Usage" section of the script states that the interval param is --period_ms
That isn't recognized, but --period-ms is and works

Just a typo in the usage section I'm sure, but thought it worth reporting, just in case it trips anyone else up

Will report back with any other feedback

@shadow443
Copy link

Hi, how can I make the job stop?

@benmoran
Copy link
Contributor Author

@juanspeed good question! The jobs aren't persisted so rebooting should do it (after removing any Termux::boot scripts you might have set up).

You can also submit a new job with same job_id to change the timeout or the script, without rebooting.

I should make a new PR to fix the doc bugs noted by @theARE, and expose the cancel method too.

1 similar comment
@benmoran
Copy link
Contributor Author

@juanspeed good question! The jobs aren't persisted so rebooting should do it (after removing any Termux::boot scripts you might have set up).

You can also submit a new job with same job_id to change the timeout or the script, without rebooting.

I should make a new PR to fix the doc bugs noted by @theARE, and expose the cancel method too.

@shadow443
Copy link

@benmoran I did a new job to overwrite the job_id. I did not know about the restart, I'll try.

Apparently there are ways to cancel the jobs it would be nice to be added in future updates :)

@n0ego
Copy link

n0ego commented Feb 21, 2019

I'm able to run this
sh storage/downloads/my-script.sh

But when doing
termux-job-scheduler --script ~/storage/downloads/my-script.sh --period-ms 10000
There is an error: Cannot execute file: /data/data/com.termux/files/home/storage/downloads/my-script.sh

Any idea how to fix this?

Many thanks.

@Grimler91
Copy link
Member

@n0ego file in /sdcard cant be executed. Either move it to somewhere in $HOME or make a wrapper script that runs sh /sdcard/Downloads/my-script.sh

@n0ego
Copy link

n0ego commented Feb 22, 2019

It works now. Thank @Grimler91.

Is there any way to list jobs and a script file associated with each job? Also can we specify starting time for a job?

@Dmole
Copy link

Dmole commented Jun 18, 2020

termux-job-scheduler --help
Usage: termux-job-scheduler [options]
Schedule a script to run at specified intervals.
  -p/--pending               list pending jobs and exit
  --cancel-all               cancel all pending jobs and exit
  --cancel                   cancel given job-id and exit
Options for scheduling:
  -s/--script path           path to the script to be called
  --job-id int               job id (will overwrite any previous job with the same id)
  --period-ms int            schedule job approximately every period-ms milliseconds (default 0 means once).
                             Note that since Android N, the minimum period is 900.000ms (15 minutes).
  --network text             run only when this type of network available (default any): any|unmetered|cellular|not_roaming|none
  --battery-not-low boolean  run only when battery is not low, default true (at least Android O)
  --storage-not-low boolean  run only when storage is not low, default false (at least Android O)
  --charging boolean         run only when charging, default false
  --persisted boolean        should the job survive reboots, default false
  --trigger-content-uri text (at least Android N)
  --trigger-content-flag int default 1, (at least Android N)

termux-job-scheduler -p
Pending Job 0: /data/data/com.termux/files/home/test.sh (periodic: 900000ms) (battery not low) (network: NetworkRequest [ NONE id=0, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Uid: 10148] ])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants