1

I have Ubuntu server 20.04LTS running on an old optiplex. I'm trying to figure out how to shut down my server automatically after a certain idle period.

What's the best way to go about this ?

[The suggested answer it to just show how to shutdown from terminal. Not even remotely related]

Kumudu
  • 91
  • 1
    Does this answer your question? How do I shut down or reboot from a terminal?. For more info see man shutdown. – mikewhatever Jul 04 '20 at 19:03
  • That doesn't answer my question. Not even remotely – Kumudu Jul 04 '20 at 20:02
  • 1
    So let's brainstorm this. How will you define "idle"? What will be your trigger? How will you prevent false positives and keep the system from shutting down when you don't really want it too? I don't know if there are good answers to these questions, because computers don't think like people. Check out https://askubuntu.com/questions/215870/how-can-i-shutdown-my-pc-when-the-system-is-idle – Nmath Jul 04 '20 at 20:25

4 Answers4

3

So let me describe my use case a bit more and provide the answer

  • I have a server setup at home. Which is used for all kind of different things including a NAS
  • When I'm not home or not using it I don't want it running.
  • I use wake on lan to wake it up when I need it
  • When idle I want it to shut down.

Solution

  • Use systemd timers
  • Check hdd usage
  • Shutdown if not used
  1. Create the systemd service /etc/systemd/system/idle-shutdown.service
[Unit]
description=Idle shutdown service

[Service] Type=oneshot Nice=19 IOSchedulingClass=idle ExecStart=/home/kumudu/idle.sh

  1. Create the systemd timer /etc/systemd/system/idle-check.timer
[Unit]
Description=Idle check timer

[Timer]

Run hourly

OnCalendar=*:0/15 Persistent=true Unit=idle-shutdown.service

[Install] WantedBy=timers.target

  1. The idle.sh script. This initially waits for 2 hours. After that keeps checking for hard disk usage. The disk it's checking is the disk I have all the samba shares.
#!/bin/bash

RUN_LIMIT_SEC=7200

How long the machine has been idle for

RUN_TIME_SEC=$(/sbin/runuser -l kumudu -c "awk '{print $1}' /proc/uptime") echo "[idle-shutdown] Runtime is $RUN_TIME_SEC" | systemd-cat -p info; if [ ${RUN_TIME_SEC%.*} -gt $RUN_LIMIT_SEC ] ; then DISK_USAGE1=$(iostat -md -p dm-1 | awk '/dm-1/ {print $2}') sleep 10 DISK_USAGE2=$(iostat -md -p dm-1 | awk '/dm-1/ {print $2}') echo "[idle-shutdown] Disk usage is $DISK_USAGE1, $DISK_USAGE2"

 if [ ${DISK_USAGE2%.*} -eq ${DISK_USAGE1%.*} ] && [ ${DISK_USAGE2#*.} \> ${DISK_USAGE1#*.} ] || [ ${DISK_USAGE2%.*} -gt ${DISK_USAGE1%.*} ]; then
      echo "[idle-shutdown] Disk is being used, ignoring shutdown. $DISK_USAGE1, $DISK_USAGE2" | systemd-cat -p info
 else
      echo "[idle-shutdown] Disk is not being used, powering off system. $DISK_USAGE1, $DISK_USAGE2" | systemd-cat -p info
      # /bin/systemctl suspend -i
      # If you prefer to shut down instead of suspend, comment the
      # previous line and uncomment the following one:
      /sbin/poweroff
 fi

fi

  1. Enable the timer sudo systemctl enable --now idle-check.timer
Kumudu
  • 91
2

You could use dconf to shut down your server. But I don`t understand why you would want to do this. If the server is down, you'll have to start it before anyone could access it via ssh again.

But nonetheless a possible solution:

dconf write /org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-timeout 120
dconf write /org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-type "shutdown"

That means, after 2 minutes of inactivity (120 seconds) the action will be shutdown. (alternatives are suspend,hibernate,nothing...)

You should still consider the answer of WinEunuchs to implement the script he wrote about.

kanehekili
  • 6,402
1

So I got a solution for you that might work! #First you are gonna need to download the Autopoweroff package

wget https://github.com/deragon/autopoweroff/releases/download/3.0.0/autopoweroff-3.0.0-1.noarch.deb

#Then install it using

sudo dpkg -i autopoweroff-3.0.0-1.noarch.deb
sudo apt-get install -f

Then if you are using GNOME just look up autopoweroff in the activities, else go to /etc/autopoweroff/autopoweroff.conf.dpkg-new and modify the idle time as a sudo user!

If you use the file to modify the idle shutdown settings, here is what I did

[NO_SHUTDOWN_TIME_RANGE]
StartHour=
EndHour=

StartupDelay parameter (expressed in minutes):

When the computer is booting up, if all the conditions are met and

the computer is in the shutdown time range, as soon as Autopoweroff

is started, the computer will shutdown. Thus, the user will never

have the chance to boot into the computer. This is where the

"delay" parameter comes in. If "delay" is set to 15 for example,

Autopoweroff will not poweroff the computer even if all the

conditions are met, for a period of 15 minutes after the computer

has booted. This allows the user to login and change Autopoweroff's

configuration.

IdleTime parameter (expressed in minutes):

Like a screensaver, Autopoweroff detects keyboard and mouse

activity, and if there is any activity on the server, it would not

be powered off regardless if all the other conditions are met. If

set to 0, user activity on the server will be ignored.

[TIMEOUTS] StartupDelay=5 IdleTime=30

#Part of the info was obtained here https://www.ostechnix.com/auto-shutdown-reboot-suspend-hibernate-linux-system-specific-time/

mikewhatever
  • 32,638
FreddyDs
  • 92
  • 2
1

In system settings setup your server to power off after x minutes of inactivity.

Then use this script to fake server activity when anything is typed via ssh login.

Note that running a job on the ssh login doesn't constitute activity. You actually have to be typing something via ssh to fake server activity.

60, 30, 15, 10, 5, 3, 2 and 1 minute before server shutdown a warning message is broadcast to all ssh users that the server will go down unless something is typed.