But I really hope there is some better solution to this ? In my case the problem is that I have a mac in a server room that automatically runs a script in a terminal window. This machine can be rebooted remotely. But thus every time it reboots it keeps the old terminal window and opens a new one, so if I have rebooted it 10 times there will be 10 terminal windows open where only one is in use.
The command you are after is shutdown.
This informs all users that the machine is going to be shutdown and tells all apps to close files etc.
The command takes a parameter -h, -r or -s to shut down, restart or sleep the Mac.
The command has to be run as root so you need to use sudo.
e.g. to reboot the machine immediately
sudo shutdown -r now
e.g. to shutdown the machine in 60 minutes
sudo shutdown -h +60
From comments there are two things to be addressed
How shutdown works is by sending a sigterm to all processes which should then deal with that e.g. save open files etc. If they don't exit then they will get sent a SIGKILL which forces them to die with no chance to respond. The signals are not sent via the normal key message queue so Apps have to deal with this separately to the code that gets called from quit on the menu. A good app should call common code from both.
This other answer shows how to shutdown as if you hit the menu options. But note that apps can cancel this shutdown
Best Answer
You can just enter the following command into Terminal:
The 168h refers to 168 hours, which I've used so that the command goes back a week (since you want to capture the weekend).
However, you can change the
168
to another value if you like.