How to use crontab in Android?


I can't find answer to my question: Is it possible to run crontab to reboot Android using busybox (or other means)

Tried to run crontab, and it complain about unknown uid 0.

Tried to run reboot, and it does nothing.

Or I am asking for the impossible right now?


Answers:


Requirements

  1. Root access: for superuser commads like reboot, or init.d config. Crond can still run under normal user privs.

  2. Busybox: for 'crond' service

  3. (Optional) init.d support: to start 'crond' service at boot. Or start via Magisk post-fs-data.d script.

Creating cronjob

Create the cronjob file in directory /data/crontab/ (it could be any accessible directory even in sdcard) with filename 'root'. Write your cronjob inside the file 'root'.

echo ' 
53 * * * * reboot' >> /data/crontab/root

Test without rebooting

Now open any terminal emulator in device and run the following commands..

su -
crond -b -c /data/crontab

Now the crond service will start, to check type pgrep -l crond or ps | grep crond

Start crond at boot

create a file at /system/etc/init.d with executable permission:

echo '   
crond -b -c /data/crontab' > /system/etc/init.d/crond

chmod +x /system/etc/init.d/crond

Example cronjobs

53 * * * * reboot

Will reboot your device on 53rd minute of every hour.

Note: 1. If you modify crontab, remember to restart crond daemon after killing the existing one.

  1. If the crond is not obeying your timezone, you might need to update the tzdata in your device.

  2. Better to test with */1 * * * * to see if it is working.