Skip to content

Instantly share code, notes, and snippets.

@davebrny
Created June 13, 2022 16:13
Show Gist options
  • Save davebrny/a3794d266fa0098a7c82ac658b3b8203 to your computer and use it in GitHub Desktop.
Save davebrny/a3794d266fa0098a7c82ac658b3b8203 to your computer and use it in GitHub Desktop.
(python, termux) run python scripts outside of the .termux/tasker folder
#!/data/data/com.termux/files/usr/bin/python
import sys
import subprocess
file = sys.argv[1]
args = ' '.join(map(str, sys.argv[2:]))
subprocess.run(['python', file, args])
@davebrny
Copy link
Author

when trying to run pythons script from the termux plugin in tasker, it only has permission to run scripts that are in the ~/.termux/tasker folder. i have my scripts in /storage/emulated/0/scripts and that folder is synced between all my devices using syncthing but i didnt want to change the location of those scripts for the sake of tasker, so this was the quick fix i came up with which is basically just a script that runs other scripts and passes on arguments if there are any

usage

download termux-relay.py and movie it to ~/.termux/tasker or download it using the following command in termux:

curl https://gist.githubusercontent.com/davebrny/a3794d266fa0098a7c82ac658b3b8203/raw/termux-relay.py \
    --output /data/data/com.termux/files/home/.termux/tasker/termux-relay.py

in the termux plugin, set termux-relay.py as the executable, and then in the arguments section add the path of whatever python script you want to run. if you have any arguments you want to pass on to that script then just add them on at the end.

"/storage/emulated/0/scripts/some_script.py" "123"

in your python script, add the following so you can retrieve any arguments that you passed to the script

import sys
args = ' '.join(map(str, sys.argv[1:]))

if you want to return something back to tasker then just use the print() command in your script

@davebrny
Copy link
Author

davebrny commented Jun 13, 2022

the termux plugin seems to return the stdout with a linefeed at the end and i cant figure out how to get rid of it so i made a task that runs the termux plugin and then trims any whitespace when anything is returned. its a bit easier to add/edit arguments since you can just put the python script path in the %par1 section and arguments in %par2

curl command to download it to your tasker folder:

curl https://gist.githubusercontent.com/davebrny/a410b08acf7a776926cd0cf4ddead77b/raw/python_termux.tsk.xml \
    --output /storage/emulated/0/Tasker/tasks/python_termux.tsk.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment