Close safari automatically
One of the Apple-supported ways to do it is the following AppleScript command:

tell app "Safari" to quit


Use the following one-liner to run the same thing from your shell:

osascript -e 'tell app "Safari" to quit'


Or, use the bundle ID if that’s more your thing:

osascript -e 'tell app id "com.apple.Safari" to quit'