Thursday, December 25, 2014

shell - How can I run a script on boot?


I'm accustomed to running scripts on boot in Linux, but I'm not sure how to do this in Android. I'd like to start my SSH daemon on start, so I'll always be able to connect. How can I run an arbitrary script on Android boot? It'd be preferable to do this outside of Dalvik.



Answer



While looking around my Android filesystem, I found that it did, in fact have a /etc/init.d/ directory. After peeking around in there, I found /etc/init.d/20userinit with the following lines:


if [ -e /data/local/userinit.sh ];
then
log -p -i -t userinit "Executing /data/local/userinit.sh";

busybux chmod +x /data/local/userinit.sh;
logwrapper /system/bin/sh /data/local/userinit.sh;
setprop cm.userinit.active 1;
fi;

This being, of course, exactly what I needed, I wrote the following script on my computer then pushed it to my device:


#!/system/bin/sh 

dropbear -s -g


(pushed to device via scp userinit.sh phone:/data/local/userinit.sh, mind you :] )


Rebooted the device, then ran ps | grep "[d]ropbear", and sure enough, it's running. Coolness!


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...