DEV Community

Mohammed Samgan Khan
Mohammed Samgan Khan

Posted on • Originally published at codebysamgan.com

Python script to run multiple servers simultaneously on a local machine on Mac.

There are a number of times we have a bunch of servers we need to run to fully run an application, yes i am talking about the microservice arch. It's annoying to go to each repo and get the server running. Below is the script for making it automated.


#!/usr/bin/python3

"""
Script that starts the servers from the "services" array.
services = [
  {
    "name": "service_name",
    "path": "path_to_service",
    "command": "command_to_run",
  },
  ...
]
for a single server, please pass the name of the service to start...
author: msamgan
created at: 2022-05-01
updated at: 2022-07-22
version: 1.1.0
signature: ./server | ./server <service_name>
"""

import os
import sys

services = [

]

# uncomment if on mac os..
# os.system("say 'booting shopini servers'")
os.system("sleep 1")

n = len(sys.argv)

serverCommand = ""
for service in services:
    if n > 1:
        if service["name"] != sys.argv[1]:
            continue

    # say = "say 'booting " + service["name"] + " server'"
    # os.system(say)
    os.system(
        "ttab.sh -w -t '"
        + service["name"]
        + "' -d "
        + service["path"]
        + " "
        + service["command"]
    )

# os.system(
#     "say 'all shopini servers are running at there full potentials, have a nice day!'"
# )
# kill command: kill -9 $(lsof -ti:5100)

Enter fullscreen mode Exit fullscreen mode

The ttab.sh library you will need

Top comments (0)