Last active 1765743276

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

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