Running multiple python scripts on terminal sequentially

You can achieve this by using a shell script that calls multiple Python scripts in sequence. When one Python script finishes, the next one will start. Here is a simple example of a bash shell script that runs two Python scripts sequentially:


```bash

#!/bin/bash


python3 /path/to/your/first_script.py

python3 /path/to/your/second_script.py

```


Save this script with a `.sh` extension, for example `run_scripts.sh`. Then you can run the script from the terminal like this:


```bash

bash run_scripts.sh

```


This will first run `first_script.py`, and when that is finished, it will run `second_script.py`.


Please replace `/path/to/your/first_script.py` and `/path/to/your/second_script.py` with the actual paths to your Python scripts.


If you're using a Windows system, you can do something similar with a batch file:


```batch

@echo off

python C:\path\to\your\first_script.py

python C:\path\to\your\second_script.py

```


Save this with a `.bat` extension, for example `run_scripts.bat`. You can then run this batch file in the Command Prompt like so:


```bash

run_scripts.bat

```


Again, replace `C:\path\to\your\first_script.py` and `C:\path\to\your\second_script.py` with the actual paths to your Python scripts.


What is the first script is already running?

If the first Python script is already running and you want to run another script once it finishes, you can do it manually by waiting for the first script to finish and then starting the second script. However, if you want this process to be automated, you can use a tool like `wait`.

Here's an example of how to do this in a bash shell:

1. First, you need to find out the Process ID (PID) of the running Python script. You can find this by using a command like `ps aux | grep python`. This will list all the running Python processes and their PIDs.

2. Once you have the PID of the running script, you can use the `wait` command to wait for it to finish. The `wait` command can only be used for child processes of the current shell, so you can use a workaround with a `while` loop:

```bash
while kill -0 <PID>; do sleep 1; done; python3 /path/to/your/second_script.py
```

Replace `<PID>` with the actual PID of the running script, and `/path/to/your/second_script.py` with the path to the script you want to run next. This command will check every second if the process with `<PID>` is still running, and once it's not, it will start the second Python script.

Please note that the `kill -0 <PID>` command doesn't actually kill the process, it just sends a signal to check if the process is still running. If the process doesn't exist, it will return a non-zero exit code, which will break the `while` loop and start the next script.

If you're using a Windows system, the process is a bit more complex as there's no direct equivalent to the `wait` command. You might need to use a tool like PowerShell, or write a short Python or batch script that periodically checks if the first script is still running, and starts the second script once it's not.

Comments