3

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.

yoarch
  • 31
  • 2

1 Answers1

4

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?

Irfan Latif
  • 20,353
  • 3
  • 70
  • 213
  • 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 ?

    – yoarch Dec 20 '20 at 12:17
  • How to make code blocks in comments ? – yoarch Dec 20 '20 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 ? – yoarch Dec 22 '20 at 18:19
  • @yoarch the usual troubleshooting steps. Set proper mode, owner, context on files. Apply dos2unix if needed. Start with something simple like exec - -- /bin/echo Hello. Then replace - with u:r:magisk:s0 or some other super context. Or set SELinux permissive. Check dmesg for init log. – Irfan Latif Dec 22 '20 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. – yoarch Dec 23 '20 at 11:18
  • Any one ? I can not get that script be run while shutting down my android phone. This is the only part I can not get working .. – yoarch Jan 02 '21 at 12:41