How to automatically shutdown Ubuntu when idle?

Solution 1:

If you are using a recent version of Ubuntu Desktop, you can configure this pretty quickly using dconf-editor. Here's how:

  1. Install dconf-editor if it is not already present:

    sudo apt update
    sudo apt install dconf-editor
    
  2. Open dconf-editor:

    Open the Editor

  3. Click the magnifying glass to start a search, and type "sleep-inactive":

    dconf-editor Search Results

  4. Edit the sleep-inactive-ac-timeout value to 1800 (30 Minutes):

    Sleep Inactive AC Timeout

  5. Edit the sleep-inactive-ac-type value to shutdown (or something else you might prefer):

    Sleep Inactive AC Type

  6. Be sure to save your changes.

That's all there is to it.

Solution 2:

matigo's answer works and is pretty easy, but if you prefer the command line, you can do the same with the following commands:

gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 1800
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'shutdown'