<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Runit on Ivon's Blog</title><link>https://ivonblog.com/en-us/tags/runit/</link><description>Recent content in Runit on Ivon's Blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>infoivonblog.nkfjt@aleeas.com (Ivon Huang)</managingEditor><webMaster>infoivonblog.nkfjt@aleeas.com (Ivon Huang)</webMaster><copyright>You are welcome to share articles of Ivon's Blog (ivonblog.com). Please include the original URL when citing articles, and abide by CC BY-NC-ND 4.0 license. For commercial use, please write an e-mail to me.</copyright><lastBuildDate>Mon, 18 Nov 2024 06:00:00 +0800</lastBuildDate><atom:link href="https://ivonblog.com/en-us/tags/runit/index.xml" rel="self" type="application/rss+xml"/><item><title>Manage auto-starting services: how to use Termux-services &amp; Termux:Boot</title><link>https://ivonblog.com/en-us/posts/termux-services/</link><pubDate>Mon, 18 Nov 2024 06:00:00 +0800</pubDate><author>infoivonblog.nkfjt@aleeas.com (Ivon Huang)</author><guid>https://ivonblog.com/en-us/posts/termux-services/</guid><description>&lt;!-- Co-translated by ChatGPT --&gt;
&lt;p&gt;Manage auto-startup services in Termux.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Why would you need Termux-services?&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.profile&lt;/code&gt;, then killing processes with the &lt;code&gt;kill&lt;/code&gt; command when you want to stop them?&lt;/p&gt;
&lt;p&gt;At this point, Termux-services is a pretty good tool.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;sshd&lt;/code&gt;, &lt;code&gt;apache2&lt;/code&gt;, and &lt;code&gt;bitcoin&lt;/code&gt;, which can be started automatically through Termux-services after the Termux app opens.&lt;/p&gt;
&lt;p&gt;Termux-services uses &amp;ldquo;runit&amp;rdquo; 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.&lt;/p&gt;
&lt;p&gt;First I will introduce how to use Termux-service, then explain how to use Termux: Boot.&lt;/p&gt;

&lt;h2 class="relative group"&gt;1. Termux-services auto-start
 &lt;div id="1-termux-services-auto-start" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#1-termux-services-auto-start" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Install the Termux-services package&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pkg install termux-services&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="2"&gt;
&lt;li&gt;
&lt;p&gt;When installing some packages, Termux will also install runit service files. The files for all services are located at &lt;code&gt;$PREFIX/var/service/sv&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For example, after installing the &lt;code&gt;openssh&lt;/code&gt; package, Termux-services will automatically install the SSHD service file. &lt;code&gt;$PREFIX/var/service/sv/sshd/run&lt;/code&gt; defines the command executed after the service starts, and &lt;code&gt;$PREFIX/var/service/sv/sshd/down&lt;/code&gt; defines the command executed after the service stops.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To start and stop the SSHD service, use the following commands:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Start&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sv up sshd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Stop&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sv down sshd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Auto-start after opening the app &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sv-enable sshd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Disable auto-start after opening the app &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sv-disable sshd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="5"&gt;
&lt;li&gt;By analogy, if you want to execute other programs after Termux starts, create a service file in the same way. Refer to the &lt;a href="https://smarden.org/runit/runscripts" target="_blank" rel="noreferrer"&gt;official runit templates&lt;/a&gt; when writing scripts. I will use running a custom script as an example:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Edit content&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;echo &amp;#39;Hello world&amp;#39; &amp;#34;&lt;/span&gt;&amp;gt;&amp;gt; ~/run.sh
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;chmod +x ~/run.sh
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Add service file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;vim &lt;span class="nv"&gt;$PREFIX&lt;/span&gt;/var/service/sv/myscript/run
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Fill in:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;bash ~/run.sh
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Configure auto-start after starting the Termux app&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sv-enable myscript&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 class="relative group"&gt;2. Termux:Boot auto-start on boot
 &lt;div id="2-termuxboot-auto-start-on-boot" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#2-termuxboot-auto-start-on-boot" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;Termux:Boot automatically executes the scripts you write after the Android system boots.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install the Termux: Boot APK&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Tap the icon once to launch it, so it will start automatically after boot&lt;/p&gt;</description></item><item><title>Solutions when Termux proot cannot use the systemctl command</title><link>https://ivonblog.com/en-us/posts/termux-systemd-issues/</link><pubDate>Sat, 31 Aug 2024 23:00:00 +0800</pubDate><author>infoivonblog.nkfjt@aleeas.com (Ivon Huang)</author><guid>https://ivonblog.com/en-us/posts/termux-systemd-issues/</guid><description>&lt;!-- Co-translated by ChatGPT --&gt;
