0

When I programmerly do open -F -W -n -g -a /Applications/Safari.app/Contents/MacOS/Safari https://kernel.org, I got back the process of open and its pid 93930.

But that's not the process (93931) of newly created Safari instance itself.

huocp 93931   0.0  1.9  6047904 160240   ??  S    11:55am   0:04.39 /Applications/Safari.app/Contents/MacOS/Safari
huocp 93930   0.0  0.1  4340272  12212 s004  S+   11:55am   0:00.07 open -F -W -n -g -a /Applications/Safari.app/Contents/MacOS/Safari https://kernel.org

Now I have no way to close the Safari process programmerly. I can close the open process programmerly by killing 93930, but it leaves 93931 running.

So the question is: is there a way to get the pid of the application instance created by open command?

93931 (safari) is not even a child process of 93930 (open), I tried with pstree command.

3
  • 1
    Just an FWI, open -FWnga Safari https://kernel.org does the same thing as the command you've shown, no need to draw it out the way you have. If you're looking to close the last opened occurrence of Safari, then this will do it: kill -nx Safari However, the next time you use the aforementioned open command, the -F option will be ignored. In other words, the next time there will be two tabs for https://kernel.org. Commented Jun 12, 2019 at 13:46
  • @user3439894 the mac kill is bsd version, it seems doesn't know about -nx, but pgrep does the trick.
    – huocp
    Commented Jun 12, 2019 at 23:19
  • 1
    pkill -nx Safari will do it. kill was a typo on my part, which I missed when copying it from Terminal after testing. Commented Jun 12, 2019 at 23:50

1 Answer 1

2

Similar question on StackOverflow

If you're programmatically doing this, one way to get the opened app's pid (subsequently using it to kill the process) is to get the grep the output of ps aux passing it the application's name.

ps aux | grep -v grep | grep -I <AppName> | sort -rn | head -1 | awk '{print $2}'

Basically, pipe the output of the ps aux command through grep and sort it based on the value of the start time column.

As pointed by @user3439894 below, there's an easier way without the above rigmarole.

pgrep -nx Safari

returns the PID of the last opened instance of Safari and

pkill -nx Safari

kills the last opened instance of Safari.

5
  • Thx! But that solution is not good enough, I have multiple safari instance running. The open -n will create a new safari instance which is the only one I want to kill.
    – huocp
    Commented Jun 12, 2019 at 10:31
  • 1
    There is absolutely no need whatsoever to go to such rigmarole with the compound command you've shown! E.g. pgrep -nx Safari does the same thing without the rigmarole. Additionally, if the goal is to close the last opened occurrence of e.g. Safari, then kill -nx Safari does it. Commented Jun 12, 2019 at 14:32
  • Oh nice. I'll update my answer. Commented Jun 12, 2019 at 15:13
  • The answer needs to be updated. kill on mac doesn't know about -nx, have to use pgrep to find out pid first.
    – huocp
    Commented Jun 12, 2019 at 23:30
  • 1
    @huocp, pkill -nx Safari will do it. kill was a typo on my part, which I missed when copying it from Terminal after testing. Commented Jun 12, 2019 at 23:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .