Manage auto-startup services in Termux.
Here I share two Termux extensions. The first is Termux-services, which manages services after the Termux app is started. The second is Termux:Boot, which configures Termux services to start automatically after Android boots.
Why would you need Termux-services?
If you want to start and stop services in Termux, and configure services to start automatically after opening the app, but Termux has no Systemd, are you stuck writing auto-start services into ~/.bashrc or ~/.profile, then killing processes with the kill command when you want to stop them?
At this point, Termux-services is a pretty good tool.
Termux-services lets you use a set of commands to control service execution, so you can freely start and stop services. It is very useful for programs that need daemons, such as sshd, apache2, and bitcoin, which can be started automatically through Termux-services after the Termux app opens.
Termux-services uses “runit” to manage system services. This is a very simple init service manager, now used by only a small number of Linux distributions. Speaking of Linux distributions that use runit, the most famous are probably Artix Linux and Void Linux.
First I will introduce how to use Termux-service, then explain how to use Termux: Boot.
1. Termux-services auto-start#
- Install the Termux-services package
pkg install termux-servicesWhen installing some packages, Termux will also install runit service files. The files for all services are located at
$PREFIX/var/service/sv.For example, after installing the
opensshpackage, Termux-services will automatically install the SSHD service file.$PREFIX/var/service/sv/sshd/rundefines the command executed after the service starts, and$PREFIX/var/service/sv/sshd/downdefines the command executed after the service stops.To start and stop the SSHD service, use the following commands:
# Start
sv up sshd
# Stop
sv down sshd
# Auto-start after opening the app
sv-enable sshd
# Disable auto-start after opening the app
sv-disable sshd- By analogy, if you want to execute other programs after Termux starts, create a service file in the same way. Refer to the official runit templates when writing scripts. I will use running a custom script as an example:
# Edit content
echo "echo 'Hello world' ">> ~/run.sh
chmod +x ~/run.sh
# Add service file
vim $PREFIX/var/service/sv/myscript/run
# Fill in:
bash ~/run.sh
# Configure auto-start after starting the Termux app
sv-enable myscript2. Termux:Boot auto-start on boot#
Termux:Boot automatically executes the scripts you write after the Android system boots.
Actually, you do not have to use it together with Termux-service. Termux:Boot can call Termux to execute arbitrary commands, but I think system services are better managed centrally. So the architecture becomes: first add the services you want to run through Termux-services, then use Termux:Boot to automatically start Termux, and all the services you wrote will start with it.
Install the Termux: Boot APK
Tap the icon once to launch it, so it will start automatically after boot
Create a directory in Termux and add a boot script
mkdir -p ~/.termux/boot/
vim ~/.termux/boot/run.sh- Fill in the following content to configure all runit auto-start services to run automatically after boot
#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
. $PREFIX/etc/profile
