termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.29k stars 452 forks source link

termux-api commands hang in terminal without any output #466

Open johnwuelk opened 2 years ago

johnwuelk commented 2 years ago

Problem description

termux-api commands do not work and hang in terminal without any visible output. The only thing I can do is Control+c for cancelling the command. According to forum posts, I'm not the only encountering this issue.

Steps to reproduce

Newest version of termux-api and termux is installed from F-Droid and all permissions are granted in the app settings for both apps. Plus, battery optimization is turned off for all termux apps, so that termux apps are granted to run unlimited at all times. termux-battery-status is weirdly only working sometimes, it has only worked 2 or 3 times out of the many I tried. The other commands do not work at all. Rebooting and/or reinstalling the apps did not lead to any solution.

Expected behavior

Regular output of termux-api specific commands like termux-battery-status

Additional information The phones were NOT rooted. Does the Android device really have to be rooted for termux-api to (fully) work all the time?

Grimler91 commented 2 years ago

Do you have the termux-api app installed? You mention the installed version of the termux-api package, but not of the app.

Termux-api should work without root, trying to use it as root will in fact lead to issues

johnwuelk commented 2 years ago

Yes, I have the termux-api APP from F-Droid installed as well, not only the package within termux.

It does work without root on other phones like Samsung but sadly not on my Blu phone.

Grimler91 commented 2 years ago

Do you get any errors/warnings/related output in adb logcat when trying to use it?

johnwuelk commented 2 years ago

Yes:

~ $ adb logcat

* daemon not running; starting now at tcp:5037
* daemon started successfully
- waiting for device -
agnostic-apollo commented 2 years ago

Yes:

Generate a full dump after connecting phone to pc, running api command and then running command on pc: adb shell -d > logcat.txt

https://developer.android.com/studio/command-line/logcat

You should also disable battery optimizations and duraspeed, etc.

https://dontkillmyapp.com/

johnwuelk commented 2 years ago

adb shell logcat -d > logcat.txt worked and generated this following file: logcat.txt termux-battery-status was running on phone before I ran above command.

if this is relevant the output of termux-api-start was: Starting service: Intent { cmp=com.termux.api/.KeepAliveService }

Any battery optimization was disabled and no battery app installed.

john-peterson commented 1 year ago
termux-api-start

I did not even have a clue about this command

And btw can you extend the expiration of action files? The latest and all previous have expired

sylirre commented 1 year ago

And btw can you extend the expiration of action files? The latest and all previous have expired

90 days is the max possible time for keeping GitHub Actions artifacts in public repositories.

agnostic-apollo commented 1 year ago

The latest and all previous have expired

https://github.com/termux/termux-api/actions/runs/5930893260

rocket-pig commented 1 year ago

Im in same position. Were any meaningful actions to take found? Even on another phone when *-api commands /did/ work, there was usually a 2-5 second delay, so I expected the wait...once it got over a minute I headed here to search.

Where do we go from here?

Here's strace termux-sms-list , maybe thats helpful? https://pastebin.com/hebAL3Ux

sylirre commented 1 year ago

Here's strace termux-sms-list , maybe thats helpful? https://pastebin.com/hebAL3Ux

Your log is not correct as basically traces shell execution. You need to include -f (--follow-forks) strace option to trace also termux-api server. But you won't be able to debug Termux:API application with strace.


Background:

termux-sms-list is a shell script that spawns termux-api server. The server itself sends an intent to Android OS to start Termux:API application and then creates sockets on which listens for connection from Termux:API.

If you don't have Termux:API Android application (Termux API consists of Termux:API application and termux-api package) installed, termux-sms-list will wait indefinitely. Same would happen if Termux:API was not able to start because the termux-api server will not exit until connection was received.

rocket-pig commented 1 year ago

Hi, I appreciate response - I have both installed and actually reinstalled both before posting just to double make sure. Still same result. I can try the -f hook if its helpful? (Is that what youre saying, or that it also wouldnt be?)

