Skip to content
English
  • There are no suggestions because the search field is empty.

How to run your own scripts at boot (init.d)

Make your device do custom setup steps every time it starts.

Sometimes your product needs a small custom setup step every time the device starts: switch a GPIO pin, give your app access to a hardware interface, or start a helper task. Android OS built by Emteria lets you do this with your own shell scripts. You put them into the folder /data/init.d, and the system runs them automatically at boot, with full system (root) rights.

This article shows how to install your first script, how to choose when it runs, and what to check if it doesn't work.

What you need

  • A device with Android OS built by Emteria, connected to your network with an Ethernet cable.
  • A computer with ADB (Android SDK Platform-Tools) installed.
  • A text editor that can save files with Linux line endings (LF), for example Notepad++ or Visual Studio Code.

Why you need SuShell

The folder /data/init.d is protected. A normal adb shell can't open it and shows Permission denied. Our builds are typically compiled with build variant "user" for increased performance and security, so they don't support adb root. Instead, they include SuShell: type sushell in the ADB shell, and you get a root shell. Read more in How to get root access with the SuShell App.

Step 1: Connect with ADB

  1. On the device, open Emteria Settings.
  2. Turn on Enable ADB over Ethernet. The setting now shows the device's address, for example 192.168.1.50.
  3. On your computer, run:
    adb connect 192.168.1.50
  4. On the device screen, the question Allow USB debugging? appears. Select Always allow from this computer and tap Allow.

Security tip: with ADB and SuShell, every computer you allowed gets full control of the device over the network. When you're done, turn Enable ADB over Ethernet off again, especially on devices in the field.

Step 2: Write your first script

Create a file named hello-boot on your computer with this content:

#!/system/bin/sh

### BEGIN INIT INFO
# Exec: ready
### END INIT INFO

log -t hello-boot "Hello from my first init.d script"
  • The first line must be #!/system/bin/sh.
  • The block ### BEGIN INIT INFO … ### END INIT INFO tells the system when to run the script. # Exec: ready means: when the device has finished booting. See "Choose when your script runs" below.
  • Save the file with Linux line endings (LF). With Windows line endings (CRLF), the script silently does nothing.

Step 3: Install the script

  1. Copy the script to the device's shared storage:
    adb push hello-boot /sdcard/
  2. Open a shell on the device, then a root shell and check UI for confirmation dialogs:
    adb shell
    sushell
  3. Copy the script into the init.d folder and make it executable:
    cp /sdcard/hello-boot /data/init.d/
    chmod 755 /data/init.d/hello-boot
  4. Leave the root shell, delete the copy on the shared storage, and leave the ADB shell:
    exit
    rm /sdcard/hello-boot
    exit
  5. Restart the device:
    adb reboot

Step 4: Check that it worked

When the device has booted, connect again with adb connect and run:

adb logcat -d -s sysinit hello-boot

You should see a line like Executing sysinit script /data/init.d/hello-boot in phase ready, followed by your message. That's it: your script now runs at every boot.

To change the script later, repeat Step 3 with the new version. To remove it, run rm /data/init.d/hello-boot in the root shell.

Choose when your script runs

Set the line # Exec: … in the header to one of these values:

Value When it runs Good for
early Very early, before most of Android starts Rarely needed
boot During boot, a few seconds after power-on Hardware setup, file permissions (for example GPIO)
zygote When Android starts its app system Rarely needed
ready When the boot is complete Anything that needs a fully started Android

Good to know:

  • Scripts with the same value run one after another, in no fixed order. If you need a certain order, write one script that calls your other scripts in that order.
  • Each script must finish before the next one starts. Keep scripts short. For tasks that keep running, see "Scripts that keep running" below.
  • The phases can overlap a little. Don't rely on an early script having finished before a boot script starts.

Example: control a Raspberry Pi GPIO pin from your app

This example prepares GPIO 5 (pin 29 on the Raspberry Pi header) as an output and lets your app switch it. Without this script, an app can't access the pin.

  1. Find the user ID (UID) of your app. In adb shell, run the following command with your app's package name:
    pm list packages -U com.example.myapp
    The output looks like package:com.example.myapp uid:10085.
  2. Create the script, replace 10085 with your app's UID, and install it as described in Step 3:
    #!/system/bin/sh
    
    ### BEGIN INIT INFO
    # Exec: boot
    ### END INIT INFO
    
    # GPIO 5 has the number 517 on Android 16 and newer (512 + 5)
    GPIO=517
    
    echo $GPIO > /sys/class/gpio/export
    sleep 1
    echo out > /sys/class/gpio/gpio$GPIO/direction
    echo 0 > /sys/class/gpio/gpio$GPIO/value
    
    # Let your app (UID 10085) control the pin
    chown -R 10085:10085 /sys/class/gpio/gpio$GPIO/
  3. After a reboot, your app can switch the pin by writing 1 (on) or 0 (off) to /sys/class/gpio/gpio517/value.

GPIO numbers depend on the Android version: on Android 16 and newer, the GPIO numbers start at 512, so GPIO 5 is 517. On Android 13 and older, GPIO 5 is simply 5.

Scripts that keep running

If your script should keep running in the background, for example to check something every minute, add the line # Process: new to the header:

#!/system/bin/sh

### BEGIN INIT INFO
# Exec: ready
# Process: new
### END INIT INFO

while true; do
  # your recurring task here
  sleep 60
done

The script then runs alongside the other scripts and doesn't block them.

Don't start background tasks with &: background processes started inside a script (with &, nohup or setsid) are stopped as soon as the boot phase finishes. Use # Process: new instead.

Frequently asked questions

My script doesn't run. What can I check?

  • Does adb logcat -d -s sysinit show Executing sysinit script … for your script? If not, check the header: it needs exactly # Exec: boot (or another value from the table).
  • Does the file have Linux line endings (LF)? Windows line endings (CRLF) make the script fail silently. Save it again with LF and reinstall it.
  • Is the script executable? Run chmod 755 /data/init.d/YOUR-SCRIPT in the root shell. Without it, the script also fails silently.
  • Does the first line read #!/system/bin/sh?

Why do I get "Permission denied" for /data?

A normal adb shell has no access to /data/init.d. Type sushell first to get a root shell, as shown in Step 3.

Why does the data partition look empty on my computer?

The data partition uses the Linux file system ext4, which Windows can't read. Also, the folder /data/init.d is only created when the device starts for the first time. Install your scripts with ADB as described above.

There are already files in /data/init.d. Can I delete them?

No. Files like banner are part of the system. Only add, change or remove your own scripts.