How to run a python script always in linux (ubuntu) - python

I have a linux system server using ssh to connect. Now I have a python script.I want it run always.I using this commond
ubuntu:~$ nohup python3 -u ~/test/main.py > test.outs 2>&1 &
but I exit ssh connect,That python script exit at the same time.
What should I do?

You could run the script regularly / check it is running with a cronjob and this would also allow you to run the script at system start-up so it would keep running in the event of a reboot.
There are a few suggestions here;
https://superuser.com/questions/448445/run-bash-script-in-background-and-exit-terminal
Although this also suggests that nohub should stop the child process being killed when you exit the session. How are you aware that the script stops running upon exit?

There are multiple ways to do this and they depend on your use-case.
One way to do it is to install 'screen' on your server.
sudo apt-get install screen
Now, whenever you connect to your server with ssh, you can type 'screen' and then start your code there. (first time you do this, you get an explanation on screen, press space to skip it)
With this code executing, you type Ctrl + A and then Ctrl + D. This 'detaches' the screen. You can now disconnect and this 'screen' will keep existing and the code will still run.
When reconnecting, you might want to go back to this screen. Type
screen -ls
to get an overview of screens that you have running. (in this case, there'll only be one) They can be identified by the 5 numbers they all start with. So go back to this screen by typing
screen -r XXXXX
and now you're back. More information on this here:
https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/
Again, I don't know what your script does so it might not be the best solution.

You may use screen
After installation, you can start screen by sending screen to the console, and then run your script. When you type CTRL+A+D, the screen will disappear and you can exit your ssh connection.
If you re-open the screen you opened before, just type screen -r
If you have multiple screen instances, the app will show you numbers of screen when you type screen -r. You just need to find the id of the screen and type screen -r id i.e. screen -r 2643

Related

Run script inside Screen with python parallelSSH

what I'm trying do to is:
start Screen session
inside this screen session, execute an alias (which, for completeness, goes into a directory and runs a .sh) inside that screen
and I want to do this task with my python program using the library ParallelSSH, hence to many remote hosts.
I had many problems during my work, in particular I experienced that:
I can't run an all-in-one command to create a screen and run the alias inside that
I need some trick to send the "enter" key, otherwise the alias (or any command) will just be written and not executed in the screen
There is some tricky problem in terms of process handling, given that (to debug my code problems) I tried to run just the commands through ssh from command line, as follows:
ssh mymachine screen -dmS myscreen
ssh mymachine screen -r myscreen -X stuff "myalias ; wait"
ssh mymachine screen -r myscreen -X eval "stuff \015"
What I obtain is that the screen is create, then I noticed that without the wait in the second command it doesn't write the command inside the screen, or, probably, it writes it but then it disappear, hence the wait in some way does help. The last command is to send the Enter key to run the alias.
The most strange thing is that this command sequence doesn't work 100/100: sometimes it runs correctly, sometimes it doesn't, and I don't understand why.
First of all I would like to understand these commands from command line, because then I want to execute them through ParallelSSH in my python program. What I do at the moment is the following:
start = client.run_command('screen -dmS XXX')
xxx_run = client.run_command("screen -r XXX -X stuff 'myalias' ; wait")
I didn't wrote the third command since the second one yet doesn't work, while the first one runs correctly (the screen session with that name is created).
I can add that I'm already using ParallelSSH library to execute some other tasks, hence I have no problem with set up of hosts or client, it always worked good.
Can someone help me please? I suspect it has something to do with process handling, but I'm not so good with it.
Thank you all

Ways to run python script in the background on ubuntu VPS and save the logs

So, I have a python script which outputs some data into terminal from time to time. Im trying to run in on the Ubuntu VPS even after I close the SSH connection and still keep the logs somewhere.
Im saving the logs by using:
python3 my_script.py >>file.txt
and it works perfect, however when I try to run this process using
nohup python3 my_script.py >>file.txt &
so it runs in the background and after the ssh connection is closed it seems to save only the first log outputted from my_script.py. I've also tried running this in crontab but the result is similar - only the first log is saved.
Any tips? What am I doing wrong?
I could not understand what you mean "the first log". Maybe the first line of logs?
To run something in the background when SSH connection is closed, I prefer Linux screen, a terminal simulation tool that help you run your command in a sub-process. With it, you could choose to view your output any time in the foreground, or leave your process run in the background.
Usage (short)
screen is not included in most Linux distributions. Install it (Ubuntu):
$ sudo apt-get install screen
Run your script in the foreground:
$ screen python3 my_script.py
You'll see it running. Now detach from this screen: Press keys Ctrl-A followed by Ctrl-D. You'll be back to your shell where you run previous screen command. If you need to switch back to the running context, use screen -r command.
This tool supports multiple parallel running process too.
Something weird
I've tried to redirect stdout or stderr to a file with > or >> symbol. It turned out in failure. I am not an expert of this either, and maybe you need to see its manual page. However, I tend to directly write to a file in Python scripts, with some essential output lines on the console.

How to run a python script on a remote server that it doesn't quit after I log off?

I've created a script for my school project that works with data. I'm quite new to working remotely on a server, so this might seem like a dumb question, but how do I execute my script named
stats.py
so that it continues executing even after I log off PuTTy? The script file is located on the server. It has to work with a lot of data, so I don't want to just try something and then few days later find out that it has exited right after I logged off.
Thank you for any help!
There are many ways you can run a python program after you disconnect from an SSH session.
1) Tmux or Screen
Tmux is a "terminal multiplexer" which enables a number of terminals to be accessed by a single one.
You start by sshing as you do, run it by typing tmux and executing it. Once you are done you can disconnect from putty and when you login back you can relog to the tmux session you left
Screen also does that you just type screen instead of tmux
2) nohup
"nohup is a POSIX command to ignore the HUP signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout."
You can run it by typing nohup <pythonprogram> &

How do i stop process running a certain Python script?

I am running two different Python scripts on a Ubuntu VPS. One of them is the production version, and the other one is for testing purposes.
From time to time, I need to kill or restart one of them. However ps does not show which script does each Python process runs.
What is the conventional way to do this?
ps -AF will give you All processes (not only the ones in your current terminal, or run as your current user), in Full detail, including arguments.
Easiest way to keep it simple would be to create a screen for each. screen -S prod and screen -S test, then run the python script in the background of each and detach the screen (using ctrl +a+d) then when you need to stop one screen -r prod then kill it/restart then detach again.

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.

Categories