Faerbit / android-crond

Simple Android app which implements crond using AlarmManager
MIT License
26 stars 6 forks source link

cron does not execute until phone is woken up #7

Closed Ji-eF closed 6 years ago

Ji-eF commented 7 years ago

Please consider the cron log below : `2017-05-31 03:53:24.4060 Scheduled line 0 at 2017-05-31 06:45:00.0000

2017-05-31 07:09:59.6570 Execute line 0 2017-05-31 07:10:38.0580 Executed line 0` The script (to turn off airplane) is not executes at the time it was meant to (6:45 am). Rather, it was executed at 7:09, when I explicitely opened my phone.

Is there some kind of weirdness with the way latest android versions try to minimize battery usage with their "doze" feature ? I tried to turn off "Battery optimization" for crond, but it did not change a thing.

Faerbit commented 7 years ago

I am not aware oft any documented Android shenanigans. I explicitly chose the option which should wake up the device at the specified option itself.

But you can still be right. Can you tell me your device info( Android version, Phone Model and ROM)?

Faerbit commented 7 years ago

Also can you quantify how long the the execution is at most delayed?

Ji-eF commented 7 years ago

My Android phone is the Motorola G4 (athene : http://www.gsmarena.com/motorola_moto_g4_plus-8050.php ) running the latest weekly build of LineageOS 14.1 (as of this writing 2017-05-22) , based on Android 7.1.2.

As a matter of facts, I happened to be awake this past night at 3:50am. First thing I did was to reopen crond app on my phone. Log file was OK, it told me that all was planed as it should be. So I went back to sleep. Can we consider this delay of about 3 hours (3:50am-6:45am) to be significant ?

I'll continue to try various tasks in the following hours and let you know my findings, and try to put a finger on this issue, hoping you'll be able to solve it :)

Cheers

Faerbit commented 7 years ago

I'm confused: The log file says that you scheduled it for 6.45 and it executed at 7.09, which would mean a delay of 24 minutes. This sounds like a lot to me, bit might be the best we can get with Android 7.1. My tasks start with a one minute delay, but I am still on android 6.0.

Ji-eF commented 7 years ago

OK, on top of changing individual battery optimization for crond, I just found another thing on my Android Device to change (systemwide?) performance profile for battery saving (Found on Settings > Battery > "Battery Saver and performance" on Andriod 7.) I pushed the slider there to the right (more performance). Stay tuned for more infos in a few hours.

and it executed at 7.09

The moment I turned my screen on, actually.

Ji-eF commented 7 years ago

So, it definitely have something to do with how Android handles battery's optimization. Giving more performance ("High performance mode. Useful for apps requiring low latency and highest throughput when power consumption is not a concern", it says) to apps allowed crond to execute tasks at desired time. Here is the crontab file : 33 17 * * 1-5 sh "/data/airplane.sh" 0 And here is the log :

2017-05-31 17:33:00.9250 Execute line 0
2017-05-31 17:41:36.2310 Executed line 0
2017-05-31 17:41:36.3130 Scheduled line 0 at 2017-06-01 17:33:00.0000

Well at least, even if it took forever to execute... the script was executed at the right time :) If I let the screen on, the script executes in mere seconds, but that is another story :D Thanks for your concern anyways, I'll try to find another alternative to go to airplane

Cheers !

Faerbit commented 7 years ago

Actually I did find one thing, which could lead to more timely execution. Would you be willing to test the following build for me? https://ufile.io/bcsz9 It's signed with my personal signing keys, so you have to uninstall the F-Droid APK first.

Ji-eF commented 7 years ago

With the new APK, things are better indeed, as this log tells

2017-06-01 07:21:15.9950 Crontab change detected. Scheduling new crontab:
2017-06-01 07:21:16.0460 Scheduled line 0 at 2017-06-01 07:24:00.0000
2017-06-01 07:24:01.3300 Execute line 0
2017-06-01 07:24:55.8950 Executed line 0
--
2017-06-01 09:43:58.0390 Crontab change detected. Scheduling new crontab:
2017-06-01 09:43:58.0890 Scheduled line 0 at 2017-06-01 10:00:00.0000
2017-06-01 10:00:15.1560 Execute line 0
2017-06-01 10:45:18.9520 Executed line 0
--
2017-06-01 10:48:32.2410 Crontab change detected. Scheduling new crontab:
2017-06-01 10:48:32.3010 Scheduled line 0 at 2017-06-01 12:45:00.0000
2017-06-01 10:48:32.4450 Scheduled line 3 at 2017-06-01 13:25:00.0000
2017-06-01 12:45:02.5170 Execute line 0
2017-06-01 12:53:45.9090 Executed line 0
2017-06-01 13:25:01.0490 Execute line 3
2017-06-01 13:26:45.2520 Executed line 3

Thanks for this new version. But alas, Android puts the device that much in slumbers that it does not execute the script until the screen is on.

Faerbit commented 7 years ago

If you're still interested: Here is another build, which includes a wake lock: https://ufile.io/ac8zk

Ji-eF commented 7 years ago

Wow, things are better indeed !!

2017-06-09 12:47:01.8100 Scheduled line 12 at 2017-06-09 12:49:00.0000
2017-06-09 12:49:02.4460 Execute line 12
2017-06-09 12:49:11.5190 Executed line 12
2017-06-09 12:49:11.6110 Scheduled line 12 at 2017-06-10 12:49:00.0000

This above, with my screen off. I'll test more accurately over this week end and I'll come back to you with my findings :D Stay tuned !

bdantas commented 7 years ago

I'm also having this problem. I'm on LineageOS 14.1 on a Nexus 5. Running crond v1.2.12

I have a cronjob for middle of the night. The job doesn't run because screen is off and device is "sleeping". My ugly workaround is to set a silent alarm using the clock app for a minute before the job.

It would be nice for crond to wake the device when a job is due to run.

Ji-eF commented 7 years ago

Yup, I did not commented more after my last post, because even if crond register the job as being executed, the script is still not executed. But the bug is no longer in crond at this point.

"Fortunately", LineageOS re-enabled a missing feature from CyanogenMod : the ability to choose a system profile when an alarm is triggered.

bdantas commented 7 years ago

So, does that feature help with this issue? If so, would you kindly give me a hint as to how to solve this? I'd love to retire my ugly workaround.

Ji-eF commented 7 years ago

Well, for my needs, it helps me well : Turn on Wifi at wake-up time :

The same applies to "Turn Airplane On" at night-time : Create a "System Profile" to turn on Airplane mode, then use it on a silent alarm at 11pm for example

Those were my needs. If you need something more advanced, well, dunno what to tell :/

bdantas commented 7 years ago

I figured it out. Crond is innocent--it runs the commands in /data/crontab when it's supposed to, screen on or off.

The problem is Termux. If a script is launched in Termux when screen is off, the terminal just stupidly sits there while screen is off. Termux only begins executing the script when screen turns on.

Therefore, the solution is for crond to turn on the screen just a few seconds before launching a script in Termux. This /data/crontab works for me:

00 04 * * * PATH=/system/bin su -c 'input keyevent 25'
00 04 * * * /system/bin/sleep 5; /system/bin/am startservice -n com.termux/com.termux.app.TermuxService -a com.termux.service_execute -d /data/data/com.termux/files/home/.shortcuts/myscript.sh