Recently, I realized that a certain disk partition on a Linux Server that is only accessed by ssh was full. I tracked down the problem to a VNC log which had filled up the disk. Using lsof, I determined that even though the log was deleted and VNC was restarted the disk space was not returned because various processes related to the window manager had the files opened (nautilus, gnome-ses, gnome-key, metacity, gnome-pan, eggcups, bt-applet, nm-applet, pam-panel, escd, mapping-d, gnome-ter).
The suggestion from the sysadmin is to reboot the server. Actually, the GUI has only been used once to install the packaged software that the server runs so is not required for most of the time. I would rather fix the problem without rebooting if possible.
How can the GUI be restarted or even turned off gracefully from the command line to be restarted later when required?
On most major distros, the runlevel 5 is used for GUI environments and 3 is used for text-mode. Because of this you should be able to run the following command to bring it to text-mode:
That tells the init process to head to runlevel 3. This will terminate the GUI, not terribly gracefully. To terminate it gracefully you'll have to actually connect to the GUI and log out any open sessions. However, if there is no open session and it's just sitting at the GDM login screen the above command is safe.
Changing the run-level the server boots into varies by distribution unfortunately, there is no one way to make it stick.
sudo killall X11 or the like. or killall your wm