Take the 2-minute tour ×
Android Enthusiasts Stack Exchange is a question and answer site for enthusiasts and power users of the Android operating system. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

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!

share|improve this answer
1  
Great. Apparently CM has its own way of doing things. –  Matthew Read Mar 2 '11 at 21:57
    
Yep, weird, but it works. –  Naftuli Tzvi Kay Mar 2 '11 at 22:01
1  
sorry to bump an old question, i tried adding /etc/init.d/99mount with content mount -o bind /emmc /mnt/sdcard/external_sd/ however it isn't being executed at startup –  Joe Apr 18 '12 at 18:47
    
@Joe did you find an answer? I have the same issue. –  Shimmy Nov 27 '13 at 10:04
    
@Shimmy my problem was solved by executing chmod o+x to the file. hope it helps. –  Joe Nov 27 '13 at 14:35
show 2 more comments

/data/init.sh runs at boot, if you have root you can edit it as you like. Be careful ;)

Edit: Apparently you might need to shoehorn the edited script into the boot image as well. Info on how to do that here: http://forum.xda-developers.com/showthread.php?t=443994

share|improve this answer
1  
Eee! Thanks :) I'll "be careful." –  Naftuli Tzvi Kay Mar 2 '11 at 21:21
1  
Yeah, I can't seem to find this file on my Nexus One running CM7-RC1. I'll see if a find / -name "init.sh" turns anything up. Are there any other scripts that run on boot? –  Naftuli Tzvi Kay Mar 2 '11 at 21:32
    
You should have an /etc/init.rc which starts the shell. It should call init.sh but if it doesn't you can just make it call your own script. –  Matthew Read Mar 2 '11 at 21:40
    
Unfortunately, I don't have that script either, but I did find a solution. –  Naftuli Tzvi Kay Mar 2 '11 at 21:49
1  
What version of Android is it ? 4.3 does have /data but no /data/init/.sh or /etc/init.rc. Grep does not find any interesting instance of the string init in /etc (even recursive). –  Stéphane Gourichon Dec 7 '13 at 18:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.