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.
Related
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
I am trying to run a computationally intensive Python program (which will take several hours) on a virtual machine (VM) through connecting to that virtual machine from a remote desktop's command line interface (CLI), will that Python program continue to run until it finishes, even when I close the CLI?
If not, how can I guarantee that the program will run until completion?
For your question, I assume you use the CLI command az vm run-command invoke to run the scripts in the remote VM, then here is the description:
You can't cancel a running script.
It means if you start running the script in the remote VM, then the script will run until the task finished, even if you close or cancel the CLI command.
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.
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
So I have tornado server setup on my vps running ubuntu 12.04. So when I am ssh'd into my server or am vnc'd in there the site loads static/templates files just fine. But when I exit out of ssh or terminate vnc python throws error that the file it was looking for does not exist.
[Errno 2] No such file or directory
When I execute the server I just run the python command to run it as a background process, and once its successfully running and exit out.
I have the server running at www.calapp.manangandhi.com
Edit: As per the answer below I was able to figure out a way for it to work. here si the link to daemonizing tornado application, there are other ways suggested in the thread as well. https://groups.google.com/forum/?fromgroups=#!topic/python-tornado/4cxKEFsS0RE
Are you trying to say that you run the server from within the ssh shell? If so, your problem is most likely that on shutdown of the shell, the software gets a HUP and disconnects despite being in the background. You need the software to daemonize and detach completely from the running terminal. If you're using a toolkit, look up "starting as daemon" or launch your software from within DJB's supervise or other system-wide launcher system.