Home > Enterprise >  How to execute shell script command in other terminal window
How to execute shell script command in other terminal window

Time:11-06

i have a quick question. I just wonder how i can execute command in shell script. Code should checking if python script is running, if not i want to run it again but in another terminal window. Shell code below.

while :
do
    if pgrep -f "python instagram_bot.py" &>/dev/null; then
        echo "it is already running"
        sleep 1
    else
        python instagram_bot.py
    fi
done

I'm using macOS system

In else statement i just wondering it is possible to execute command that open new terminal and run python script.

Thanks for any help, Best, Kacper

CodePudding user response:

Assuming you are using Terminal on macOS, you can run:

osascript -e 'tell application "Terminal" to activate' \
  -e 'tell application "System Events" to keystroke "n" using {command down}' \
  -e 'tell application "Terminal" to do script "python instagram_bot.py" in front window'

You can start Terminal by using a "Spotlight search" by typing Space and typing "Terminal" and hitting Enter as soon as it guesses "Terminal.app".

Then, at the top-left of the screen, click Shell -> New Window and you will see that the shortcut for a new window is n. That's why the osascript above does keystroke "n" using {command down}' - it starts a new window.

  • Related