Running Python Scripts & Redirection¶
Executing Python simulation scripts from the command line allows you to automate runs, log output, and keep long simulations running even after closing your terminal.
In this tutorial, you will learn:
1. Executing Python scripts from the terminal.
2. Understanding the standard input, output, and error streams (stdin, stdout, stderr).
3. Redirecting simulation logs to files (>, >>, 2>&1).
4. Running long-running tasks in the background with nohup and &.
5. Monitoring and terminating processes (ps, top, kill).
1. Running Scripts from the Terminal¶
Ensure your virtual environment is activated, then invoke python:
# Activate your project's virtual environment
source .venv/bin/activate
# Execute the simulation script
python run_cv_simulation.py
Making a Script Executable¶
You can run a script directly without explicitly typing python by adding a shebang on the first line of your .py file:
Then grant execute permissions using chmod:
2. Standard Streams & Redirection¶
Every Linux process automatically connects to three standard I/O streams:
- stdin (File descriptor 0) — Standard input.
- stdout (File descriptor 1) — Normal output (e.g. print()).
- stderr (File descriptor 2) — Error and warning messages.
Overwriting with >¶
Redirect standard output to a file (creates the file or overwrites existing content):
Appending with >>¶
Append output to an existing file without deleting its previous contents:
Capturing Both Output & Errors (2>&1)¶
To ensure that error tracebacks and warning logs are captured along with standard prints:
Here,2>&1 redirects stream 2 (stderr) into stream 1 (stdout), writing everything into simulation.log.
3. Running Tasks in the Background¶
Simulations that take minutes or hours to finish shouldn't freeze your terminal prompt.
Backgrounding with &¶
Append an ampersand & to run the command in the background immediately:
[1] 34821, and immediately returns your shell prompt.
Detaching with nohup¶
If you are logged into a remote machine over SSH and want the script to continue executing even if your Wi-Fi disconnects, prefix the command with nohup (no hang up):