Running command in Termux from shell block?

138 views
Skip to first unread message

David Nanian

unread,
Dec 26, 2020, 9:29:20 PM12/26/20
to Automate
If I've installed Termux and installed various executables there (eg awk, unzip) and want to run a shell script I've installed in the Termux Home folder from an Automate block (and pass in a parameter)...how do I do that?

  • I've turned on Termux permissions in Automate's permissions
  • I've tried running a Termux intent with a Run action, but that just seemed to open Termux.
  • I tried running a "shell" command with a path to the script, but that gave file not found (I haven't tried moving that script into download)
  • I tried searching on Termux for blocks and there don't seem to be any
I'm clearly missing something obvious, and I'm sorry to be asking...I just can't figure it out, and I'm clearly going to feel pretty dumb when someone points out what I've overlooked...

Henrik "The Developer" Lindqvist

unread,
Dec 26, 2020, 9:50:46 PM12/26/20
to Automate

David Nanian

unread,
Dec 26, 2020, 10:03:08 PM12/26/20
to Automate
Thanks!  I'll give that a try. It might be helpful to put that in the documentation...and maybe add a Termux block?

David Nanian

unread,
Dec 26, 2020, 10:06:23 PM12/26/20
to Automate
Or an option for Termux in the shell block that does this for the user (or, maybe change Termux permissions to a "default shell"? That might be even clearer. 

On Saturday, December 26, 2020 at 9:50:46 PM UTC-5 Henrik "The Developer" Lindqvist wrote:

Henrik "The Developer" Lindqvist

unread,
Dec 26, 2020, 11:15:34 PM12/26/20
to Automate
No such plans. The few who use that Termux feature will have to be satisfied that Automate at least include the permission.

David Nanian

unread,
Dec 27, 2020, 8:51:34 AM12/27/20
to Automate
Fair enough! Thank you for your consideration, though. I'll give this a try and hopefully it'll be a reasonable workaround for the unzip problem I ran into.

Have you considered a block that runs a shell script? I recognize that in general you want most operations to basically be done within the context of the block's own actions, but I was thinking that embedding the script in the block would mean the flow would be more portable, since it would contain the script. (This is something that Hazel, on the Mac, allows, and it's pretty handy.)

I don't think it'd work for me, since the underlying, non-Termux Linux doesn't include awk and unzip (and I honestly don't know if bash or sh are there without Termux - sorry, more ignorant than I want to be here). But it might be useful to others, given the existing "shell" support.

Anyway, thanks for the replies and for Automate. I've been using it "Premium" for some time and it's been extremely useful.

Ricardo Fernández Serrata

unread,
Dec 27, 2020, 3:55:25 PM12/27/20
to Automate
If you delete all newlines/linefeeds/linebreaks in a script and you make sure to use ";" (semicolon) as delimiter, you can run scripts directly. But also make sure to use it like sh -c 'script here' or sh -c "script here".

Also, Android already has zip and AWK executables. Zip since A8, and awk since A9. See this

Henrik "The Developer" Lindqvist

unread,
Dec 27, 2020, 4:25:44 PM12/27/20
to Automate
Use the Shell command block, e.g.:
sh foobar.sh

David Nanian

unread,
Dec 30, 2020, 1:40:48 PM12/30/20
to Automate
So, I'm still trying to get this stuff to work natively in Automate, and feeling kinda dumb. I was able to get it to work immediately with the Tasker Termux plugin, called by Automate (as long as I turned off the "timeout" option)...but I can't seem to do it with Automate directly. Note that I also tried just calling the script from a shell block, and that didn't work either.

So, I've followed the steps you referenced above and have turned on the ability to run commands with:

$ cd ~/.termux
$ cat termux.properties
allow-external-apps=true
$

Of course, I also have the ability turned on in Automate's permissions. I then tried to use a Start Application block, but that can't work: referencing the Termux source here


I discovered that it's definitely a service, and so I needed to start a service, So I tried that:

termux-example.jpg
 
And I get this:

-30 12:36:13.580 I 172@7: File exists?
12-30 12:36:13.584 I 172@14: Service start
12-30 12:36:13.585 F 172@14: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.termux.RUN_COMMAND cmp=com.termux/.app.RunCommandService (has extras) }: app is in background uid UidRecord{40bce6e u0a398 CEM bg:+1h19m51s559ms idle change:idle procs:1 seq(0,0,0)}
12-30 12:36:13.585 I 172@14: Stopped by failure

I did it with and without RUN_COMMAND_BACKGROUND, and restarted the phone, too.

I checked to see if I need to let Termux draw over things: that didn't seem to help.

Again, I feel like I'm missing something quite simple, but I don't know what it is. Since it's all working with the Tasker plugin in this same environment, I know it's possible (since they must be doing it this same way), and don't begrudge them the $2, I'm basically now just trying to figure out what I did wrong so I don't have to bug you folks the next time through.

Note, too: I was able to get it to work natively in Termux by using inotifywait and calling the same script, which was nice--always amazes me what can be accomplished in Android, with a little work; even if the user experience isn't quite up to iOS standards, the utility of this stuff far surpasses anything that I can do on iOS without actually writing an entire application (and even then).

It's also rather nice that the same basic bash scripts work on Android/Termux, macOS and Windows WSL, with small modifications for how you have to launch apps on each (I should work on detecting the environment and literally sharing the entire script).

Henrik "The Developer" Lindqvist

unread,
Dec 30, 2020, 10:58:00 PM12/30/20
to Automate
Looking at the Termux source code, they seem to do everything to ensure the service is started/running in the foreground. I don't know why the failure occur, both apps should be in the "foreground".
The App start block has a hidden feature. Try setting the Flags field to 0x10000000 then the block will use startForegroundService instead of startService.
I haven't exposed it, i.e. made visible UI for it, since it's generally meant for apps to use internally as only they can guarantee that the requirements are met, e.g. to start its service after getting a broadcast.

Henrik "The Developer" Lindqvist

unread,
Dec 30, 2020, 11:06:01 PM12/30/20
to Automate
Typo, i meant the Service start block of course.

Also, you Android version?
Reply all
Reply to author
Forward
0 new messages