如何在启动时运行脚本? -- boot 领域 和 shell 领域 和 scripts 领域 android 相关 的问题

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.




在查看我的android文件系统时,我发现它确实有一个 /etc/init.d/ 目录。在那里偷看之后,我找到了 /etc/init.d/20userinit ,下行:

  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;   


  #!/system/bin/sh   dropbear -s -g    

(通过 scp userinit.sh phone:/data/local/userinit.sh ,mind为您:])

重新启动设备,然后ran ps | grep "[d]ropbear" ,并熟悉它正在运行。凉爽!


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!


/data/init.sh 在引导时运行,如果您有root,您可以根据您的喜好编辑它。小心;)

编辑:显然您也需要将已编辑的脚本蒙塞还是靴子映像。关于如何在此处执行以下操作: http://forum.xda-developers.com/showthread.php? t = 443994


/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


查看 /etc/ 目录。通常它被放置在 /system/ 分区中,您可以安装为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"   





现在您的任务查找可执行文件或 /etc/init.d/20userinit2 文件,您修改以实现目标:






例如,我找到了korn shell使用的code> /etc/init.d/20userinit5 。我更新此文件以扩展 /etc/init.d/20userinit6 env var,现在我每次做 /etc/init.d/20userinit7 我有我的路径中的busybox symlinks!

另请参见 stract方式(如果您没有找到finding magic 文件): https://stackoverflow.com/questions/9768103/make-persistent-changeS-to-init-rc


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


我尝试了所有这些方法,都没有为我工作。 然而,这是基于Lord-Ralf-Adolf的答案的原因 如何在cm12中启动启动脚本。 1?

基本上,找到文件 /etc/init.d/20userinit8 并在开头添加以下行 /etc/init.d/20userinit9


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

完成! 您现在可以在 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; 1 中放入您想要的任何内容,它将在启动时运行。 如果文件 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; 2 不在系统中,则此答案将为您提供。不要打扰创造它。


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.


如果您有 magisk 安装,您可以将.sh放入:

/sbin/magisk/img/.core/service.d /

/sbin/magisk/img/.core/post-fs-data.d /

别忘了使它可执行: chmod +x your-script.sh

更多信息: https:// github。 com / topjohnwu / magisk / blob / master / docs / guides.md#引导脚本


If you have magisk installed you can place the .sh to:


or to


Don't forget to make it executable: chmod +x your-script.sh.

More info: https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md#boot-scripts


当SELinux不是 ABCDEFGHIJKLMNABCDEFGHIJKLMN1 时,在Android 5之前简单您可以将代码放在任何脚本中,或用脚本替换二进制文件,该脚本是在引导上使用root权限执行的。另一种方法是定义自定义 init 服务,专门从某个目录中批量执行脚本。

基于这些方法,自定义ROM开发人员引入不同的 init.d 现象如 abcdefghijklmnabcdefghijklmn /etc/install-recovery.sh ,<代码> ABCDEFGHIJKLMNABCDEFGHIJKLMN6 , ABCDEFGHIJKLMNABCDEFGHIJKLMN7 ABCDEFGHIJKLMNABCDEFGHIJKLMN8 ABCDEFGHIJKLMNABCDEFGHIJKLMN9 ,<代码> ABCDEFGHIJKLMNABCDEFGHIJKLMN10 等。 但是,使用UID enforcing1 但是在受限制的selinux上下文中的进程非常无助。在 enforcing2 文件中启动了服务,其中包含 enforcing3 上下文甚至无法执行shell脚本来自 enforcing4 ,所以需要修补selinux策略以注入不受限制的上下文magisk 定义 enforcing5 。之后,可以直接运行脚本作为 enforcing6 服务或从 enforcing7 -like目录。



Things were simple before Android 5 when SELinux wasn't enforcing. You could put your code in any script or replace a binary with script which was executed with root privileges on boot. Another method was to define a custom init service specifically to batch execute scripts from some directory.

Based on these approaches custom ROM developers introduced different pseudo-init.d phenomenon like /etc/init.d/, /etc/install-recovery.sh, /etc/init.qcom.post_boot.sh, /system/bin/debuggerd, /data/init.sh, /data/local/userinit.sh, /data/local/init.d/ etc.

However a process running with UID 0 but in a restricted SELinux context is quite helpless. A service started in init.rc file with u:r:init:s0 context can't even execute a shell script from /system/bin/, so SELinux policy needs to be patched to inject an unrestricted context e.g. Magisk defines u:r:magisk:s0. After that it's possible to run a script directly as init service or from init.d-like directory.

