languitar / autosuspend

A daemon to automatically suspend and wake up a system
https://autosuspend.readthedocs.io
GNU General Public License v2.0
75 stars 15 forks source link

Ubuntu 22.04 - LocalUsers check not working #281

Closed madbrain76 closed 1 year ago

madbrain76 commented 1 year ago

I have autosuspend installed, including the default check for LocalUsers in /etc/autosuspend.conf, along with some other checks.

root@server10g:/etc# cat autosuspend.conf
## This is an exemplary documentation file that mainly serves as a syntax explanation.
## For a list of available options and checks, please refer to `man autosuspend.conf` or the online documentation.

[general]
interval = 30
idle_time = 900
suspend_cmd = /bin/systemctl suspend
wakeup_cmd = sh -c 'echo 0 > /sys/class/rtc/rtc0/wakealarm && echo {timestamp:.0f} > /sys/class/rtc/rtc0/wakealarm'
woke_up_file = /var/run/autosuspend-just-woke-up
lock_file = /var/lock/autosuspend.lock
lock_timeout = 30
# Can be used to call a command before suspending, either with scheduled wake up or not.
# notify_cmd_wakeup = su myuser -c notify-send -a autosuspend 'Suspending the system. Wake up at {iso}'
# notify_cmd_no_wakeup = su myuser -c notify-send -a autosuspend 'Suspending the system.'

# Basic activity check configuration.
# The check class name is derived from the section header (Ping in this case).
# Remember to enable desired checks. They are disabled by default.
[check.Ping]
enabled = false
hosts = 192.168.0.7

[check.NetworkBandwidth]
enabled = true
#interfaces = net1
interfaces = enp3s0
threshold_receive=600000
threshold_send=600000

# This check is disabled.
[check.Smb]
enabled = false

# Example for a custom check name.
# This will use the Users check with the custom name RemoteUsers.
# Custom names are necessary in case a check class is used multiple times.
# Custom names can also be used for clarification.
[check.RemoteUsers]
class = Users
enabled = false
name = .*
terminal = .*
host = [0-9].*

# Here the Users activity check is used again with different settings and a different name
[check.LocalUsers]
class = Users
enabled = true
name = .*
terminal = .*
host = localhost

# check for RaspiBackup
[check.Processes]
enabled=true
processes=raspiBackup.sh

# Check for ongoing ZFS scrub
[check.ExternalCommand]
enabled=true
command=/scripts/iszfsscrubbing.sh

# Checks to determine the next scheduled wakeup are prefixed with 'wakeup'.
[wakeup.Calendar]
enabled = false
url = http://example.org/test.ics

# Apart from this, wake up checks reuse the same configuration mechanism.

Unfortunately, when I'm logged in locally to Gnome, this check does not seem to work. The system goes to sleep while I'm using it, which is very inconvenient.

The other checks I'm using seem to work fine, and prevent autosuspend from suspending as I expected. This check used to work in the past in Ubuntu 20.04 but not anymore, unfortunately.

madbrain76 commented 1 year ago

Additional info :+1: Repeatedly typing "systemctl status autosuspend" shows the following . Note how the "since idle" time does not change .

