Last active 1765743276

A script that install some service management utilities in a Termux environment

Revision 05374ed65400626564864c60b42e6912c61c016d

termux-services-setup.sh Raw
1#!/data/data/com.termux/files/usr/bin/sh
2
3# This script installs some service management utilities in a Termux environment
4# NOTE: It requires termux-services to be already installed:
5# pkg update && pkg install termux-services
6
7TERMUX_HOME="$HOME/.termux"
8BIN_PATH="$PREFIX/bin"
9
10# Create directory for user scripts
11mkdir -p "$TERMUX_HOME/scripts"
12
13cat <<EOF > "$TERMUX_HOME/scripts/sv-log"
14#!/data/data/com.termux/files/usr/bin/bash
15
16# sv-log script: A utility journalctl-like script to view and follow logs from
17# Termux services
18
19usage() {
20 echo "Usage: \$0 [-h] [-f] [-n <lines>] <service>" >&2
21 exit 1
22}
23
24lines=25
25
26while getopts "fhn:" opt; do
27 case "\${opt}" in
28 f)
29 follow=1
30 ;;
31
32 n)
33 lines=\${OPTARG}
34 (echo -n "\$lines" | grep -E '^[0-9]+$' >/dev/null) || usage
35 ;;
36
37 *)
38 usage
39 ;;
40 esac
41done
42
43shift \$((OPTIND-1))
44service="\$1"
45[ -n "\$service" ] || usage
46
47logfile="\$PREFIX/var/log/sv/\$service/current"
48[ -f "\$logfile" ] || (
49 echo "No logs found for service \$service" >&2
50 exit 0
51)
52
53OPTS="-n -\${lines}"
54[ -n "\$follow" ] && OPTS="-f \${OPTS}"
55tail \$OPTS "\$logfile"
56EOF
57
58cat <<EOF > "$TERMUX_HOME/scripts/install-termux-services"
59#!/data/data/com.termux/files/usr/bin/sh
60
61# A script that supports the installation of custom Termux services
62# by symlinking service directories from \$TERMUX_HOME/var/service
63# to \$PREFIX/var/service and setting up log directories
64
65TERMUX_HOME="$TERMUX_HOME"
66
67mkdir -p "\$HOME/.local/bin"
68mkdir -p "\$PREFIX/var/service"
69ln -sf "\$TERMUX_HOME/scripts/sv-log" "\$HOME/.local/bin"
70
71find "\$TERMUX_HOME/var/service" -maxdepth 1 -type d | tail -n -1 | while read srv; do
72 ln -sf "\$srv" "\$PREFIX/var/service"
73 mkdir -p "\$PREFIX/var/log/sv/\$srv"
74 ln -sf "\$PREFIX/var/log/sv/\$srv" "\$srv/log"
75done
76EOF
77
78# Copy sv-log and install-termux-services to bin path
79install -m 700 "$TERMUX_HOME/scripts/sv-log" "$BIN_PATH/sv-log"
80install -m 700 "$TERMUX_HOME/scripts/install-termux-services" "$BIN_PATH/install-termux-services"