Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

shell - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...