How To Use Crontab In Android?
在安卓(android) 上打开 计划任务 的功能,就是 crontab.
首先,你得 root , 如果没有,那没戏了。
Requirements
Root access: for superuser commads like reboot, or init.d config. Crond can still run under normal user privs.
Busybox: for ‘crond’ service
(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’.
1
echo '53 * * * * reboot' >> /data/crontab/root
Test without rebooting
Now open any terminal emulator in device and run the following commands..
1
2
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:
1
2
3
echo 'crond -b -c /data/crontab' > /system/etc/init.d/crond
chmod +x /system/etc/init.d/crond
Example cronjobs
1
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
2
3
2. If the crond is not obeying your timezone, you might need to update the tzdata in your device.
3. Better to test with `*/1 * * * *` to see if it is working.
因为我没在 /system/etc
目录下找到 init.d
, 因此我是这么着的:
1
2
3
echo 'crond -b -c /data/crontab' > /data/adb/service.d/crond
chmod +x /data/adb/service.d/crond
本来吧,我是准备把 crontab
目录放到 SD 卡上的,试了一下,启动的时候识别不到,因此放弃了。
所以,如果你有什么需要在启动的时候运行的话,可以把脚本放在 /data/adb/service.d
或是 /data/adb/post-fs-data.d
目录里面就可以了。
具体可以参考: Magisk: Developer Guides
还有一个详细介绍 Crond 的: How To Add Jobs To cron Under Linux or UNIX
用 termux
实现应该也是可以的,具体没有试,可以参考: How do I Crontab on Termux..