Linux python run in background

Bring the current Python program to background

A python script prints information to screen and then should go to background. How to do that from within the script itself?

@tmg: That’s not a dup. The link you posted is how to start a new process in the background, not how to make the current process go to the background after some actions.

2 Answers 2

Copying related code from Creating a daemon the Python way; please read the comments as to why that code is quite thought-out.

def createDaemon(): """Detach a process from the controlling terminal and run it in the background as a daemon. """ try: # Fork a child process so the parent can exit. This returns control to # the command-line or shell. It also guarantees that the child will not # be a process group leader, since the child receives a new process ID # and inherits the parent's process group ID. This step is required # to insure that the next call to os.setsid is successful. pid = os.fork() except OSError, e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): # The first child. # To become the session leader of this new session and the process group # leader of the new process group, we call os.setsid(). The process is # also guaranteed not to have a controlling terminal. os.setsid() # Is ignoring SIGHUP necessary? # # It's often suggested that the SIGHUP signal should be ignored before # the second fork to avoid premature termination of the process. The # reason is that when the first child terminates, all processes, e.g. # the second child, in the orphaned group will be sent a SIGHUP. # # "However, as part of the session management system, there are exactly # two cases where SIGHUP is sent on the death of a process: # # 1) When the process that dies is the session leader of a session that # is attached to a terminal device, SIGHUP is sent to all processes # in the foreground process group of that terminal device. # 2) When the death of a process causes a process group to become # orphaned, and one or more processes in the orphaned group are # stopped, then SIGHUP and SIGCONT are sent to all members of the # orphaned group." [2] # # The first case can be ignored since the child is guaranteed not to have # a controlling terminal. The second case isn't so easy to dismiss. # The process group is orphaned when the first child terminates and # POSIX.1 requires that every STOPPED process in an orphaned process # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the # second child is not STOPPED though, we can safely forego ignoring the # SIGHUP signal. In any case, there are no ill-effects if it is ignored. # # import signal # Set handlers for asynchronous events. # signal.signal(signal.SIGHUP, signal.SIG_IGN) try: # Fork a second child and exit immediately to prevent zombies. This # causes the second child process to be orphaned, making the init # process responsible for its cleanup. And, since the first child is # a session leader without a controlling terminal, it's possible for # it to acquire one by opening a terminal in the future (System V- # based systems). This second fork guarantees that the child is no # longer a session leader, preventing the daemon from ever acquiring # a controlling terminal. pid = os.fork() # Fork a second child. except OSError, e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): # The second child. # Since the current working directory may be a mounted filesystem, we # avoid the issue of not being able to unmount the filesystem at # shutdown time by changing it to the root directory. os.chdir(WORKDIR) # We probably don't want the file mode creation mask inherited from # the parent, so we give the child complete control over permissions. os.umask(UMASK) else: # exit() or _exit()? See below. os._exit(0) # Exit parent (the first child) of the second child. else: # exit() or _exit()? # _exit is like exit(), but it doesn't call any functions registered # with atexit (and on_exit) or any registered signal handlers. It also # closes any open file descriptors. Using exit() may cause all stdio # streams to be flushed twice and any temporary files may be unexpectedly # removed. It's therefore recommended that child branches of a fork() # and the parent branch(es) of a daemon use _exit(). os._exit(0) # Exit parent of the first child. 

Источник

Читайте также:  Linux application x executable

Running a Python Script in the Background

This is a quick little guide on how to run a Python script in the background in Linux.

Make Python Script Executable

First, you need to add a shebang line in the Python script which looks like the following:

