How to Execute a Script Remotely in Python Using Ssh

Run local python script on remote server

It is possible using ssh. Python accepts hyphen(-) as argument to execute the standard input,

cat hello.py | ssh user@192.168.1.101 python -

Run python --help for more info.

Execute remote python script via SSH

This might work, or something similar:

ssh user@remote.host nohup python scriptname.py &

Basically, have a look at the nohup command.

Run a script on remote host without caring ssh session

You can run the script either in a screen or can run the process in nohup+bg. I always prefer Screen but let me explain both methods.

1. nohup

You can use nohup command to run a process by detaching from terminal like this nohup python /my/folder/abc.py &
This by default creates nohup.out file where all the logs will be stored.
If you want custom file then you can use redirection then it will be nohup python /my/folder/abc.py >> abc.log &

In single command it will be

ssh user@remotehost 'nohup python /my/folder/abc.py >> abc.log &'

nohup wikipedia

2. Screen

From the docs.

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.

When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal.

Screen Manual

So you can directly run the script in screen using ssh and then you can view the logs whenever needed either by attaching to screen or you can redirect the logs to some file directly or redirect to both file and out put using tee.

Run command in screen and print output in stdout(terminal).

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py; exec bash")'

Run command in screen and redirect output to file.

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py >> abc.log &2>1; exec bash")'

Run command in screen and redirect output to both file as well as to stdout(terminal).

ssh user@remotehost '(screen -dmS ScreenName bash -c "python /my/folder/abc.py &2>1 |tee abc.log; exec bash")'

Note: In all the above commands exec bash is needed else the screen will terminate once the job complets.

Any one of the above commansd should do the job.

In all the above cases you can attach the screen ScreenName using screen -r ScreenName and can see the logs.

I always recommend stderr redirection when redirecting to a file.

some references on using linux screen

  1. 10 Screen Command Examples to Manage Linux Terminals
  2. How To Use Linux Screen

Paramiko: Execute a script on a remote server and actually see it running on remote desktop

Ned,

I would try referring to this post here:

https://serverfault.com/questions/690852/use-powershell-to-start-a-gui-program-on-a-remote-machine

Executing the command remotely using PsExec with the -i arg seems to have worked for them. Documentation and info on PsExec can be found at https://docs.microsoft.com/en-us/sysinternals/downloads/psexec .



Related Topics



Leave a reply



Submit