&lt;p&gt;Systemd is an init program. Almost all Linux distributions use Systemd to manage system services, and common commands include &lt;code&gt;systemctl&lt;/code&gt;, &lt;code&gt;journalctl&lt;/code&gt;, and so on.&lt;/p&gt;
&lt;p&gt;However, Termux cannot use Systemd. If you run commands related to &lt;code&gt;systemctl start&lt;/code&gt; inside a Termux proot-distro or chroot container environment, you will see this error: &lt;code&gt;System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This is because the container environment does not support Systemd.&lt;/p&gt;
&lt;p&gt;There is actually no real solution, unless the Termux development team learns from Microsoft WSL and finds a way to make Systemd run. However, we do have some alternatives to address this problem.&lt;/p&gt;

&lt;h2 class="relative group"&gt;Method 1: Start the program directly from the executable path
 &lt;div id="method-1-start-the-program-directly-from-the-executable-path" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#method-1-start-the-program-directly-from-the-executable-path" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;First, understand that Systemd is just an init program for managing system services. It is responsible for starting programs after Linux boots and letting users turn programs on and off. But simply starting a program does not actually require &lt;code&gt;systemctl&lt;/code&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;For example, to start the SSH service in proot Debian, the Systemd way is to run the following command:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sudo systemctl start sshd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="2"&gt;
&lt;li&gt;If we find Systemd&amp;rsquo;s service file &lt;code&gt;/etc/systemd/system/sshd.service&lt;/code&gt; (most Systemd service files are here), we will see that what follows &lt;code&gt;ExecStart=&lt;/code&gt; is the command it really executes:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;[&lt;/span&gt;Service&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;EnvironmentFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;-/etc/default/ssh
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;ExecStartPre&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/sbin/sshd -t
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/sbin/sshd -D&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="3"&gt;
&lt;li&gt;Therefore, the SSH service can be started directly with this command and run in the background:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;mkdir -p /run/sshd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/usr/sbin/sshd -D&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note: programs inside Termux proot-distro cannot use ports below 1000. When logging in to proot-distro, add the &lt;code&gt;--fix-low-ports&lt;/code&gt; parameter to redirect SSH port 22 to 2022.&lt;/p&gt;
&lt;p&gt;If you want to automatically start a specific program after logging in to proot-distro, the simplest way is to add the command you want to run to &lt;code&gt;~/.bashrc&lt;/code&gt; in the user&amp;rsquo;s home directory.&lt;/p&gt;

&lt;h2 class="relative group"&gt;Method 2: Switch to a distribution that uses OpenRC
 &lt;div id="method-2-switch-to-a-distribution-that-uses-openrc" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#method-2-switch-to-a-distribution-that-uses-openrc" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;OpenRC and runit are more traditional init programs. They are not as complex as Systemd, and they can run and manage services in proot. Users unfamiliar with OpenRC commands can refer to the Systemd and OpenRC command comparison table below.&lt;/p&gt;
&lt;p&gt;At present, the proot-distro distribution that uses OpenRC is Alpine Linux, while Artix Linux and Void Linux use runit.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install Alpine Linux&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;proot-distro install alpine
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;proot-distro login alpine&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="2"&gt;
&lt;li&gt;Install the OpenRC package&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;apk add openrc&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="3"&gt;
&lt;li&gt;After that, append this command when logging in to proot, and the OpenRC program will start&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;proot-distro login alpine --fix-low-ports -- /bin/ash -c &lt;span class="s2"&gt;&amp;#34;/sbin/openrc default; /bin/ash -l&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="4"&gt;
&lt;li&gt;After that, you can use OpenRC to manage services. For example, SSH can be managed with the &lt;code&gt;rc-service&lt;/code&gt; command, without typing the executable path&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sudo rc-service sshd start&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol start="5"&gt;
&lt;li&gt;You can also configure the SSH service to start automatically after logging in to proot-distro.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;sudo rc-update add sshd&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 class="relative group"&gt;Method 3: Run a full Linux system virtual machine
 &lt;div id="method-3-run-a-full-linux-system-virtual-machine" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#method-3-run-a-full-linux-system-virtual-machine" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;The ultimate solution is to emulate a full Linux system so that Systemd can run.&lt;/p&gt;</description></item></channel></rss>