Is there a code to run in the terminal that I can use to close all applications and restart the mac? I am currently using "osascript -e 'tell app \"System Events\" to restart" to cause a restart, but it isn't closing the applications. - I need for the code to all be in one command, so I can put it in my code without one canceling out the other.
shutdown will close the apps automatically for you (they may not save their working files, however):
$ sudo shutdown -s now
$ sudo shutdown -r now