termux / termux-api-package

Termux package containing scripts to call functionality in Termux:API.
MIT License
1.02k stars 318 forks source link

termux-job-scheduler not running jobs #78

Closed heywoodlh closed 5 years ago

heywoodlh commented 5 years ago

Trying to run a script with the job-scheduler and it doesn't seem to run every 15 minutes (despite it showing up in termux-job-scheduler --pending).

Aside from installing the termux-api package, is there something I need to do to ensure the job is runs?

Here is my command to schedule the job:

$ termux-job-scheduler --period-ms 900000 --script /data/data/com.termux/files/home/scripts/check-shell.sh --network any

Scheduling Job 0: /data/data/com.termux/files/home/scripts/check-shell.sh       (periodic: 900000ms) (battery not low) (network: NetworkRequest [ NONE id=0, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Unwanted:  Uid: 10121] ]) - response 1

Here is the pending job:

$ termux-job-scheduler --pending

Pending Job 0: /data/data/com.termux/files/home/scripts/check-shell.sh  (periodic: 900000ms) (battery not low) (network: NetworkRequest [ NONE id=0, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED Unwanted:  Uid: 10121] ])

Again, is there some other step I am missing to ensure that my job runs?

xalexalex commented 5 years ago

What's inside ~/scripts/check-shell.sh ? Also, since termux-api v0.34 you don't need to explicitly state --network any because it is the default.

heywoodlh commented 5 years ago

It's a script to check if an SSH process is running and if it isn't it runs another script to re-establish the tunnel (this isn't my most elegant script):

$ cat scripts/check-shell.sh
#!/usr/bin/env bash

ps aux | grep ssh | grep skynet | grep -q boomerang

STATUS="$?"

DATE="$(date +%m-%d_%H:%M)"

echo "reverse shell last checked: $DATE" > ~/log/shell-check.txt
if [[ "$STATUS" != 0 ]]
then
        echo "reverse shell not running, restarting."
        pkill sshd; sshd -E ~/log/ssh.log
        ~/.termux/boot/reverse-ssh.sh
else
        echo "reverse shell is running, exiting"
fi

As a hack-ey control, you can see that I have it echo the time that the script is run to ~/log/shell-check.txt and it never updates unless I run the script manually.

I did have a thought, maybe Termux's job scheduler handles STDOUT differently so that the echo command isn't ever redirected to the file? I would guess not because the SSH connection never gets re-established, but just a guess.

xalexalex commented 5 years ago

Run termux-fix-shebang ~/scripts/check-shell.sh and retry. It should work (just tested and it worked for me).

heywoodlh commented 5 years ago

That seems to have fixed it. Thanks!