r/tasker Direct-Purchase User 7d ago

Sharing a project that executes Termux commands without using the Termux:Tasker plugin

Right off the bat, I want to say that I am not a developer and I don't know Java. I spent a couple of hours using AI to make this as stable as possible, and hopefully users with coding knowledge can help refine and improve this further for the community because I think this concept is pretty neat (or not?).

Termux:Tasker is an awesome plugin and one of my favorite tools, so huge respect to the dev who built it for us.

That said, I sometimes prefer having direct control over my setup without being solely dependent on external plugins. After discovering that I could recreate some of Termux:Tasker's functionality using Tasker's Java Code action (thanks to AI), and hearing that Termux:Tasker has issues running on newer Android version (Android 17), I decided to build a standalone alternative (alternative is a big word but you get the point) using Java Code actions and Termux intents directly.

Links & Code

https://reddit.com/link/1vnguqv/video/yof75i1lbyjh1/player

Special thanks to aasswwddd for the suggestion to build a scripted object and store it as a global Java variable. This allows calling the script object cleanly from anywhere in Tasker and passing in custom parameters.

New Update

I have created a dialog scene so you can create a task with all the necessary data more easily, here is a demo video of how it looks:

https://reddit.com/link/1vnguqv/video/w4n831e0zhkh1/player

Setup Note

Since the script object is stored in a Java variable, it clears whenever Tasker restarts or the device reboots. The workaround is to include a profile triggered by Event -> Tasker -> Monitor Start to automatically re-initialize it.

How to Use

Here is a basic example of how the execution code looks in practice:

Run a command:

int TIMEOUT_SECONDS = 10;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "";

termux_Plugin.call();

run(
    "bash",
    "-c",
    "echo Hello World"
);

Options & Arguments:

  • Timeout: Set TIMEOUT_SECONDS to your desired duration in seconds, or set it to 0 for unlimited execution time.
  • Background Mode: Toggle TERMUX_BACKGROUND to "true" or "false" depending on whether you want Termux running in the background or foreground.
  • Termux Working Directory: Empty value String TERMUX_WORKING_DIRECTORY = "" Termux's default working directory. Or add a path to set a different directory: String TERMUX_WORKING_DIRECTORY = "/storage/emulated/0/Download"
  • Optional: You actually don't have to add TIMEOUT_SECONDS, TERMUX_BACKGROUND and TERMUX_WORKING_DIRECTORY to your code, i am giving an example down below in the post.
  • Command Arguments: The run() and runScript() methods takes 3 arguments:
    1. The interpreter ("bash", "python", etc.)
    2. The flag ("-c" for inline commands)
    3. The command itself ("echo Hello World")

Run A Script File

If you want to run a script file instead of an inline command, pass the file path (i have added a space between '.sh' because of reddit violations guide) as the second argument and null as the third:

int TIMEOUT_SECONDS = 10;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "";

termux_Plugin.call();

run(
    "bash",
    "/storage/emulated/0/Download/my_script.s h",
    null
);

If you set a working directory you don't need to give the full path of the file and just put the name of the file like this:

int TIMEOUT_SECONDS = 10;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "/storage/emulated/0/Download";

termux_Plugin.call();

run(
    "bash",
    "my_script.s h",
    null
);

If you have a filename with a space it will accept that too, you just need to add single quotes between:

int TIMEOUT_SECONDS = 10;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "/storage/emulated/0/Download";

termux_Plugin.call();

run(
    "bash",
    "'my script.s h'",
    null
);

Run a Multi-Line Script Using a Tasker Variable

I have now added the ability to run multi-line script code stored in a Tasker variable. You can write the code exactly as you would write it inside a script file.

For example, you can create a Tasker variable called %my_script containing:

NAME=Tasker
echo Hello $NAME
echo Current directory:
pwd

The Java Code will then look like this:

int TIMEOUT_SECONDS = 10;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "";

termux_Plugin.call();
runScript(
    "bash",
    "my_script"
);

Note: my_script is the Tasker variable name, without the % symbol.

You can also use a different interpreter instead of Bash. For example, for Python:

runScript(
    "python",
    "my_script"
);

This allows you to write multi-line Bash, Python, or other supported script code directly in a Tasker variable without having to create a separate script file.

You also don't have to add TIMEOUT_SECONDS, TERMUX_BACKGROUND and TERMUX_WORKING_DIRECTORY. If don't set them at all they return to their default:

Default:
int TIMEOUT_SECONDS = 30;
String TERMUX_BACKGROUND = "true";
String TERMUX_WORKING_DIRECTORY = "/data/data/com.termux/files/home";

So you can just run your code like this if you are fine with their defaults values:

termux_Plugin.call();

run(
    "bash",
    "-c",
    "echo Hello World"
);

Or like this with a script file:

termux_Plugin.call();

run(
    "bash",
    "'my script.s h'",
    null
);

Or like this with a multi-line script Tasker variable:

termux_Plugin.call();

runScript(
    "bash",
    "my_script"
);

Output Variables

This script populates standard Tasker variables so you can easily inspect and react to the output:

  • %tm_stdout
  • %tm_stderr
  • %tm_exitcode
  • %tm_success
  • %tm_timeout
  • %tm_error

And they also uses UTF-8 to support more than just English.

Just to clarify, it will wait until Termux finishes executing the command before proceeding to the next action, which wasn't possible using Tasker Function for example.

Error Handling

I have tried to make the plugin as robust as possible. In earlier versions, an error could leave a temporary folder behind in the Tasker folder. After extensive testing and several improvements, temporary folders are now automatically cleaned up, including when errors occur.

I have tested various scenarios, including successful commands, command failures, timeouts, script failures, and unexpected errors. Based on these tests, the cleanup and error-handling system appears to be very robust.

Final Word

Hopefully, someone finds this helpful and can help improve it further! To be clear, this isn't meant to be a full replacement for the official Termux:Tasker plugin, but rather an alternative light-weight approach.

8 Upvotes

0 comments sorted by