This path is necessary if you have multiple versions of Python installed and /usr/bin/env will ensure that the first Python interpreter in your $PATH environment variable is taken. You can also hardcode the path of your Python interpreter (e.g. #!/usr/bin/python3 ), but this is not flexible and not portable on other machines. Next, you’ll need to set the permissions of the file to allow execution:

Start Python Script in Background

Now you can run the script with nohup which ignores the hangup signal. This means that you can close the terminal without stopping the execution. Also, don’t forget to add & so the script runs in the background:

If you did not add a shebang to the file you can instead run the script with this command:

nohup python /path/to/test.py & 

The output will be saved in the nohup.out file, unless you specify the output file like here:

nohup /path/to/test.py > output.log & nohup python /path/to/test.py > output.log & 

Find and Kill the Running Process

You can find the process and its process Id with this command:

If you want to stop the execution, you can kill it with the kill command:

It is also possible to kill the process by using pkill, but make sure you check if there is not a different script running with the same name:

Читайте также:  Linux mint icons png

Output Buffering

If you check the output file nohup.out during execution you might notice that the outputs are not written into this file until the execution is finished. This happens because of output buffering. If you add the -u flag you can avoid output buffering like this:

Or by specifying a log file:

nohup python -u ./test.py > output.log & 

Источник

Linux python run in background

Suppose, we have a Python script that uses some Python packages. To keep the global environment clean, we use a virtual environment ( venv ) to install the dependencies. Let’s assume, the script requires a long time to finish. We may need to check the intermediate results (e.g. print statement debugging) from the script during the execution process. We also want to run the script in the background without blocking a terminal window.

I will mock this scenario and will show how to run the Python script inside a virtual environment in the background that writes partial output to a file.

Running script in the foreground

python3 -m venv venv source venv/bin/activate 
pip install -r requirements.txt 
import time from datetime import datetime import numpy as np count = 0 while True: current_time = datetime.now() rng = np.random.default_rng() random_integers = rng.integers(low=0, high=10 ** 6, size=3) print(f": ") count += 1 if count == 10: print(f"Script completed") break time.sleep(5) 
python background_script.py 
2022-03-18 04:18:12.376506: [874273 493185 580873] 2022-03-18 04:18:17.378234: [175390 989369 463402] 2022-03-18 04:18:22.382138: [721299 669516 197204] 2022-03-18 04:18:27.386138: [537835 627442 400862] . . Script completed 

Running script using Bash in the background

  • Let’s create a Shell script ( run_bg.sh ) in the same directory that activates the virtual environment and runs the Python script. We have used -u parameter in the Python run command that enables printing the partial outputs to the standard output:
#!/usr/bin/env bash set -e source "./venv/bin/activate" python -u background_script.py 
nohup ./run_bg.sh > custom-output.log & 

alt Run Python Script in background with partial output

Sometimes we may need to stop the script running in the background. We need to kill the process which runs the script to do so. The following command will list the process ID for the script:

ps aux | grep background_script.py 

It will return the process details which contain the process ID, typically in the second column. Then we can kill the process by using:

Читайте также:  Linux socket accept blocking

Use case

I was trying to run a hefty Python script that requires a virtual environment in a cluster computing platform. I did not want to keep the terminal session open as the script would take days to finish. I also wanted to see the partial output while the script was running.

So, I followed this approach to run the Python script inside a virtual environment in the background that wrote the partial output to a file.

References

Cite This Work

APA Style Shovon, A. R. (2022, March 18). Run a Python script inside a virtual environment in the background. Ahmedur Rahman Shovon. Retrieved June 24, 2023, from https://arshovon.com/blog/python-background/ MLA Style Shovon, Ahmedur Rahman. “Run a Python script inside a virtual environment in the background.” Ahmedur Rahman Shovon, 18 Mar. 2022. Web. 24 Jun. 2023. https://arshovon.com/blog/python-background/. BibTeX entry

Источник

How to run a python program in the background even after closing the terminal? [duplicate]

But my whole program will be stopped if I close the terminal, Is there any way of running this python program in the background so that if I close my terminal then it still keeps on running? And also after running this program in the background, how do I find out my actual program whether it is still running or not if I am logging back again to that terminal?

And, when you run a program in the background with & , it will print a PID that you can search for with something like ps aux | grep PID .

2 Answers 2

Use the shebang line in your python script. Make it executable using the command,

Use no hangup to run the program in the background even if you close your terminal,

or simply (without making any change in your program)

nohup python /path/to/test.py & 

Do not forget to use & to put it in the background.

Role of nohup : nohup makes your script ignore SIGHUP , and redirects stdout/stderr to a file nohup.out, so that the command can continue running in the background after you log out. If you close the shell/terminal or log off, your command is no longer a child of that shell. It belongs to init process. If you search in pstree you’ll see it is now owned by process 1 (init).

To see the process again, use in terminal,

That cannot be brought back to the foreground because the foreground (as the terminal already closed) no longer exists. So there is no way to get that terminal back again once it is closed.

Источник

Оцените статью
Adblock
detector