knowledge/technology/applications/mobile/Termux.md

12 KiB

obj android-id website repo
application com.termux https://termux.dev https://github.com/termux/termux-app

Termux

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically - additional packages are available using the APT package manager.

Intents and Hooks

Termux is able to catch several intents and execute shell scripts to act upon them:

  • Sharing single files with Termux.
    Following handle will be executed:~/bin/termux-file-editor
    • Opening in your popular editor by using a symbolic link:
      ln -s $PREFIX/bin/nvim ~/bin/termux-file-editor
    • Creating a handle by creating a file and changing the access rights:
      1. nano ~/bin/termux-file-editor)
      2. chmod +x ~/bin/termux-file-editor)
  • URL sharing available in common apps (e.g. Youtube).
    Following handle will be executed: ~/bin/termux-url-opener
    • Sharing a URL to be downloaded.
      You can handle incoming URL by editing the content.
      yoursth-dl -f 'bestvideo[ext=mp4][height<=720]+bestaudio' --restrict-filenames -o '~/storage/downloads/%(title)s-%(id)s.%(ext)s' $1

SSHD

Since Termux does not use initialization system, services are started manually from command line.
To start OpenSSH server, you need to execute this command:

sshd

If you need to stop sshd, just kill it's process:

pkill sshd

SSH daemon does logging to Android system log, you can view it by running logcat -s 'sshd:*'. You can do that either from Termux or ADB.

Sharing Data

Files stored in the home directory in Termux is not accessible to other applications by default. This is a limitation of Android itself.

As a workaround, you can use termux-open available in termux-tools package to share files with read access.

$ termux-open -h
Usage: termux-open [options] path-or-url
Open a file or URL in an external app.
  --send               if the file should be shared for sending
  --view               if the file should be shared for viewing (default)
  --chooser            if an app chooser should always be shown
  --content-type type  specify the content type to use
$ termux-open hello.c

Termux:Boot

This addon will run scripts immediately after device was booted.

Installation

Download add-on from F-Droid

Usage

  1. Install the Termux:Boot app.
  2. Go to Android settings and turn off battery optimizations for Termux and Termux:Boot applications.
  3. Start the Termux:Boot app once by clicking on its launcher icon. This allows the app to be run at boot.
  4. Create the ~/.termux/boot/ directory: Put scripts you want to execute inside the ~/.termux/boot/ directory. If there are multiple files, they will be executed in a sorted order.
  5. It is helpful to run termux-wake-lock as first thing to prevent the device from sleeping.

Example: to start an sshd server and prevent the device from sleeping at boot, create the following file at ~/.termux/boot/start-sshd:

#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
sshd

Termux:API

This addon exposes device functionality as API to command line programs in Termux.

Installation

Download the Termux:API add-on from F-Droid or the Google Play Store. It is required for the API implementations to function.

Installing termux-api package

To use Termux:API you also need to install the termux-api package.

pkg install termux-api

APIs

termux-wake-lock

Use termux-wake-lock and termux-wake-unlock respectively to prevent the system from putting termux to sleep.

termux-battery-status

This command returns battery status information as JSON.

termux-brightness

Set the display brightness. Note that this may not work if automatic brightness control is enabled.

termux-brightness [brightness]

Brightness value should be between 0 and 255 or auto.

termux-camera-info

Get information about device camera(s) in JSON format.

termux-camera-photo

Take a photo and save it to a file in JPEG format.

termux-camera-photo [-c camera-id] output-file

termux-wallpaper

Change wallpaper on your device.

Options
Option Description
-f <file> set wallpaper from file
-u <url> set wallpaper from url resource
-l set wallpaper for lockscreen

termux-volume

Change volume of specified audio stream.

termux-volume [stream] [volume]

Valid audio streams are: alarm, music, notification, ring, system, call.

Call w/o arguments to show information about each audio stream (output format is json).

termux-vibrate

Vibrate the device.

Options
Option Description
-d duration the duration to vibrate in ms (default:1000)
-f force vibration even in silent mode

termux-torch

Toggle LED Torch on device.

termux-torch [on | off]

termux-storage-get

Request a file from the system and write it to the specified file.

termux-storage-get [output-file]

