Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use crontab in Android?

Tags:

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?

like image 465
see2851 Avatar asked May 25 '13 08:05

see2851


People also ask

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

What is crontab and how do you use it?

A cron job is a command run by the cron daemon at regularly scheduled intervals. To submit a cron job, specify the crontab command with the -e flag. The crontab command invokes an editing session that allows you to create a crontab file. You create entries for each cron job in this file.

How do I add or modify a crontab job?

Generally, the most used method to add or modify crontab jobs is the crontab -e command. Run this command in the terminal: This will open a text editor such as nano with the contents of your crontab file. Each crontab job resides on one line, and you may add, modify and remove lines as desired.

What is the use of crontab command?

Crontab is the answer. Take advantage of the crontab Linux command to automate various mundane and repetitive tasks, simplifying your life. This excellent and easy to use tool, combined with some scripting, can not only automate all sorts of tasks, but also help avoid simple human errors or forgetfulness.

How do I edit a crontab in Linux terminal?

Here are some basic terminal commands you will use to view and modify the crontab file: crontab-l --- List all crontab jobs. crontab -e --- Edit the crontab file. crontab -r --- Remove all entries from the crontab file.

What is the difference between a crontab and a cron?

Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks; cron itself is named after “chronos, ” the Greek word for time. cron is the system process which will automatically perform tasks for you according to a set schedule.


2 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.

like image 185
Seff Avatar answered Oct 03 '22 09:10

Seff


This is a complement to Seff's answer above. Couldn't place it in a comment because it's too long

/system/etc/init.d is not always guaranteed to work. In my case it did not. There are other methods mentioned in the following link in case this one didn't work for you https://android.stackexchange.com/questions/6558/how-can-i-run-a-script-on-boot/196785

Even then crond didn't run any job for me. To debug the errors I killed the running instance pkill crond and ran it like that

crond -f -d0 -c /data/crontab/ 

This make crond run in forground and print all the debugging information. When I ran it like that I got this warning

crond: crond (busybox 1.27.2-Stericson) started, log level 0 crond: ignoring file 'root' (no such user) 

So I had to create a passwd file with entry for root

 echo 'root:x:0:0:root:/data:/system/bin/sh' > /system/etc/passwd                                                                                                          

Even then, it still failed with error like

crond:  job: 0 /system/bin/ls crond: child running /bin/sh crond: can't execute '/bin/sh' for user root 

Please note that no where in my cronjob did I mentioned "/bin/sh". This seems to be hard coded in the binary.

Next I added the following lines to my init script

/system/xbin/mount -o remount,rw / /system/xbin/ln -s /system/bin/ /bin /system/xbin/mount -o remount,ro / 

and that's it. It worked fine after that

like image 36
Ramast Avatar answered Oct 03 '22 09:10

Ramast