Create and manage services in Termux (Linux-based Android Terminal)
Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required.
Today, I will show you how to create and manage services in termux.
Termux uses runit
as its init system.
“runit is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes.”
First, To use Termux services, you need to install the service package.
pkg install termux-services
then restart termux so that the service daemon is started.
Now let's create a service,
I am going to create a service for gogs(a git service).
All termux services are located at $PREFIX/var/service
($PREFIX=$HOME/../usr).
create a folder for the service and name as the application name. In my case that is gogs
.
mkdir $PREFIX/var/service/gogs
then link the termux-logger.
mkdir $PREFIX/var/service/gogs/logln -sf $PREFIX/share/termux-services/svlogger $PREFIX/service/gogs/log/run
Now write the service file for the application with the below content,
touch $PREFIX/var/service/gogs/run
You can get help to write runit service here.
make it executable,
chmod +x $PREFIX/var/service/gogs/run
Now restart Termux and run,
sv up gogs
It will work smoothly.
Here are some useful commands about termux services,
To enable service to start at boot,
sv-enable <service>
To disable from starting at boot,
sv-disable <service>
To start a service,
sv up <service>
To stop a service,
sv down <service>
Bonus Topic: Manage proot(s) in Termux,
You can install multiple Linux based distributions using proot-distro
,
Lists supported distributions and their installation status,
proot-distro list
To install a distribution,
proot-distro install <distro name>
To login into a distribution,
proot-distro login <distro name>
Delete a distribution from Termux,
proot-distro remove <distro name>
To reinstall a specified distribution,
proot-distro reset <distro name>
To backup a distribution,
proot-distro backup <distro name>
To restore a distribution from backup,
proot-distro restore <distro name>