This is an automated archive made by the Lemmit Bot.

The original was posted on /r/tasker by /u/br64n on 2024-12-12 13:26:28+00:00.


This project allows interaction between Tasker and Termux to send commands and receive task results. It demonstrates how to execute Termux tasks in the background, monitor their output, and clean up after completion.


Overview

This setup enables:

  1. Sending a command from Tasker to Termux.
  2. Receiving the task’s results (stdout, stderr, exit code) after execution.
  3. Storing task results in a local variable for further use.
  4. Cleaning up completed tasks to maintain efficiency.

Profiles

Profile: T - Receive

Trigger:

  • Event: Command with the identifier termux_receive.

  • Captures the task ID (%tid) to process Termux results.

Linked Task:

T - Receive Task Result


Tasks

Task: T - Receive Task Result

Processes Termux task results by:

  1. Reading stdout, stderr, and exit files created by Termux.
  2. Storing these results in a JSON object (%tasks).
  3. Cleaning up the task ID from the JSON object after processing.

Steps:

  1. JavaScriptlet (A1):
  • Reads Termux result files (stdout, stderr, exit) and stores them in %tasks under the provided tid.
  • Updates the %tasks variable with the latest data.
  1. Wait (A2):

    • Introduces a short delay to avoid processing conflicts.
  2. JavaScriptlet (A3):

    • Removes the tid from %tasks after processing to free memory.

Task: T - .Run Background

Prepares and sends a command to Termux for execution.

Steps:

  1. Setup File Paths:

Defines paths for the handler script, stdout, stderr, and exit.

  1. Handler Script:

Creates a shell script (handler.sh) to execute the command and write results to the specified paths. 2. Run Command:

Sends the handler script to Termux using the Termux API.


Task: T - .Run

Handles the entire workflow of:

  1. Sending a command to Termux (echo Hello, World by default).
  2. Waiting for the result.
  3. Returning the task output (stdout, stderr, exit).

Steps:

  1. Execute Command (A1):

Calls the T - .Run Background task to send the Termux command and store its task ID in %uid.

  1. Wait Until (A2):

Waits for Termux to complete execution by checking if %tasks[%uid.exit] is set. 2. Return Results (A3):

Retrieves and returns the task result from %tasks.


How It Works

  1. Execution Flow:

    • T - .Run is triggered, executing the desired Termux command.
    • Results (stdout, stderr, exit) are stored in %tasks.
  2. Receiving Results:

    • The T - Receive profile captures the event (termux_receive).
    • Tasker reads the Termux output files and processes them into %tasks.
  3. Cleanup:

    • Completed tasks are removed from %tasks to avoid clutter.

Key Variables

  • %tasks: JSON object storing task results (stdout, stderr, exit) for each task ID.
  • %uid: Unique identifier for the Termux task.

Usage Example

  1. Run the task T - .Run.
  2. Pass your desired Termux command (e.g., apt update) as %par1.
  3. Results will be stored in %tasks under the corresponding task ID.

Customization

  • Modify %par1 in T - .Run to send any command to Termux.
  • Update the handler.sh script to handle specific output requirements.

Learn how to configure termux to receive commands

Import