1

I have a script I need to run once, and only once the phone is booted.

I have attempted init.qcom.post_boot.sh, init.sec.boot.sh, debuggerd, and finally mkshrc

While my script ran with debuggerd, I found that it ran every time debuggerd ran, which is not something I need to happen... thus when I stumbled upon /system/etc/mkshrc (the other 2 simply did not work)

What I found was, the script runs that one time, on boot... however, it also seems to run every time adb shell runs, along with (I assume) everytime shell is fired up... which is also undersireable.

So, what can I do in /system/etc/mkshrc to ensure my call to my script runs only the one time... at boot?

I thought about running a while loop to check for the getprop sys.boot_completed if it == 1 then dump a file somewhere, and through an if statement inside the looop to check for that file, if exists then exit, if not then run the next command... however, everywhere I am allowed to dump that file, it persists between boots, so that next command never runs again.

NOTE Does not need root to do this, nor does my script need root to run (yes, I have confirmed)

CODE

BB=/system/xbin/busybox
MYPID=/mnt/sdcard/.kev-run

while [ `getprop init.svc.bootanim` == "stopped" ] ; do 
    # if the run file does not exist, and we are stopped
    if [ ! -e $MYPID ]; then
        # run the scripts, then create the run file
        LOG=/mnt/sdcard/Download/kevs-scripts.log
        $BB rm -f $LOG;
        $BB echo "Launching Kevs Scripts" >> $LOG;
        MY_SCRIPT
        # write the run file so we aren't running this everytime this file runs
        touch $MYPID;
        sleep 1;
    fi
done

# if we are still booting, delete the run file
while [ `getprop init.svc.bootanim` != "stopped" ] ; do
    # we're not done booting, remove our makeshift pid file and sleep a second
    $BB rm -f $MYPID;
    sleep 1;
done

Seems to be off to me

1

mkshrc is by definition run every time an interactive shell is started, so it’s the wrong place.

You really should hook up your script with Android’s init system instead. (Sorry, I can’t give details on how to do that; I don’t know the Android ecosystem well, I’m just mksh’s developer.)

Writing to a file in tmpfs (so it’s auto-removed on poweroff) is usually a good approach for a script to run only once. However, I’d be surprised if Android’s init system wouldn’t provide such a facility; it’s the ideal tool for your job.

  • yeah, noticed that. I'm hooking into debuggerd again, and utilizing setprop to set a non-persistent "flag" now – Kevin Mar 1 '18 at 16:02
  • i wish I could hook into init. or even compile a kernel for it (which would be much much better), but alas... it's a Verizon, with a locked down bootloader so I'm limited LOL – Kevin Mar 1 '18 at 16:03
  • Well, there’s this but the accepted answer does not work on all phones as /system/etc/init.d/ support is a CM(IIRC?) addition. Patching init.rc seems to be the generally correct solution. In general, service …\n\tdisabled\n\toneshot seems to work; don’t forget to chmod +x it and copy to the correct partition beforehand. – mirabilos Mar 1 '18 at 20:14
  • I wish I could. on a G935V, and it's bootloader locked, so root is the only thing we really have. I ended up utilizing debuggerd as my "firing" mechanism, setting a "flag" via setprop but what I'm finding now, is once root is removed, setprop no longer works. My goal is to get some tweaks to work without having to root. I have confirmed that some do indeed work, while some dont (which is expected), but the trick is getting them to fire off on boot... once :) – Kevin Mar 2 '18 at 13:02
  • I forgot to mention, I can root/unroot my device at will. Rooted, everything works just fine. Unrooted, debuggerd throws a permission denied, so I think what I need to do is figure out the proper ownership, sepolicy, and context for it in order to get the permissions right... almost there LOL – Kevin Mar 2 '18 at 13:48

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.