Close and Re-open Script

nnichson
New Contributor

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 2

Look
Valued Contributor III

Use an osascript call.

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

ICTMuttenz
Contributor

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'"