termux / termux-boot

Termux add-on app allowing programs to be run at boot.
https://f-droid.org/en/packages/com.termux.boot
933 stars 219 forks source link

[Feature Request] Allow Termux:Boot to run a Termux "session" instead of an invisible "task" #44

Open NightfallAlicorn opened 4 years ago

NightfallAlicorn commented 4 years ago

When Termux:Boot runs a script and an unknown error occurs. There's no way for the user to see what the issue is on the log. From my experience outputting the command log to a file isn't reliable such as node ~/path/to/file/index.js >> log.txt.

The script runs fine when I run it in Termux but the auto script with the exact same command says it's running but it really isn't.

Grimler91 commented 4 years ago

You probably want to do &> /data/data/com.termux/files/home/log.txt. &> captures the error stream as well and you probably want to use an absolute path so that you know where the log ends up

NightfallAlicorn commented 4 years ago

The only concern I have with this method is that it will reduce the write cycles of my android's internal memory. Say if a script goes out of control with the log writes without the user being aware. But this should help users keep some track of what's going on.

BlueMax commented 4 years ago

I use it as well in combination with set -x for more verbose output:

#!/data/data/com.termux/files/usr/bin/bash
set -x
exec &> /mnt/ramdisk/log.txt
...

Size-limited ramdisk:

mount -t tmpfs -o size=100M tmpfs /mnt/ramdisk

I actually spawn multiple size-limited ramdisks at early-boot to overlay several /data/data/*/{tmp,cache} folders (browser cache etc) to save write cycles. Plus a large one for temporary stuff (downloads etc).

erkkkkkkkkk commented 3 years ago

Task#44

pwqw commented 2 years ago
mount -t tmpfs -o size=100M tmpfs /mnt/ramdisk

I actually spawn multiple size-limited ramdisks at early-boot to overlay several /data/data/*/{tmp,cache} folders (browser cache etc) to save write cycles. Plus a large one for temporary stuff (downloads etc).

Interesting approach.

But what if the phone is not rooted?

ShaKabosh commented 11 months ago

Here's a bit of a fix I found for this: Uncomment the line

# allow-external-apps = true

in ~/.termux/termux.properties, so it looks like this:

allow-external-apps = true

Now, create a file in ~/.termux/boot/ for your shell script containing the below text:

am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \
-a com.termux.RUN_COMMAND \
--es com.termux.RUN_COMMAND_PATH '<CMD_PATH>' \
--esa com.termux.RUN_COMMAND_ARGUMENTS '<ARGS>' \
--es com.termux.RUN_COMMAND_WORKDIR '<WORKING_DIR>' \
--ez com.termux.RUN_COMMAND_BACKGROUND 'false' \
--es com.termux.RUN_COMMAND_SESSION_ACTION '0'

Substitute <CMD_PATH> for an absolute path to the binary, <ARGS> for a comma-separated list of arguments, and <WORKING_DIR> for the working directory. When this script runs, it calls an intent to open a session. It is important that RUN_COMMAND_SESSION_ACTION is set to '0' which opens the session in the app when the intent is sent, as otherwise (in my experience, at least), Termux will not run the command until the session is switched to.