Skip to main content
Question

Close and Re-open Script

  • March 20, 2018
  • 2 replies
  • 15 views

Forum|alt.badge.img+1

I would like to create a script to close google Chrome and then Re-open the program .

I have tried using :

pkill -a -i "Google Chrome"


#!/bin/bash
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
su -l $loggedInUser -c "open -a "Google Chrome""

This closes the application successfully but the re-opening portion is not working which is the second half of this script.

Any help is appreciated.

2 replies

Forum|alt.badge.img+16
  • Valued Contributor
  • March 21, 2018

Use an osascript call.

/usr/bin/osascript<<END
tell application "Google Chrome"
open
end tell
END

Forum|alt.badge.img+7
  • Contributor
  • March 21, 2018

Hi, why do you need to run this as loggedInUser and su? I would make this:

#!/bin/bash
pkill -a -i "Google Chrome"
open -a Google Chrome

but if you want do run as su and loggedInUser then it working so:

#!/bin/bash
pkill -a -i "Google Chrome"
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
su -l $loggedInUser -c "open -a Google Chrome"

The mistakes is here, your script look like so:

su -l $loggedInUser -c "open -a "Google Chrome""

and have to be so:

su -l $loggedInUser -c "open -a Google Chrome"

or so:

su -l $loggedInUser -c "open -a 'Google Chrome'"