Edit: termux-api-start completes instantly with this output: "Starting service: Intent { cmp=com.termux.api/.KeepAliveService }"

sylirre commented 1 year ago

Try with -f, at least it should tell whether termux-api part was started properly.

rocket-pig commented 1 year ago

its 1.5MB of text, pastebin has no upload function and other sites are giving me grief as well. It ends with many many copies of the mprotect line... Im not knowlegable enough to decipher what this means or if its helpful in troubleshooting why this is happening for folks. Edit:andn amazing, the termux copy/paste and github's cr/lf /r/n 'carraiage return' apparently arent a match either. what a nightmare. github also says i can upload, then the file is greyed out in the selector. and now trying to add line ends to the texxt inserts tabs which...im not going to spend two hours trying to un-fuck this when its unclear if its even going to help smarter people troubleshoot.

[pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ) = 0
 [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ|PROT_WRITE) = 0                                                              [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ) = 0         [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ|PROT_WRITE) = 0                                                              [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ) = 0         [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ|PROT_WRITE) = 0                                                              [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ) = 0         [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ|PROT_WRITE) = 0                                                              [pid 13823] mprotect(0x79c43cd000, 4096, PROT_READ) = 0         [pid 13823] munmap(0x79c313e000, 4096)  = 0                     [pid 13823] munmap(0x79c3271000, 4096)  = 0                     [pid 13823] munmap(0x79c3274000, 4096)  = 0                     [pid 13823] munmap(0x79c43cc000, 4096)  = 0                     [pid 13823] munmap(0x79c43cd000, 4096)  = 0                     [pid 13823] exit_group(0 <unfinished ...>                       [pid 13834] <... futex resumed>)        = ? <unavailable>       [pid 13840] <... ioctl resumed>)        = ? <unavailable>       [pid 13833] <... futex resumed>)        = ?                     [pid 13832] <... futex resumed>)        = ?                     [pid 13830] <... futex resumed>)        = ? <unavailable>       [pid 13823] <... exit_group resumed>)   = ?                     [pid 13841] <... ioctl resumed> <unfinished ...>) = ?           [pid 13839] <... futex resumed>)        = ?                     [pid 13838] <... futex resumed>)        = ?                     [pid 13836] <... futex resumed>)        = ?                     [pid 13835] <... rt_sigtimedwait resumed> <unfinished ...>) = ? [pid 13837] <... futex resumed>)        = ?                     [pid 13833] +++ exited with 0 +++                               [pid 13832] +++ exited with 0 +++                               [pid 13831] <... futex resumed>)        = ?                     [pid 13841] +++ exited with 0 +++                               [pid 13840] +++ exited with 0 +++                               [pid 13839] +++ exited with 0 +++                               [pid 13838] +++ exited with 0 +++                               [pid 13837] +++ exited with 0 +++                               [pid 13836] +++ exited with 0 +++                               [pid 13835] +++ exited with 0 +++                               [pid 13834] +++ exited with 0 +++                               [pid 13831] +++ exited with 0 +++                               [pid 13830] +++ exited with 0 +++                               [pid 13823] +++ exited with 0 +++                               [pid 13820] <... accept4 resumed>0x7fd59b89f0, [110], 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
agnostic-apollo commented 1 year ago

Long hold on terminal and then Report Issue with debug enabled. Then save file from options button in report activity.

rocket-pig commented 1 year ago

Long hold on terminal and then Report Issue with debug enabled. Then save file from options button in report activity.

...heres the result of that (400K+):

https://pastebin.com/awxp0Xbh

rocket-pig commented 1 year ago

...and just to skip that possible suggestion, i see it says in there that termux-am package is updateable, so i did, and ran termux-api stop; termux-api start; termux-sms-list and...its been over a minute with no response at all still.

sylirre commented 1 year ago

@rocket-pig To catch the long strace log, use strace -f -o strace-log.txt your-command. Then upload file strace-log.txt.

