1

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

3
  • You didn't say which OS you use. Nor which terminal. Commented Nov 5, 2022 at 19:02
  • Yeah, I'm sorry, I'm using macOS System. I got this link that should show an answer. Unfortunately i cannot find it there. Commented Nov 5, 2022 at 21:51
  • I am voting to re-open this question as the duplicate that has been identified specifies gnome-terminal and is not applicable to macOS. Commented Nov 5, 2022 at 21:52

1 Answer 1

1

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.

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

5 Comments

Thanks man, it's works. If i can ask, can you tell me how i can deal with this on Windows analogous to what you wrote in the solution for macos ?
Sorry, I know very little about Windows.
Ok, thank you for your help. I have last question, how i can pass any variable to python script, I'm using something like this ``` \"$COUNTER\" \"$b\" \"$c\" ``` but when i run my python file and pass ``` print(sys.argv) ``` I'm getting empty list only with file name. Best, Kacper
Ugh! It's hard to read code in comments. Questions are free, so just ask a new one. Show the command you actually want to run, and show the code with your attempt.
Ok, i will pass link here stackoverflow.com/questions/74335091/…, Thanks

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.