termux-share

Share a specified file or text from standard input.

Options
Option Description
-a action which action to performed on the shared content: edit/send/view (default:view)
-c content-type content-type to use (default: guessed from file extension, text/plain for stdin)
-d share to the default receiver if one is selected instead of showing a chooser
-t title title to use for shared content (default: shared file name)

termux-sensor

Get information about types of sensors as well as live data.

Options
Option Description
-a, all Listen to all sensors (WARNING! may have battery impact)
-c, cleanup Perform cleanup (release sensor resources)
-l, list Show list of available sensors
-s, sensors [] Sensors to listen to (can contain just partial name)
-d, delay [ms] Delay time in milliseconds before receiving new sensor update
-n, limit [num] Number of times to read sensor(s) (default: continuous) (min: 1)

termux-fingerprint

Use fingerprint sensor on device to check for authentication. Output is JSON.

termux-media-player

Play specified file using Media Player API.

termux-media-player info # Displays current playback information
termux-media-player play # Resumes playback if paused
termux-media-player play <file> # Plays specified media file
termux-media-player pause # Pauses playback
termux-media-player stop # Quits playback

termux-microphone-record

Record using microphone on your device.

Options
Option Description
-d Start recording with defaults
-f Start recording to specific file
-l <limit> Start recording w/ specified limit (in seconds, unlimited for 0)
-e <encoder> Start recording w/ specified encoder (aac, amr_wb, amr_nb)
-b <bitrate> Start recording w/ specified bitrate (in kbps)
-r <rate> Start recording w/ specified sampling rate (in Hz)
-c <count> Start recording w/ specified channel count (1, 2, ...)
-i Get info about current recording
-q Quits recording

termux-notification

Display a system notification. Content text is specified using -c/--content or read from stdin.

Options
Option Description
--action action action to execute when pressing the notification
--button1 text text to show on the first notification button
--button1-action action action to execute on the first notification button
--button2 text text to show on the second notification button
--button2-action action action to execute on the second notification button
--button3 text text to show on the third notification button
--button3-action action action to execute on the third notification button
-c, --content content content to show in the notification. Will take precedence over stdin.
--group group notification group (notifications with the same group are shown together)
-i, --id id notification id (will overwrite any previous notification with the same id)
--image-path path absolute path to an image which will be shown in the notification
--on-delete action action to execute when the the notification is cleared
--ongoing pin the notification
--priority prio notification priority (high/low/max/min/default)
--sound play a sound with the notification
-t/--title title notification title to show

termux-notification-remove

Remove a notification previously shown with termux-notification --id.

termux-notification-remove [id]

termux-dialog

Show dialog widget for user input.

termux-dialog widget [options]
confirm - Show confirmation dialog
    [-i hint] text hint (optional)
    [-t title] set title of dialog (optional)

checkbox - Select multiple values using checkboxes
    [-v ",,,"] comma delim values to use (required)
    [-t title] set title of dialog (optional)

counter - Pick a number in specified range
    [-r min,max,start] comma delim of (3) numbers to use (optional)
    [-t title] set title of dialog (optional)

date - Pick a date
    [-t title] set title of dialog (optional)
    [-d "dd-MM-yyyy k:m:s"] SimpleDateFormat Pattern for date widget output (optional)

radio - Pick a single value from radio buttons
    [-v ",,,"] comma delim values to use (required)
    [-t title] set title of dialog (optional)

sheet - Pick a value from sliding bottom sheet
    [-v ",,,"] comma delim values to use (required)
    [-t title] set title of dialog (optional)

spinner - Pick a single value from a dropdown spinner
    [-v ",,,"] comma delim values to use (required)
    [-t title] set title of dialog (optional)

speech - Obtain speech using device microphone
    [-i hint] text hint (optional)
    [-t title] set title of dialog (optional)

text - Input text (default if no widget specified)
    [-i hint] text hint (optional)
    [-m] multiple lines instead of single (optional)*
    [-n] enter input as numbers (optional)*
    [-p] enter input as password (optional)
    [-t title] set title of dialog (optional)
       * cannot use [-m] with [-n]

time - Pick a time value
    [-t title] set title of dialog (optional)