mprotect lines possibly related to /system/bin/app_process. This utility will generate lots of calls.

rocket-pig commented 1 year ago

@rocket-pig To catch the long strace log, use strace -f -o strace-log.txt your-command. Then upload file strace-log.txt.

mprotect lines possibly related to /system/bin/app_process. This utility will generate lots of calls.

Tldr my post above, i did that and its multiple MBs and couldn't get it here or on any paste sites.

agnostic-apollo commented 1 year ago

Change log level to Verbose for Termux:API and then run api command and then run adb logcat -d > logcat.txt. After that, install Termux:API app from here and then start app from launcher and then try running command again.

https://github.com/termux/termux-app#debugging

rocket-pig commented 1 year ago

I appreciate the help, updating to that version fixed the problem, maybe could have led with that lol.

FWIW Chrome seems incapable of clicking the .apk links on those Action pages, but in Brave browser they always work.

Thanks again.

agnostic-apollo commented 1 year ago

Welcome. Logs would have been useful to find real issue.

Probably aren't logged into github on chrome.

machitgarha commented 1 year ago

Change log level to Verbose for Termux:API and then run api command and then run adb logcat -d > logcat.txt. After that, install Termux:API app from here and then start app from launcher and then try running command again.

https://github.com/termux/termux-app#debugging

Thank you very much! It fixed the problem for me too!

suikoy commented 10 months ago

I have incurred in the same issue, is there a fix that I could apply? In my case I use termux-api just to show some popups with termux-toast. I'm on Pixel 7 with Android 14, no root, Termux installed via F-Droid and termux-api installed with this version: termux-api/stable,now 0.57 aarch64 [installed]

viacheslav-dobrynin commented 10 months ago

@suikoy, try this command: apt update; apt install termux-api termux-am As written in the https://github.com/termux/termux-api/issues/638#issuecomment-1868383070 It helped me

suikoy commented 10 months ago

Yes it works like a charm! Many thanks Dobrynin

amirhosein-vedadi commented 5 months ago

To address the issue of termux-api commands not working and hanging in the terminal without any visible output, I suggest the following solution:

1- Uninstall the termux app from your device. 2- Install the latest version of termux-api from F-Droid. 3- After installing termux-api, install the latest version of the termux app from F-Droid.

I followed this procedure on my Redmi 10c device running Android 13 and was able to resolve the issue.

WA1870 commented 3 months ago

From What I Know Termux-api Commands Hang Because Termux:APi App Is Not Running. I Have Same Problem Because First DuraSpeed Restricted Termux:API So I Uninstall DuraSpeed But Now Battery Manager Restricted Termux:API If I Disabled Battery Manager. So I Have To Reboot Mobile To Relaunch Termux:API App But Now I Found A Solution For This. I Don't Have To Reboot Mobile. But Battery Manager Kill Termux:API But This Is Best Solution For Me.

https://youtu.be/mL4FvCVJ4Os?si=i3uDgmeUXS2ERfF7

david-shu commented 2 months ago

I encountered the same problem. Through the following methods:

-Install termux API in termux terminal: pkg install termux-api -Install termux-api APK under Android -Run under termux terminal: termux-api-start

Now the notification has been successfully popped up:

-Run under termux terminal: termux-toast hello

iamgitcat commented 3 days ago

I have same issue on my new device. Termux-api commands work after restart, but eventually they stop working, they just hang. I tried above suggestions, replaced termux-api package and apk install order, changed termux source fdroid and github. Do not works. It works fine on my other Android 12 device and hangs on new Android 11 one. I found other issues related to termux-job-scheduler as well, this one is most needed one, I would really like to avoid cronie.

I found android battery saver added termux-api app to save mode, which I reverted and turned safe mode off, but even than it probably kills it.

Edit. I run a script in background which runs am start -n com.termux.api/com.termux.api.NfcActivity every 5 minutes. Also .bashrc checks if script is not running it starts it. That is a hack, but it works at least.