-
Notifications
You must be signed in to change notification settings - Fork 340
Description
Feature description
There are a few programs that I'd like to always run, or as close to always as possible: e.g. ssh server, and a script I wrote to limit battery charging to 80%. termux-boot provides a way to start these. However, if a foreground app hogs RAM, they can get killed, and I know of no way to have them restarted once there's enough memory.
I see 3 solutions. The first one would be the best, as it doesn't require root access. However, even if you provide that one, also providing the other two would make things more convenient for people with root access in some situations.
Solution 1: Start (a) script(s) once there's enough memory available
Some other background apps, like KDE Connect, somehow manage to restart automatically after they get killed by Android for memory, I guess via some sort of event hooks or timers. If termux-boot could use a similar mechanism, it could then either start the boot scripts again (perhaps with an argument to indicate that this is not the first time they get launched since boot), or launch a separate "resurrection" script (or a set of scripts in a directory, which one could optionally symlink to the boot script directory).
Solution 2 (with root access): Provide instructions or a command to make Android no longer consider a process part of any app
I found that running sudo bash -c "echo $BASHPID > /sys/fs/cgroup/uid_0/cgroup.procs" in a script that backgrounds itself makes Android not kill it if I manually kill the Termux app. However, it's not enough to also save it from being killed when Android kills background processes to save memory, and I haven't found a way to achieve that.
Solution 3 (with root access): Provide instructions or a command to run commands in Termux's context from outside Termux
This solution would involve not actually using termux-boot. Magisk provides its own way to run scripts on boot (perhaps other root managers too?), and Android considers those to be system processes and not parts of any app, so it doesn't kill them for memory. (The kernel OOM killer could kill them, but the Android OOM killer, which kills apps, generally kicks in before the kernel OOM killer would; anyway, the kernel OOM killer generally wouldn't choose small background services to kill, while Android lines up small background scripts against the wall before a memory-hogging foreground app.)
However, even if one uses Magisk to start the boot scripts, one may prefer to call into Termux for various reasons: for GUI features like termux-notification, or to run programs installed via Termux packages. (I use a chroot environment for most of my background stuff, but for many people it may be more convenient to install stuff via Termux.) However, there are many reasons Termux commands don't work properly if started from outside Termux (environment variables, selinux stuff); there seems to be no official documentation on how to do this, and I haven't managed to get it to work. Except via sshing into an ssh server started from termux-boot, but of course that can get killed.
By the way, what are Termux tasks, and how does one start one? I haven't found any documentation on them. I've only seen them in the context of termux-boot: the Termux notification shows e.g. "0 sessions, 1 task" as long as any process started from a boot script is running, even if it backgrounds itself. This way, at least I have a visual indicator of whether the boot scripts are still running: the persistent notification disappears if they get killed. However, if I then manually restart them, if they background themselves, there's no notification, so I don't see if they get killed again (while if they run in the foreground, they are shown as a session, not as a task).