
If the above Shell script runs without error, we try to run it in a background process using nohup command.

Run the script in the foreground to check if it can activate 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 Let's create a Shell script ( run_bg.sh) in the same directory that activates the virtual environment and runs the Python script. Running script using Bash in the background

The script takes 50 seconds to complete and will stop after printing 30 random numbers: 04:18:12.376506: It shows 3 random numbers continuously each 5 seconds. Run the Python script ( background_script.py): python background_script.py Random_integers = rng.integers(low=0, high=10 ** 6, size=3) The code is listed in background_script.py file: import time It continues the process until 30 integers are generated. Install the required packages: pip install -r requirements.txtĬreate a Python script that generates 3 random integers and sleep for 5 seconds at each step. Suppose the required packages are listed in requirements.txt: numpy=1.22.3 Running script in the foregroundĬreate and activate virtual environment using Python 3: python3 -m venv venv 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. We also want to run the script in the background without blocking a terminal window.

print statement debugging) from the script during the execution process. We may need to check the intermediate results (e.g. Let's assume, the script requires a long time to finish. To keep the global environment clean, we use a virtual environment ( venv) to install the dependencies. Suppose, we have a Python script that uses some Python packages. Jpython high-performance computing python Scenario