root@server10g:/etc# systemctl status autosuspend
● autosuspend.service - A daemon to suspend your server in case of inactivity
     Loaded: loaded (/lib/systemd/system/autosuspend.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-12-02 02:08:00 PST; 23h ago
       Docs: https://autosuspend.readthedocs.io/en/latest/systemd_integration.html
   Main PID: 463831 (autosuspend)
      Tasks: 1 (limit: 38368)
     Memory: 12.3M
     CGroup: /system.slice/autosuspend.service
             └─463831 /usr/bin/python3 /usr/bin/autosuspend -l /etc/autosuspend-logging.conf daemon

Dec 03 01:55:55 server10g autosuspend[463831]: 2022-12-03 01:55:55,640 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,667 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,702 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,702 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,715 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,750 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,750 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,781 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,813 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,813 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
root@server10g:/etc# systemctl status autosuspend
● autosuspend.service - A daemon to suspend your server in case of inactivity
     Loaded: loaded (/lib/systemd/system/autosuspend.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-12-02 02:08:00 PST; 23h ago
       Docs: https://autosuspend.readthedocs.io/en/latest/systemd_integration.html
   Main PID: 463831 (autosuspend)
      Tasks: 1 (limit: 38368)
     Memory: 12.3M
     CGroup: /system.slice/autosuspend.service
             └─463831 /usr/bin/python3 /usr/bin/autosuspend -l /etc/autosuspend-logging.conf daemon

Dec 03 01:55:55 server10g autosuspend[463831]: 2022-12-03 01:55:55,640 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,667 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,702 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:56:25 server10g autosuspend[463831]: 2022-12-03 01:56:25,702 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,715 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,750 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:56:55 server10g autosuspend[463831]: 2022-12-03 01:56:55,750 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,781 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,813 - autosuspend.Processor - INFO - System is idle since 2022-12-03 09:43:53.687480+00:00
Dec 03 01:57:25 server10g autosuspend[463831]: 2022-12-03 01:57:25,813 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.

Right as I was typing this post in Firefox, autosuspend suspended the machine. I had to wake it up with WOL. Most annoying. I suppose I could write a custom checker that calls the "who" command to work around this problem, but it seems like this should is a basic use case that should work.

madbrain76 commented 1 year ago

On a separate note, it's pretty confusing to show the "time since" in UTC format rather than the local time zone. Certainly makes it non-obvious when looking at "systemctl status" output.

Also,the message "Desired idle time of 900.0 s not reached yet" could be more detailed, for instance by displaying the "currently idle for" time, which I would have expected to be zero or close to it when I issued the systemctl status command, and would have made it obvious that it was incrementing without being reset by my user activity.

languitar commented 1 year ago

Can you run the following on a terminal to find out which users are reported on your system in a situation where the system shouldn't suspend:

 python3 -c "import psutil; print(psutil.users())"
madbrain76 commented 1 year ago

Here is some more output including what you requested :

root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal=':1', host=':1', started=1670105600.0, pid=5650), suser(name='madbrain', terminal='pts/1', host='', started=1670105728.0, pid=7432)]
root@server10g:/home/madbrain/Desktop# systemctl status autosuspend
● autosuspend.service - A daemon to suspend your server in case of inactivity
     Loaded: loaded (/lib/systemd/system/autosuspend.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-12-03 14:13:13 PST; 2min 34s ago
       Docs: https://autosuspend.readthedocs.io/en/latest/systemd_integration.html
   Main PID: 3784 (autosuspend)
      Tasks: 1 (limit: 38368)
     Memory: 14.5M
     CGroup: /system.slice/autosuspend.service
             └─3784 /usr/bin/python3 /usr/bin/autosuspend -l /etc/autosuspend-logging.conf daemon

Dec 03 14:14:13 server10g autosuspend[3784]: 2022-12-03 14:14:13,489 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 14:14:43 server10g autosuspend[3784]: 2022-12-03 14:14:43,518 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 14:14:43 server10g autosuspend[3784]: 2022-12-03 14:14:43,555 - autosuspend.Processor - INFO - System is idle since 2022-12-03 22:13:14.083844+00:00
Dec 03 14:14:43 server10g autosuspend[3784]: 2022-12-03 14:14:43,556 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 14:15:13 server10g autosuspend[3784]: 2022-12-03 14:15:13,586 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 14:15:13 server10g autosuspend[3784]: 2022-12-03 14:15:13,617 - autosuspend.Processor - INFO - System is idle since 2022-12-03 22:13:14.083844+00:00
Dec 03 14:15:13 server10g autosuspend[3784]: 2022-12-03 14:15:13,617 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
Dec 03 14:15:43 server10g autosuspend[3784]: 2022-12-03 14:15:43,646 - autosuspend.Processor - INFO - Starting new check iteration
Dec 03 14:15:43 server10g autosuspend[3784]: 2022-12-03 14:15:43,712 - autosuspend.Processor - INFO - System is idle since 2022-12-03 22:13:14.083844+00:00
Dec 03 14:15:43 server10g autosuspend[3784]: 2022-12-03 14:15:43,712 - autosuspend.Processor - INFO - Desired idle time of 900.0 s not reached yet.
root@server10g:/home/madbrain/Desktop# systemctl status autosuspend^C
root@server10g:/home/madbrain/Desktop# root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal=':1', host=':1', started=1670105600.0, pid=5650), suser(name='madbrain', terminal='pts/1', host='', started=1670105728.0, pid=7432)]
^C
root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal=':1', host=':1', started=1670105600.0, pid=5650), suser(name='madbrain', terminal='pts/1', host='', started=1670105728.0, pid=7432)]
root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal=':1', host=':1', started=1670105600.0, pid=5650), suser(name='madbrain', terminal='pts/1', host='', started=1670105728.0, pid=7432)]
root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal=':1', host=':1', started=1670105600.0, pid=5650), suser(name='madbrain', terminal='pts/1', host='', started=1670105728.0, pid=7432)]
root@server10g:/home/madbrain/Desktop# who
madbrain :1           2022-12-03 14:13 (:1)
madbrain pts/1        2022-12-03 14:14
root@server10g:/home/madbrain/Desktop# uptime
 14:16:13 up 3 min,  2 users,  load average: 0.33, 0.50, 0.24

Note that I do have 2 X sessions the main Gnome one, and another running specifically for VNC with TigerVNC .

root@server10g:/etc/systemd/system# cat vncserver\@.service 
[Unit]
Description=Tiger VNC server
After=syslog.target network.target

[Service]
Type=forking
User=madbrain
PIDFile=/home/madbrain/.vnc/%H:590%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -geometry 3840x2160 -localhost no :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

