With the release of macOS 10.13.4, Apple has added a new option to the softwareupdate command line tool. As part of the installation options, softwareupdate now includes the option to automatically restart or shutdown (as appropriate) following the installation of updates that need a restart or shutdown to complete properly.
-R | --restart Automatically restart (or shut down) if required to complete installation.
As an example, to use the new option to restart if needed after installing all available updates, please run the command shown below with root privileges:
softwareupdate --install --all --restart
In the event that no updates require a restart or shutdown, the Mac is not restarted.