How do I bring a Python script's output to foreground/background in Linux?

joepetrowski

I'm running a Python script on AWS (Amazon Linux AMI). The script is meant to run 24/7 and prints out to the interpreter or command terminal.

It's still in development, so I like to check to see how it's behaving or if it's stopped due to an error. But, I want to be able to close my ssh connection or turn off my local machine without interrupting the script, and then ssh back in and see it in real-time.

First I used:

[[email protected] dir]$ nohup python3 myscript.py &

That worked fine for closing the ssh connection, coming back in, and seeing that it was still running, and then writing all the print statements to nohup.out.

Is there a way for me to bring this to the foreground and see the print statements in real-time, and then send it back to the background to disconnect from ssh without interrupting the program?

The code has the general form:

import time

count = 0

while count < 100000:
    print('Im working!')
    time.sleep(10)
    count += 1

print('All finished')
emredjan

You can use tmux or screen (depending on which is available on the system) to run your program in a terminal multiplexer, detach from it and close the connection. When you return, you can attach to the same session and see your program running.

For tmux

$ tmux

# run your program in the tmux shell:
$ python3 myscript.py

Detach from the tmux session with Ctrl + b and then d

You can now safely exit your ssh session

Next time you log in, just tmux attach and you can see you script running.

Addition: For screen the detach command is Ctrl + a and d, reattaching is done with screen -r.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I bring HEREDOC text into a shell script variable?

How do I use a script's output in an Ant Conditional

how do I pipe output of a python script into an Rscript?

How do I send VBA output to a Python script?

How do I run a python script in parallel with different arguments in linux?

How do I schedule a Python script to run in a Linux VM?

How do I schedule the execution of a python script in linux with the at command?

How do I pipe output of one python script to another python script

How do I output a variable in Zapier's python node to an email?

How do I read the output of commands in a script

How do I save the output of a script to a file?

How do I write the output of this loop to a script

How do I reformat the output of a BASH script?

How do I make a bash script know the output of a CLI program it executed in Linux?

How do I hide my script's output while also parsing content to display feedback?

How do I bring a polygon to the foreground in OpenGL?

How can I bring in updated google spreadsheet values into my python script without stopping and restarting it?

How do I create collapsible sections in the gitlab output from inside python script?

How do I get linux to automatically run my python script in the Python interpreter?

How do I clear the output screen in python?

How do I interactive plotting with matplotlib 1.5.1 from a python script on linux?

How do I call a python 3 cgi script on RedHat Enterprise Linux using their Software Collections?

How do I output contents in browser while the script is running?

How do I reliably capture the output of 'ls' in this script?

How do I output text or return an object from a script?

How do I make a shell script that sends output to a process

How do i use Get-clipboard output in a powershell script?

How do I use a CloudFormation output value in a script with Ansible

How do I hide the output from a subprocess and continue the script execution?