2

I have a rooted Lenovo Vibe K5 Plus (A6020a46) running TWRP + Arrow OS. It doesn't provide an automatically scheduled reboot out of the box. None of the apps worked. Also the apps that claim to provide this functionality are suspiciously ad-filled and I'm not comfortable giving root permission to them.

So, here is what I've tried till now:

  • SMManager. Didn't work. Also, suspicious app.

  • Wrote an infinite loop shell script, but I'm afraid it'll make my system too busy:

    while true; do
      if [[ "$(date +"%H")" -eq "3" ]] ; then
        reboot
      fi
    done
    

    But I don't know how to autorun it on boot and I'm afraid it'll break my Android

  • Tried Termux (elevated) with cronie

    I'd put 0 3 * * * /system/bin/sh reboot in /data/crontab/root like mentioned in this link, but crond isn't recognised as a valid command in root shell.

Further, the init.d directory doesn't even exist, but an init directory does with a bunch of .rc files.

I've ran sudo crontab /data/crontab and then crontab -l lists the proper crontab, but it doesn't show up anywhere else. I'm not sure it will work. Further, I'll have to always manually run this (as far as I understand it) and seriously manually rebooting is infinitely easier.

I'm out of ideas. Any suggestions appreciated. I'd prefer shell scripts over shady apps.

Andrew T.
  • 15,988
  • 10
  • 74
  • 123
  • 2
    I have a feeling that this might be an XY-problem since this is quite an uncommon request: why do you need to restart the device every 3 AM? (That said, googling a6020a446 doesn't return anything). – Andrew T. Jun 20 '21 at 09:44
  • sorry it was dumb of me to not notice the spelling error. It is a6020a46. Also, I just want to restart my device everyday at 3 am with possibly a shell script. I have shared what I've tried and the outcomes but my original problem still remains, I want to restart my device automatically at 3 AM. This is to have optimal performance and avoid network issues which for some reason appear when my phone has been running for abt 2+ days. – linux_enthusiast Jun 20 '21 at 10:14
  • 4
    [tag:automation] is an option. Using MacroDroid, create a macro with trigger of day /time and action - reboot. If you are OK with apps. You can test it by adding empty trigger as another trigger and testing the macro from 3 dots menu – beeshyams Jun 20 '21 at 10:20
  • 1
    Why not just write a normal alarm clock app that will run system("su -c reboot"); (or maybe even call the Android API function for this, if it exists) when the alarm is triggered? Alarm clocks made correctly don't usually suffer from energy saving modes. – Ruslan Jun 20 '21 at 19:09
  • @beeshyams, thank you that app seems good enough and is what I'll probably use for now since I am not technically advanced enough (yet) to implement the other solutions. Nonetheless it is an app, one that perhaps isn't gpl and hence I'm still suspicious of it.especially since it now has root access. – linux_enthusiast Jun 21 '21 at 02:37
  • @Ruslan, I am unfortunately not skilled enough in Android Dev to make an alarm app from scratch. Right now I am only a consumer and a tinkerer with Android devices but not yet a dev. Forget "made accurately", my skills ensure that (for now) I can't even make one. Further I've lots of other work (unfortunately) and I'd rather use a script than write an app from scratch. However I shall look into it. Thanks. – linux_enthusiast Jun 21 '21 at 02:40
  • There are some opensource alarm clocks that you could modify to your needs. – Ruslan Jun 21 '21 at 07:51
  • @Ruslan could you give me some examples which would be easy to modify for an absolute beginner with ZERO prior experience with Android programming?? – linux_enthusiast Jun 21 '21 at 18:35
  • ok I have pretty much fallen in love with the Macro Droid app. It is fantastic and does exactly what I want. Who knows, maybe it will make me too lazy to actually solve this problem on the cli – linux_enthusiast Jun 21 '21 at 18:37
  • @beeshyams looks like your answer using MacroDroid helped the OP. Would you like to post it as a proper answer? – Andrew T. Sep 19 '22 at 06:31
  • @AndrewT sorry for delay in reverting// I think it's better if the OP does that (being lazy). Thanks for tracking comments and checking! – beeshyams Oct 11 '22 at 10:09

1 Answers1

3

Run the following shell script from init.d (or /data/adb/*.d/ in case of Magisk). Or run directly from init's .rc file. See How to run an executable on boot and keep it running?

#!/system/bin/sh -e

ignore signals

for i in $(seq 64); do trap '' "$i"; done

seconds until today 3AM

SECS=$(( $(date -d '03:00' +%s) - $(date +%s) ))

if today's 3AM has passed, set it to tomorrow's

[ SECS -gt 0 ] || SECS=$((SECS + 86400))

wait until 3AM

echo "${0##*/}: sleeping for $SECS seconds" sleep $SECS

Reboot device

echo "${0##*/}: restarting" /system/bin/reboot scheduled-reboot || /system/bin/setprop sys.powerctl reboot,scheduled-reboot

Irfan Latif
  • 20,353
  • 3
  • 70
  • 213
  • doubt it will work, guesswork sleep is stretched in idle mode (power saving) – alecxs Jun 20 '21 at 15:47
  • This answer is interesting, one that I think I'll eventually use. However, I haven't understood completely and shall use it only when I do so. Also could you clarify what @alecxs has said in his comment above? – linux_enthusiast Jun 21 '21 at 02:49
  • 2
    @alecxs that's correct. But the process can be added to foreground cgroup so that it's not affected by idle mode. That's how services work. Or sleep for smaller intervals like 1min and check intermittently if 3AM has passed. Else the only way to wake up the device from idle mode to perform a task at exactly 3AM is to use AlarmManager API from an app. It's not possible with a shell script. – Irfan Latif Jun 21 '21 at 06:23