How to launch cli applications from command line?


doragasu

Member
Joined
Jun 2, 2008
Messages
325
I have built telegram command line client and if copy the files, open a terminal and manually launch it, it runs perfect. But when I try launching it from a PND, the terminal window pops up for a fraction of second, and closes.

This is the file tree I'm using:


.
├── lib
│   └── libconfig.so.8
├── PXML.xml
├── telegram
│   ├── telegram
│   └── tg.pub
├── telegram.png
├── telegram-preview.png
├── telegram-readme.md
└── telegram.sh
This is the launch script telegram.sh:


#!/bin/sh
export HOME=/mnt/utmp/telegram
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/mnt/utmp/telegram/lib
cd telegram
terminal -e "bash ./telegram"

# Wait for telegram to finish
for pid in $(ps hX | awk '{print $1}')
do
    command=$(ps h --pid $pid -o command)
    if [ "$command" == "bash ./telegram" ]
    then
        alive=1
        while [ "$alive" -eq "1" ]
        do
            sleep 1s
            kill -0 $pid;
            if [ "$?" -ne "0" ]; then
                alive=0
            fi
        done
    fi
done
fi
And this is the resulting pndrun_telegram.out file:


=======================================================================================
PND             : /media/mmcblk0p1/pandora/menu/telegram-cli.pnd
PND_FSTYPE      : ISO
APPDATADIR      : /media/mmcblk0p1/pandora/appdata/telegram
APPDD_FSTYPE    : vfat
PND_CPUSPEED    : 800
EXENAME         : telegram.sh
ARGUMENTS       : <unset>
=======================================================================================
[ START ]--- Mount the PND ----------
Mounting : mount -o ro "/dev/loop0" "/mnt/utmp/telegram"
Mounting the Union FS : mount -t aufs -o exec,noplink,dirs="/media/mmcblk0p1/pandora/appdata/telegram=rw+nolwh":"/mnt/pnd/telegram=rr" none "/mnt/utmp/telegram"
[SUCCESS]--- Mount the PND ----------
[ START ]--- Set CPU speed ----------
[SUCCESS]--- Set CPU speed ----------
[ START ]--- Starting the application ( telegram.sh  ) ----------
(terminal:5584): Terminal-DEBUG: D-Bus reply error: Name "org.xfce.Terminal4" does not exist (terminal-error-quark: 4)
./telegram.sh: line 24: syntax error: "fi" unexpected
[ FAILED]--- Starting the application ( telegram.sh  ) ----------
[ START ]--- Restoring the frame buffer status ----------
[SUCCESS]--- Restoring the frame buffer status ----------
[ START ]--- uMount the PND ----------
[ START ]--- Waiting the Union to be available ----------
[SUCCESS]--- Waiting the Union to be available ----------
auplink:plink.c:223: AUFS_CTL_PLINK_MAINT: Inappropriate ioctl for device
rmdir: failed to remove `/mnt/utmp/telegram': Device or resource busy
[ START ]--- Waiting the PND mount dir to be free ----------
[SUCCESS]--- Waiting the PND mount dir to be free ----------
cleanup done
[SUCCESS]--- uMount the PND ----------
=======================================================================================
Return code is : 4
Any suggestions?
 
Hum, maybe try to use #!/bin/bash instead of sh?

Also, I suggest you to create/call a ".sh" file (situated in the root) that itself defined correct PATH / LD_LIBRARY_PATH and then launch telegram (and so it wiil be "bash -rcfile run_telegram.sh" in the calling script).
 
The problem is that terminal first looks for an already running terminal and if it finds one just asks the running instance to open a new window. This is totally stupid default behavior, but fortunately can at least be overidden using

Code:
terminal --disable-server
 
Thanks for your tips. I followed them all and almost got it working. It now starts and keeps running, but instead of starting in a new terminal window, the window never pops up, and the output is grabbed by the pnd run script (I can see it in the .out log file).

I have separated the run process in two scripts. The first one that is run is tgrun.sh:


#!/bin/bash

CMD=bash ./telegram.sh

terminal --disable-server -e "$CMD"

# Wait for telegram to finish
for pid in $(ps hX | awk '{print $1}')
do
command=$(ps h --pid $pid -o command)
if [ "$command" == "$CMD" ]
then
alive=1
while [ "$alive" -eq "1" ]
do
sleep 5s
kill -0 $pid;
if [ "$?" -ne "0" ]; then
alive=0
fi
done
  fi
done

And the script that actually runs the program, telegram.sh:

Code:
#!/bin/bash
export HOME=/mnt/utmp/telegram
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/mnt/utmp/telegram/lib
cd telegram
./telegram
 
 
Removing --disable-server shows no difference, again it starts and keeps running, but no window pops up, and output is redirected to the .out log file :(
 
OK, got the little bastard working!

Inside telegram.sh script I was launching the program just as "./telegram.sh". I just launched it ALSO using "terminal --disable-server -e ./telegram" and now it's working. Dunno why I had to launch both telegram.sh script and telegram binary using terminal command...

EDIT: Another question: is there a way for the terminal to use utf-8 encoding? Non english characters (like 'Á') aren't shown properly.

EDIT2: Now I have another problem: the program is still saving its configuration to the default $HOME/.telegram, instead of savint it to the appdata directory of the SD card :(
 
Last edited by a moderator:
OK, got it working. I had to patch the way it gets the home directory (it doesn't get it from HOME env variable). The only thing that doesn't work right now is non english characters, but I'm suspecting that's not possible (or at least not easy) to achieve.

Time to upload to the repo :)
 
AFAICT terminal respects the locale settings, but I'm no expert where it should be set up. Probably during session initialization after login. Testing with the following allows me to input umlauts using the compose key.

Code:
LANG=en_US.UTF-8 terminal
 
I have tried both LANG=en_us.UTF-8 and LANG=es_ES.UTF-8 and it doesn't me allow to input tildes (e.g. áéíóú), it just displays an interrogation mark. But I must be doing something wrong, because I can neither write umlauts.  :S
 
I have made another test. As stated before, if I launch "LANG=en_US.UTF-8 terminal", I can write neither accents nor umlauts.

What I have tried now is: "LANG=en_US.UTF-8 terminal --disable-server", and now it works!, I can write accents and umlauts.

So I decided to update the PND, and tried using only one script: I removed tgrun.sh and left only telegram.sh that right now looks like this:


#!/bin/bash
export HOME=/mnt/utmp/telegram
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/mnt/utmp/telegram/lib
cd bin
LANG=en_US.UTF-8 terminal --disable-server -T "telegram-cli" -e ./telegram
It works perfect, and non english characters are displayed nicely. The keys to making it work  using only one script are calling terminal with --disable-server, and with -e "command", and NOT -e "bash command" (that is what I was doing at first).

Thanks for help!!!
 
Yeah, without disable-server the ENV of the terminal invocation effectively gets ignored when it hands over to the already running terminal process. IMO this is stupid to have as the default behavior, but it is probably ok in typical desktop usage because LANG shouldn't be tampered with inside the session (though I don't think there actually are significant benefits with having only one running terminal process).
 
Back
Top