I would like to execute a script at logout with my GS7 android 9 (Resurrection Remix). I see that it is advised to use tasker. I guess it is possible to do that without installing a graphical application. I connect to the smartphone through adb and have a shell. Where can I write a file in the file system with bash or python commands that will be executed at logout ? Also, how should I manage the bash or python environment in order to have it working ? Thanks for your time.
1 Answer
On a rooted device you can make use of init
's on shutdown
trigger:
# /etc/init/custom.rc
on shutdown
exec - -- /path/to/executable
For more details on how to run an executable from init
's .rc
file, and what factors need to be taken care of, see my answer to How to run an executable on boot and keep it running?
-
I tried the solution and was able to do things. I created a
shutdown.sh
script, make it executable and put it under/bin/
``` #!/bin/bash /data/data/com.termux/files/usr/bin/mosquitto_pub MOSQUITTO_COMMAND ``` And it works from adb shell. Then I created the/etc/init/custom.rc
file with ``` on shutdown exec - -- /system/bin/shutdown.sh ``` And when I shut it down that doesn't work. I think the files permissions are good. However, I am confused with the android file system. Should I make everything under/system
or not ?– yoarchDec 20, 2020 at 12:17 -
-
I tried different things about custom.rc. Can not get it doing anything at shutdown. I don't find anything online. Any suggestion ?– yoarchDec 22, 2020 at 18:19
-
@yoarch the usual troubleshooting steps. Set proper mode, owner, context on files. Apply
dos2unix
if needed. Start with something simple likeexec - -- /bin/echo Hello
. Then replace-
withu:r:magisk:s0
or some other super context. Or set SELinux permissive. Checkdmesg
forinit
log. Dec 22, 2020 at 18:49 -
Thanks for the answer. Could you give me a concrete example with /bin/echo Hello please ? As the partition seems to be read only, I can not do something like exec - -- /bin/echo Hello > /some/file. I checked the dmesg logs with dmesg | grep init. But I don't see anything related to the custom.rc script. The path should be "/bin/echo Hello" or "/system/bin/echo Hello" ? I guess I need magisk to be installed in order to use the u:r:magisk:s0 context ? That is my case.– yoarchDec 23, 2020 at 11:18