Run command on server using Plink and keep it running after disconnecting - python

I have a Raspberry Pi which I use to play a video on a loop. I have a button which I use to end the video to display the desktop wallpaper which I have as a static image.
To do this I use a simple Python script that launches omxplayer and loops waiting for the button to be pressed, when pressed it kills omxplayer, waits a while then re-starts the loop.
This all works fine.
I am wanting to use plink to launch this script from a Windows machine, and have used the following:
plink.exe -ssh pi#192.168.0.201 -pw ****** "sudo python /home/pi/ftp/files/button.py"
This launches the script no problem, but because the script does not 'end' the batch file just sits there.
I have other batch files using plink to kill the script and others to turn the monitor on & off using CEC all of which work fine because plink gets a return, but because the Python script runs indefinitely there is nothing returned, so plink just seems to hang.
So..Question is, can plink be told to send the command and terminate, regardless of response, or (and I've looked for this with no joy) is there a way of setting a timeout for plink to give up waiting for a response?

If I understand your question correctly (not sure), you want plink to start the the script on the server and exit (keeping the script running).
The plink is just an alternative SSH client, similar to OpenSSH ssh. So just use the same techniques you will find on Internet for ssh.
Two of zillions of questions on this topic:
How to run a command in background using ssh and detach the session
Use SSH to start a background process on a remote server, and exit session

Related

Closing Putty window terminates a program

I have a VPS server to which I connect using Putty. There's a python script that has to be running 24/7. But once I close Putty, the script stops.
Is this how it's supposed to work or am I doing something wrong?
For most of the operating systems that you host on your VPS server, with the closing of the putty terminal in your local machine, the process in the server gets killed. For that reason, you need to run the process as a daemon. To make it simpler, you need to run the command in your putty terminal in such a way that if you had control of the remote system's terminal or command line and run a command, closing the terminal or the command line doesn't kill the process there. How to do it is dependent on the operating system that is hosted on the server. Here is a way to start in Unix. Other operating systems enable you to do the same in more or less similar ways, which I will leave for you to research.

Start terminal on Raspberry Pi using SSH

My question regards SSH on Raspberry Pi.
I am able to successfully ssh on to the Pi using the command:
sudo ssh pi#<ipaddress>
and then entering the password.
Let's say I have a Python script file on the Pi that I execute over SSH. Let's say the script reads:
import time
while True:
print('Hello')
time.sleep(1)
This will print 'Hello' every second whilst the terminal/command prompt window is still open (that is, the computer I am using to access the Pi is running and the SSH session remains open). If I close the connection, then the code will stop being executed on the Pi.
Is there a way I can use SSH to keep the code running on the Pi even when I close the window running SSH on the computer I am using to access the Pi? As in the Pi will keep printing 'Hello' even after I shut down my computer. Maybe by entering a command to open a terminal window on the Pi itself and running the script in that terminal window?
Is there a way this can be done?
Thanks
There are two options I can think of:
create a cron job. This method is usually used to execute scripts/programs repeatedly. The job is triggered by the cron program, so it doesn't matter whether or not you are connected to the Pi, as long as it runs. You just have to connect once and setup the job (typically using crontab -e).
use screen (on Wikipedia) or tmux (on Wikipedia). Those are called terminal multiplexers, and allow you to keep shells (and thus any script/program) running although you aren't connected. Note that, in this case, you will have to manually start your script each time, so this solution is well-suited to scripts that run for a long time but are not started too often.

Stopping Django's Server [duplicate]

So im using ruby on rails in windows (i hear you all spitting your coffee onto the screen), its only a short term thing. (using ubuntu at home) So i tried to fire up webrick this afternoon and i get the error message
TCPServer Error, only one usage of each socket address is normally permitted
So it seems as if port 3000 is still running from last week? My question is how do i kill the process from the Windows command line. normally i have to press ctrl and pause/break in windows as ctrl c is not working which is only killing the batch process it seems..
Any solutions welcomed
Edit
So it seems as if
tasklist
will give me the list of processes, but where do i find the process for running the webrick server?
ruby.exe is not listed as a running process
Try using netstat -a -o -n to determine the pid of the process running on port 3000. Then you should be able to use taskkill /pid #### to kill whatever process is running on that port.
Probably not the most graceful way to do it, but I think it should work.
EDIT
You'll probably have to also use the /F flag to force-kill the process. I just tried it on my local machine, and that worked fine.
Go into rails_project\tmp\pids and delete the .pid file in there.
run:
rails server

Make python script to run forever on Amazon EC2

I have a python script that basically runs forever and checks a webpage every second and notifies me if any value changes. I placed it on an AWS EC2 instance and ran it through ssh. The script was running fine when I checked after half an hour or so after I started it.
The problem is that after a few hours when I checked again, the ssh had closed. When I logged back in, there was no program running. I checked all running processes and nothing was running.
Can anyone teach me how to make it run forever (or until I stop it) on AWS EC2 instances? Thanks a lot.
Edit: I used the Java SSH Client provided by AWS to run the script
You can use Linux screen.Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session.
To install:
sudo apt-get install screen
Start a new session:
screen -S <screen_name>
Run your process as you run it in the screen session. If you want to back to your main terminal press key shortcut ctrl+a+d. And also view the screen by typing,
screen -r <screen_name>
You can run the program using the nohup command, so that even when the SSH session closes your program continues running.
Eg: nohup python yourscriptname.py &
For more info you can check the man page for it using
man nohup.

to keep the script running even after internet connection goes off

I had putty on one server and run a python script available on that server. That script keep on throwing output on terminal. Later on, my internet connection went off but even then i was expecting my script to complete it job as script is on running on that server. But when internet connection resumed, I found that script has not done its job.
So is this expected ? If yes, then what to do to make sure that script runs on server even though internet connection goes off in-between?
Thanks in advance !!!
You should use screen which will let you "detach" your process from the actual terminal you're in.
On the server, you can install tmux or screen. These programs run the program in the background and enable you to open a 'window', If I use tmux:
Open tmux: tmux
Detach (run in background): press Ctrl-b d
reattach (open a 'window'): tmux attach

Categories