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.


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 ];
    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;

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


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!

  • 1
    Great. Apparently CM has its own way of doing things. – Matthew Read Mar 2 '11 at 21:57
  • 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
  • 1
    @Shimmy my problem was solved by executing chmod o+x to the file. hope it helps. – Joe Nov 27 '13 at 14:35
  • 4
    What version of Android is it ? 4.3 does have /etc but no /etc/init.d. Grep does not find any. – Stéphane Gourichon Dec 7 '13 at 18:22
  • 3
    I found no /etc/init.d in 4.4.4 (Galaxy note 4) – Luis A. Florit Jan 29 '15 at 4:12

/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

  • 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 Kay Mar 2 '11 at 21:32
  • 1
    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 Kay Mar 2 '11 at 21:49
  • 3
    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
  • Do you have any idea how to add startup script when building AOSP? – Saleh Sep 22 '18 at 15:40

Look to /etc/ directory. Usually it is placed in /system/ partition which you can mount as RW:

$ ls -l /etc
lrwxrwxrwx 1 root root 11 Jan 1  2009 /etc -> /system/etc
$ su
$ mount -o remount,rw /system
$ chmod o+w /system/etc  # for "adb push"

Some above steps may be replaces with:

$ adb root
$ adb remount

and later remount RO:

$ chmod o-w /system/etc
$ mount -o remount,ro /system

Now your task to find executable or *rc file which you modify to achieve your goal:

$ find /etc -type f -perm +110
$ find /etc -name "*rc"
$ find /etc -name "init*"
$ grep -R /data /etc
$ grep -R /system /etc

Google about each candidate to get know how this file was used.

Good candidate for including custom scripts are lines from:

$ grep service /init*.rc

As each device unique you may need to do own guess about search criteria...

For example I found /etc/mkshrc which used by Korn shell. I update this file to extend PATH env var and now each time I do adb shell I have Busybox symlinks in my PATH!

See also hard way (if you have no luck with finding magic file): https://stackoverflow.com/questions/9768103/make-persistent-changes-to-init-rc

  • But isn't it getting overriden every boot ? I think the /etc folder is part of cpio which is ramdisk. – ransh Dec 18 '16 at 17:09
  • 1
    @ransh this is not the case at least in: 8.1.0_r60 where /system is system.img, and /etc is a symlink into /system. – Ciro Santilli 新疆改造中心996ICU六四事件 Jan 31 at 10:48

I tried all these methods and none of them worked for me. What worked however was based on lord-ralf-adolf's answer here How to run a script on boot in CM12.1?

basically, find the file /system/etc/install-recovery.sh and add the following line at the beginning /data/init.sh &


touch /data/init.sh
chmod 755 /data/init.sh

Done! You can now put whatever you want in /data/init.sh and it will run on startup. If the file /system/etc/install-recovery.sh is not in your system then this answer won't work for you. Don't bother creating it.

  • 1
    Actually, I have a system where /system/etc/install-recovery.sh was not present, but it is still executed on boot if present, so it's worth checking. – jcaron Aug 8 '18 at 22:25

Simple way (working):

  1. Prepare your post boot commands in a script, say /system/xbin/post-boot (set exec perm)

  2. Add the above custom script path at the end of /system/etc/init.qcom.post_boot.sh


echo /system/xbin/post-boot >> /system/etc/init.qcom.post_boot.sh


(If you can't find the qcom post_boot (Qualcomm devices), look for any post_boot scripts)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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