0

I am developing a python application for a remote system which has a device connected via a serial port.

At the moment my workflow looks like this

  • write code locally
  • commit to git
  • git push
  • change to terminal window holding ssh session
  • git pull
  • run python code

This is kind of awkward and I want to speed things up and increase my work efficiency using some scripts

I have written two scripts, one for local and one for remote

My local script looks like this

#!/bin/bash

# do git commit
if [ "$#" -eq 3 ]
then
    if [ "$1" == "-m" ]
    then
        git commit -am "$2"
    else
        echo "syntax error, -m expected as argument 1"
        exit
    fi
else
    git commit -am "execute.sh script"
fi

# do git push
git push

# login to ssh and execute script
ssh <user>@<ip> 'bash -s' < remote-execute.sh

My remote-execute.sh looks like this

#!/bin/bash

# cd
cd <dir>

# do git pull
git pull

# run python
python3 main.py

My python code uses Python Curses (ncurses) for GUI interaction

When I run these scripts, I encounter the error

Traceback (most recent call last):
  File "Monitor.py", line 1140, in <module>
    curses.wrapper(main)
  File "/usr/lib/python3.5/curses/__init__.py", line 73, in wrapper
    stdscr = initscr()
  File "/usr/lib/python3.5/curses/__init__.py", line 30, in initscr
    fd=_sys.__stdout__.fileno())
_curses.error: setupterm: could not find terminal

This must be due to how I am using ssh with the "-s" switch to execute the remote script.

Is there a way to fix this? It seems like some kind of terminal stuff isn't being forwarded properly? I can't say anything more than this because it's a bit beyond my knowledge.

0

The ssh client does not allocate a terminal when being run in "batch" mode (with a command provided). You need to use the -t option to enable this if you're asking it to run an interactive app.

Additionally, redirecting stdin doesn't make sense when trying to run an interactive program – although you could force tty allocation (using -tt), your program would just try reading input from the .sh file instead of keyboard.

ssh -t <user>@<ip> "cd <dir> && git pull --ff-only && python3 main.py"

ssh -t <user>@<ip> "$(cat remote-commands.txt)"
  • "Pseudo-terminal will not be allocated because stdin is not a terminal", then same error "could not find terminal" - any idea what is wrong here? Also what is the reason for using "-ff-only" with git? – user3728501 Sep 3 '19 at 12:00
  • Ah, yes, the second variant (without < redirection) should work. – user1686 Sep 3 '19 at 12:02
  • Is there a way to feed the output of another file instead of putting the contents of a file in "" ? – user3728501 Sep 3 '19 at 12:03
  • pull --ff-only is safeguard used out of habit – whenever you're automating a pull (e.g. from post-receive hook), you don't want it to leave a half-finished merge; you want it to either completely succeed or completely abort. – user1686 Sep 3 '19 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.