termux-api icon indicating copy to clipboard operation
termux-api copied to clipboard

termux-location sometimes returns no output when ran in crontab

Open igorzhilin opened this issue 1 year ago • 2 comments

Problem description

When running script via crontab, it sometimes writes empty files, even with retries. When running via bash while being at the same geolocation, it writes a file as expected.

Script save-location.sh:

retries=5
path="$HOME/location"
filename=$(date +'%Y-%m-%d_%H-%M-%S').json
fullpath="$path/$filename"

touch "$fullpath"

while [[ ! -s "$fullpath" && "$retries" -ne 0 ]]; do
  termux-location > "$fullpath"
  ((retries--))
done

When executed via bash save-location.sh, it writes the termux location to the file.

When running as a crontab job

*/1 * * * * bash ~/git/linux/android/save-location/save-location.sh 

then some of the files are empty, some are populated.

Steps to reproduce

pkg in cronie
mkdir ~/location
crontab -e
# edit in editor
*/1 * * * * bash <full path to save-location.sh>
# it will run every 1 minute
# save

Look into ~/location. Some of the files will be empty, some will contain the location json.

Expected behavior

All files contain termux location json.

Additional information

Termux Variables:
TERMUX_API_VERSION=0.50.1
TERMUX_APK_RELEASE=F_DROID
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=17942
TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.1
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirror.quantum5.ca/termux/termux-main stable main
Updatable packages:
c-ares/stable 1.34.1 aarch64 [upgradable from: 1.33.1]
glib/stable 2.80.5-2 aarch64 [upgradable from: 2.80.5-1]
tree-sitter/stable 0.24.3 aarch64 [upgradable from: 0.24.2]
termux-tools version:
1.43.6
Android version:
14
Kernel build information:
Linux localhost 6.1.43-android14-11 #1 SMP PREEMPT Mon Aug 26 02:08:56 UTC 2024 aarch64 Android
Device manufacturer:
samsung
Device model:
SM-A556B
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Installed termux plugins:
com.termux.window versionCode:15
com.termux.widget versionCode:13
com.termux.nix versionCode:188036
com.termux.styling versionCode:1000
com.termux.api versionCode:51
com.termux.boot versionCode:1000

igorzhilin avatar Oct 11 '24 15:10 igorzhilin

Maybe this is still due to the geolocation that termux-location cannot determine... This, however, contradicts my tests running directly from bash. Ultimately, I need a way to make sure termux-location succeeds.

igorzhilin avatar Oct 11 '24 15:10 igorzhilin

I have a similar problem; I simply check if I got back a valid JSON containing (for example) latitude:

#!/bin/sh

SESSION_DIR=~/record_gps-`date +%s`
mkdir ${SESSION_DIR}

while true; do
    loc_json=`termux-location`;
    if [ `echo "${loc_json}" | grep -c latitude` -ne 0 ]; then
        ts=`date +%s`;
        echo "${loc_json}" > ${SESSION_DIR}/${ts}.json;
        
        # NOTE: you would exit here
    fi

    sleep 3;
done

Basically you will have to loop and wait until you get back valid GPS data.
That is BTW. very similar to what I have to do on Linux with ModemManager (loop and wait until start receiving GPS data).
termux-location -r updates should work too, but it keeps stopping and crashing on my smartphone, so I loop.

mbohun avatar Nov 29 '25 05:11 mbohun