Skip to main content

Manage auto-starting services: how to use Termux-services & Termux:Boot

·
Categories Smartphones Termux Tutorials
Tags Runit
Table of Contents

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
#

  1. Install the Termux-services package
pkg install termux-services
  1. When installing some packages, Termux will also install runit service files. The files for all services are located at $PREFIX/var/service/sv.

  2. For example, after installing the openssh package, Termux-services will automatically install the SSHD service file. $PREFIX/var/service/sv/sshd/run defines the command executed after the service starts, and $PREFIX/var/service/sv/sshd/down defines the command executed after the service stops.

  3. 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
  1. 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 myscript

2. 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.

  1. Install the Termux: Boot APK

  2. Tap the icon once to launch it, so it will start automatically after boot

  3. Create a directory in Termux and add a boot script

mkdir -p ~/.termux/boot/

vim ~/.termux/boot/run.sh
  1. 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

References
#

Related


Thank you for reading. Public comments are not available on this website. I write to explore ideas honestly, not to chase social engagement or traffic. I would be glad to hear your thoughts after reading the article with care. If you found any errors, technical issues, or would like to share feedback, feel free to contact me via the email listed on the About page.