3

I have many python scripts and it is a pain to run each one of them individually by clicking them. How to make a batch file to run them all at once?

1
  • 1
    What is your environment? Commented Feb 6, 2018 at 17:24

3 Answers 3

6

just make a script like this backgrounding each task (on windows):

start /B python script1.py
start /B python script2.py
start /B python script3.py

on *nix:

python script1.py &
python script2.py &
python script3.py &

Assuming non of your script requires human interaction to run

Sign up to request clarification or add additional context in comments.

Comments

2

Use the start command to initiate a process.

@echo off
start "" foo.py
start "" bar.py
start "" baz.py

Re comment: “is there way to start these minimized?”

You can always ask about how a command works by typing the command name followed by a /?. In this case, start /? tells us its command-line options include:

MIN          Start window minimized.

Hence, to start the application minimized, use:

start "" /MIN quux.py

1 Comment

Yes this is helpful, is there way to start these minimized?
1

Multiprocessing .py files simultaneously

Run as many .py files simultaneously as you want. Create for each .py a .bat to start the python file. Define all the .bat files in the list of lists. The second parameter in the list is a delay to start the .bat file. Don't use zero for the delay. It works fine. On this way You leave parallelism to the operating system which is very fast and stable. For every .bat you start opens a command window to interact with the User.

from apscheduler.schedulers.background import BackgroundScheduler
import datetime as dt
from os import system
from time import sleep

parallel_tasks = [["Drive:\YourPath\First.bat", 1], ["Drive:\YourPath\Second.bat", 3]]

def DatTijd():
    Nu = dt.datetime.now()
    return Nu

def GetStartTime(Nu, seconds):
    StartTime = (Nu + dt.timedelta(seconds=seconds)).strftime("%Y-%m-%d %H:%M:%S")
    return StartTime

len_li = len(parallel_tasks)
sleepTime = parallel_tasks[len_li - 1][1] + 3
Nu = DatTijd()
for x in range(0, len_li):
    parallel_tasks[x][0] = 'start cmd /C ' + parallel_tasks[x][0]
    # if you want the command window stay open after the tasks are finished use: cmd /k in the line above
    delta = parallel_tasks[x][1]
    parallel_tasks[x][1] = GetStartTime(Nu, delta)

JobShedul = BackgroundScheduler()
JobShedul.start()

for x in range(0, len_li):
    JobShedul.add_job(system, 'date', run_date=parallel_tasks[x][1], misfire_grace_time=3, args=[parallel_tasks[x][0]])

sleep(sleepTime)
JobShedul.shutdown()
exit()

Example.bat

echo off
Title Python is running [Your Python Name]
cls
echo "[Your Python Name] is starting up ..."
cd Drive:\YourPathToPythonFile
python YourPyFile.py

1 Comment

Note that the command for python might also be python3 in Example.bat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.