For details see How to run an executable on boot and keep it running?



  1. 在脚本中准备好发布命令,例如/ system / xbin / post-boot(set exec perm)

  2. 在/system/etc/init.qcom.post_boot.sh

  3. 的末尾添加上面的自定义脚本路径


回声/系统/ xbin /后启动&gt;&gt; /system/etc/init.qcom.post_boot.sh


(如果找不到qcom post_boot(Qualcomm设备),请查找任何Post_boot脚本)


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)



0  如何在rooted android设备上运行脚本?  ( How to run a script on a rooted android device ) 
我们最近部署了很多内部的Android设备,仅用于显示自定义网页。 设备:RKM MK902 操作系统:Android 4.4.2 rooted WiFi半径8021x 海豚浏览器,带有售货亭模式和标签重新加载加载(刷新页面/ 30秒) 在网页中也有代码刷新本身 尝试在计划时间窗口中刷新浏览器的其他应用程...

9  在Android上我可以编写脚本(类似于Win.bat文件)来组合几个命令行命令吗?  ( On android can i write scripts similar to win bat files to combine several co ) 
在Windows上,我可以使用.bat文件运行两个命令。 如何在Android 上实现同样的事情 例如: 通过调用pcastclient / updateall 更新我的播客 我的图片Sugarsync / forcupdate的力同步 ...

3  在vold.decrypt = trigger_shutdown_framework通过init.rc运行自己的.sh脚本(android 5.0)  ( Running your own sh script at vold decrypt trigger shutdown framework via init ) 
我想在Android 5.0手机(中文,MTK,没有适当的源)中解决Android 5.0手机的供应商错误 问题是由于某些二进制中的错误,/数据未正确释放 vold.decrypt=trigger_shutdown_framework 和 vold.decrypt=trigger_reset_main ...

2  如何脚本我的手机?  ( How do i script my phone ) 
我最想要做的是,很快更改设置,添加报警等。 也许是别的什么? 这是来自Windows PC。 语言并不重要,但我知道Python和UNIX脚本。 通常如何完成? ...

0  二进制文件在哪里?  ( Where are binaries installed ) 
我最近扎根了我的手机,所以我可以安装sl4a。我想通过Python脚本执行一些命令,具体转向所有呼叫的Google语音,拨打电话,然后重新打开。我只是猜测我需要访问任何二进制文件,但似乎没有a / bin /文件夹,所以我的应用程序在哪里? (P.S.从我的移动浏览器搜索不起作用,所以如果这是一份重复的话,请将我链接...

3  如何在启动时运行可执行文件并将其保持运行?  ( How to run an executable on boot and keep it running ) 
我有一个来自 ndk-build 程序的可执行构建。我想在rooted android设备上运行它。 Android将杀死我可执行的可能性是多少? 使用 adb shell ,我能够使用以下命令执行我的可执行文件: adb push executable /sdcard adb shell device:/ $ ...

54  如何在启动时运行脚本?  ( How can i run a script on boot ) 
我习惯于在Linux的启动时运行脚本,但我不确定如何在Android中执行此操作。我想在开始时开始我的SSH守护程序,所以我将永远能够连接。如何在Android启动时运行任意脚本?在dalvik之外做这一点是优选的。 ...

1  如何使用adb的Android 4.1.2和4.2.2播放  ( How to long press in android 4 1 2 and 4 2 2 using adb ) 
我发现下面的命令不会在Android 4.1.2和4.2.2中执行,但它从Android 4.4 +执行。 input touchscreen swipe x1 y1 x2 y2 [duration] 我正在使用我的旧Android 4.1.2和4.2.2在售货亭模式中,我需要我的脚本在某些区域上长按,但我...

1  如何尽早设置CPU频率?  ( How to set cpu frequency as early as possible ) 
这个问题的意思是在引导时执行脚本的序列。 假设我有这个脚本: echo 307000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq 我希望尽快执行此行脚本(甚至在加载Android shell之前)。把它放在哪里最好的地方? ( init.d...

1  如何创建可以复制和重命名文件的Update.zip文件?  ( How to create an update zip file that can copy rename a file ) 
这是我正在寻找的 - 我需要一个可以复制文件的'update.zip'文件(示例): /system/framework/file-to-copy.apk 到另一个文件夹: / system / 然后它将在"系统" 文件夹中将"file-to-copy.apk" 重命名为: 重命名-fife.apk 并将其...

© 2022 it.wenda123.org All Rights Reserved. 问答之家 版权所有