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:
- Sending a command from Tasker to Termux.
- Receiving the task’s results (stdout, stderr, exit code) after execution.
- Storing task results in a local variable for further use.
- Cleaning up completed tasks to maintain efficiency.
Profiles
Profile: T - Receive
Trigger:
-
Event:
Command
with the identifiertermux_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:
- Reading
stdout
,stderr
, andexit
files created by Termux. - Storing these results in a JSON object (
%tasks
). - Cleaning up the
task ID
from the JSON object after processing.
Steps:
- JavaScriptlet (A1):
- Reads Termux result files (
stdout
,stderr
,exit
) and stores them in%tasks
under the providedtid
. - Updates the
%tasks
variable with the latest data.
-
Wait (A2):
- Introduces a short delay to avoid processing conflicts.
-
JavaScriptlet (A3):
- Removes the
tid
from%tasks
after processing to free memory.
- Removes the
Task: T - .Run Background
Prepares and sends a command to Termux for execution.
Steps:
- Setup File Paths:
Defines paths for the handler script, stdout
, stderr
, and exit
.
- 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:
- Sending a command to Termux (
echo Hello, World
by default). - Waiting for the result.
- Returning the task output (
stdout
,stderr
,exit
).
Steps:
- Execute Command (A1):
Calls the T - .Run Background
task to send the Termux command and store its task ID in %uid
.
- 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
-
Execution Flow:
T - .Run
is triggered, executing the desired Termux command.- Results (
stdout
,stderr
,exit
) are stored in%tasks
.
-
Receiving Results:
- The
T - Receive
profile captures the event (termux_receive
). - Tasker reads the Termux output files and processes them into
%tasks
.
- The
-
Cleanup:
- Completed tasks are removed from
%tasks
to avoid clutter.
- Completed tasks are removed from
Key Variables
%tasks
: JSON object storing task results (stdout
,stderr
,exit
) for each task ID.%uid
: Unique identifier for the Termux task.
Usage Example
- Run the task
T - .Run
. - Pass your desired Termux command (e.g.,
apt update
) as%par1
. - Results will be stored in
%tasks
under the corresponding task ID.
Customization
- Modify
%par1
inT - .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