root@server10g:/etc/systemd/system# ps -ef | grep -i vnc
madbrain    3964       1  0 14:13 ?        00:00:00 /usr/bin/perl /usr/bin/vncserver -geometry 3840x2160 -localhost no :2
madbrain    3965    3964  0 14:13 ?        00:00:00 /usr/bin/Xtigervnc :2 -localhost=0 -desktop server10g:2 (madbrain) -rfbport 5902 -PasswordFile /home/madbrain/.vnc/passwd -SecurityTypes VncAuth,TLSVnc -auth /home/madbrain/.Xauthority -geometry 3840x2160 -depth 24
madbrain    3966    3964  0 14:13 ?        00:00:00 /bin/sh /home/madbrain/.vnc/xstartup
root        7545    7434  0 14:18 pts/1    00:00:00 grep --color=auto -i vnc
root@server10g:/etc/systemd/system# 
languitar commented 1 year ago

The logged in users are either on host ::1 or on empty string. Therefore, host = localhost doesn't match and all users are ignored by the check. You have to updated the host value to match either ::1 or empty string for the local users. I can add more debug logging later and update the documentation to make configuring this check easier.

madbrain76 commented 1 year ago

I tried host = empty string. Unfortunately, the check this matches when I am logged in from VNC only, and not logged in locally :

root@server10g:/home/madbrain/Desktop# who
madbrain pts/0        2022-12-05 14:24
root@server10g:/home/madbrain/Desktop#  python3 -c "import psutil; print(psutil.users())"
[suser(name='madbrain', terminal='pts/0', host='', started=1670279040.0, pid=1247707)]
root@server10g:/home/madbrain/Desktop# systemctl status autosuspend
? autosuspend.service - A daemon to suspend your server in case of inactivity
     Loaded: loaded (/lib/systemd/system/autosuspend.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-12-05 14:24:16 PST; 2min 33s ago
       Docs: https://autosuspend.readthedocs.io/en/latest/systemd_integration.html
   Main PID: 1246773 (autosuspend)
      Tasks: 1 (limit: 38368)
     Memory: 11.9M
     CGroup: /system.slice/autosuspend.service
             ??1246773 /usr/bin/python3 /usr/bin/autosuspend -l /etc/autosuspend-logging.conf daemon

Dec 05 14:25:16 server10g autosuspend[1246773]: 2022-12-05 14:25:16,311 - autosuspend.Processor - INFO - System is active. Resetting state
Dec 05 14:25:46 server10g autosuspend[1246773]: 2022-12-05 14:25:46,337 - autosuspend.Processor - INFO - Starting new check iteration
Dec 05 14:25:46 server10g autosuspend[1246773]: 2022-12-05 14:25:46,338 - autosuspend.Processor - INFO - Check LocalUsers matched. Reason: User madbrain is logged in on terminal pts/0 from  since 167027904>
Dec 05 14:25:46 server10g autosuspend[1246773]: 2022-12-05 14:25:46,338 - autosuspend.Processor - INFO - System is active. Resetting state
Dec 05 14:26:16 server10g autosuspend[1246773]: 2022-12-05 14:26:16,368 - autosuspend.Processor - INFO - Starting new check iteration
Dec 05 14:26:16 server10g autosuspend[1246773]: 2022-12-05 14:26:16,369 - autosuspend.Processor - INFO - Check LocalUsers matched. Reason: User madbrain is logged in on terminal pts/0 from  since 167027904>
Dec 05 14:26:16 server10g autosuspend[1246773]: 2022-12-05 14:26:16,369 - autosuspend.Processor - INFO - System is active. Resetting state
Dec 05 14:26:46 server10g autosuspend[1246773]: 2022-12-05 14:26:46,395 - autosuspend.Processor - INFO - Starting new check iteration
Dec 05 14:26:46 server10g autosuspend[1246773]: 2022-12-05 14:26:46,396 - autosuspend.Processor - INFO - Check LocalUsers matched. Reason: User madbrain is logged in on terminal pts/0 from  since 167027904>
Dec 05 14:26:46 server10g autosuspend[1246773]: 2022-12-05 14:26:46,396 - autosuspend.Processor - INFO - System is active. Resetting state
root@server10g:/home/madbrain/Desktop# 

How can I make the check match only if I'm logged in locally and not remotely through VNC ? Edit: I made it host = :1 , and it worked. I feel this should not have been required, though.

languitar commented 1 year ago

If you want to match only an empty string with a regular expression, then you can use ^$. If you just use the empty string itself as the value for the host config option, any user will match because the empty string is contained in every other string.

languitar commented 1 year ago

I've improved the log message and added more documentation on the configuration of the users check for others trying to achieve the same things. What I am not going to change is using UTC for date-time objects. Local time zones with DST changes and so on are also confusing.

Please reopen if anything else remains.