You are not logged in.

#1 2014-02-08 00:06:58

mash
Member
Registered: 2012-12-02
Posts: 15

[Solved] Prevent systemd suspend while samba is serving files

I configured logind to susped the computer when it's idle for some time. That works reasonable well when i'm working with it, but file serving with samba is not detected as activity. The man page of logind service says it makes use of the idle status that is reported by the user session. What does that mean? Is there a way to reset that idle status?

/etc/systemd/logind.conf:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# See logind.conf(5) for details

[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
HandlePowerKey=suspend
#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate
#HandleLidSwitch=suspend
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
IdleAction=suspend
IdleActionSec=15min

Last edited by mash (2014-02-10 22:07:13)

Offline

#2 2014-02-10 12:02:32

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

The idle status is probably passed by some sort of dbus mechanism. I saw a button for this in transmission. I don't think that samba has this feature (yet).

What you could do is creating a new service in /etc/systemd/system which checks for Samba activity and fails if there is:

[Unit]
Description=Inhibit suspend in case of samba activity
Before=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
RequiredBy=sleep.target

I quickly adapted this with one of my own scripts. You might need to tweak it before it will even work.

EDIT: If you call it /etc/systemd/system/dontkillsamba.service , then you have to do:

systemctl enable dontkillsamba.service

for it to work.

Last edited by Rexilion (2014-02-10 12:03:43)


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#3 2014-02-10 22:06:47

mash
Member
Registered: 2012-12-02
Posts: 15

Re: [Solved] Prevent systemd suspend while samba is serving files

Thank you so much, that helped a lot! Didn't notice that it is that easy to hook into these targets. Cool thing.. smile

Again Thanks.. will save me some money.. wink

Last edited by mash (2014-02-11 09:49:06)

Offline

#4 2014-02-11 08:06:45

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

Really, that worked? nice! big_smile . Did you have to adapt the above script to make it work or did it just work as is?


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#5 2014-02-11 09:51:20

mash
Member
Registered: 2012-12-02
Posts: 15

Re: [Solved] Prevent systemd suspend while samba is serving files

Nope.. I just did some ajustment to grep because i like the idea better to grep for my internal ip range.. but that was all..

with Wake on Lan to remotely power the maschine up and this suspend thingy it's all i needed. smile

Offline

#6 2014-11-01 02:56:10

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

Hi, i'm using my arch pc as a file server for my xbmc FireTv, and it's really annoying that my pc goes to sleep after "x" time
so i tried the "dontkillsamba" service but it gives me this error

This is the systemctl status

dontkillsamba.service - Inhibit suspend in case of samba activity
   Loaded: loaded (/etc/systemd/system/dontkillsamba.service; enabled)
   Active: failed (Result: exit-code) since vie 2014-10-31 22:34:12 ART; 6min ago
  Process: 6037 ExecStart=/usr/bin/sh -c ! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*' (code=exited, status=1/FAILURE)
Main PID: 6037 (code=exited, status=1/FAILURE)

oct 31 22:34:12 A10 systemd[1]: dontkillsamba.service: main process exited, code=exited, status=1/FAILURE
oct 31 22:34:12 A10 systemd[1]: Failed to start Inhibit suspend in case of samba activity.
oct 31 22:34:12 A10 systemd[1]: Unit dontkillsamba.service entered failed state.

my pc doesn't go to sleep but the conection to the file server gets lost for a bit.

Any Ideas?

Offline

#7 2014-11-01 06:33:44

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

Did you try the command from the commandline?


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#8 2014-11-01 07:06:44

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

Rexilion wrote:

Did you try the command from the commandline?

Rexilion Thanks for the reply!

I tried from diferent ways. this is the result (i was streaming from xbmc via samba when i did this)

[sa@A10 ~]$ sudo smbstatus -nb | tail -n +5 | grep  '^[0-9]*'
1078      1000          100           10.0.0.101   (ipv4:10.0.0.101:45883)
1077      1000          100           10.0.0.101   (ipv4:10.0.0.101:45882)

here without quiet grep
[sa@A10 ~]$ sudo /usr/bin/sh -c ! smbstatus -nb | tail -n +5 | grep '^[0-9]*'
[sa@A10 ~]$

as in dontkillsamba.service
[sa@A10 ~]$ sudo /usr/bin/sh -c ! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'
[sa@A10 ~]$

without (!) ?
[sa@A10 ~]$ sudo /usr/bin/sh -c  smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'
[sa@A10 ~]$

Offline

#9 2014-11-01 09:46:01

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

The failing is intended. The service fails if samba is busy -> no s2disk.

If it succeeds, then samba is idle -> exec s2disk.

The disruption is caused by systemd restarting samba??? (I don't know).

Baseline: It's working as intended.


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#10 2014-11-01 13:16:35

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

Yes, i think that systemd restarts samba, but i dont know how to prevent that.
i think this is the cleanest way to do this,

the other option will be a cronjob that makes a keypress or something to prevent the suspension if samba is busy....

something like

if smbstatus -nb | tail -n +5 | grep '^[0-9]*' (returns not null)
then
(keypress)
else
(nothing)

but im having trouble with that too.

Any ideas how to do that?
Thanks

Offline

#11 2014-11-02 14:29:59

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

Please note that our magic command is not providing it's intel by /dev/stdout or /dev/stderr output.

Systemd looks at the return code of the last command. Read bash for pipeline and return code semantics.

Also note the exclamation mark at the beginning of the command, it inverts the return code. Do not forget that.


As for your request. This is why I don't like systemd, it does too much. We do not know what's going on 'under the hood'. But for now, you could try to move the .service file earlier up the suspend chain. Something like:

[Unit]
Description=Inhibit suspend in case of samba activity
Before=hibernate.target
Before=hybrid-sleep.target
Before=suspend.target
Before=smbd.service # who knows?

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
WantedBy=suspend.target

Last edited by Rexilion (2014-11-02 14:30:19)


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#12 2014-11-02 19:33:01

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

Rexilion wrote:

Please note that our magic command is not providing it's intel by /dev/stdout or /dev/stderr output.

Systemd looks at the return code of the last command. Read bash for pipeline and return code semantics.

Also note the exclamation mark at the beginning of the command, it inverts the return code. Do not forget that.


As for your request. This is why I don't like systemd, it does too much. We do not know what's going on 'under the hood'. But for now, you could try to move the .service file earlier up the suspend chain. Something like:

[Unit]
Description=Inhibit suspend in case of samba activity
Before=hibernate.target
Before=hybrid-sleep.target
Before=suspend.target
Before=smbd.service # who knows?

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
WantedBy=suspend.target

This .service suspends my pc in the set time. so the other one is better



I tried this one too but still no luck. (pc powerd on but network fails)
also tried to set bigger buffer to xbmc but the problem persist (network conection lost)

[Unit]
Description=Inhibit suspend in case of samba activity
Before=sleep.target
Before=smbd.target
Before=nmbd.target
Before=dbus-org.freedesktop.nm-dispatcher.target
Before=dbus-org.freedesktop.NetworkManager.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
RequiredBy=sleep.target

Offline

#13 2014-11-04 18:38:52

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [Solved] Prevent systemd suspend while samba is serving files

[Unit]
Description=Inhibit suspend in case of samba activity
Before=hibernate.target
Before=hybrid-sleep.target
Before=suspend.target
Before=smbd.service # who knows?

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
RequiredBy=hibernate.target
RequiredBy=hybrid-sleep.target
RequiredBy=suspend.target

Perhaps a combo of both?


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#14 2014-11-08 04:32:07

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

Rexilion wrote:
[Unit]
Description=Inhibit suspend in case of samba activity
Before=hibernate.target
Before=hybrid-sleep.target
Before=suspend.target
Before=smbd.service # who knows?

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c "! smbstatus -nb | tail -n +5 | grep -q '^[0-9]*'"

[Install]
RequiredBy=hibernate.target
RequiredBy=hybrid-sleep.target
RequiredBy=suspend.target

Perhaps a combo of both?

still gets suspended neutral
i'm under gnome shell but i'm going to change DE to kde soon... kde has a LOT of power options maybe with it i'll solve this. ¿?
Thanks for the reply!

Offline

#15 2014-11-11 18:11:33

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

i'm on KDE now and the error persist changing every energy option available... BUT... i get a status message that notfies that's not samba stoping. it's my ethernet port that's down and up in less than 5 seconds. but it's enough for xbmc to stop working after the buffer consumes.
Any ideas how to prevent systemd to stop my ethernet conection before suspension?

Offline

#16 2014-11-11 19:32:18

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

Re: [Solved] Prevent systemd suspend while samba is serving files

i'm not into this issue at all, but the comment on the same line in systemd "Type" field  makes it just ignores the whole line, so i'd write this instead,(just in case):

# who knows?
Before=smbd.service 

Last edited by kokoko3k (2014-11-11 19:33:29)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#17 2014-11-12 00:03:45

nunigb
Member
Registered: 2014-11-01
Posts: 7

Re: [Solved] Prevent systemd suspend while samba is serving files

kokoko3k wrote:

i'm not into this issue at all, but the comment on the same line in systemd "Type" field  makes it just ignores the whole line, so i'd write this instead,(just in case):

# who knows?
Before=smbd.service 

Yes,  I noticed that.
also i tried adding the Before=systemd-networkd.target and RequiredBy=systemd-networkd.target
but no luck

Any Ideas?

Offline

#18 2018-07-13 07:46:17

mt7479
Member
Registered: 2018-07-12
Posts: 1

Re: [Solved] Prevent systemd suspend while samba is serving files

I'm a bit late to the party, but since this still is one of the first returned results when searching for this issue id like to share my solutions.

dbus SimulateUserActivity

This will simulate user activity and reset the suspend timeout, you can invoke this method with:

qdbus org.kde.screensaver /ScreenSaver SimulateUserActivity
qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
dbus-send --print-reply=literal --dest="org.freedesktop.ScreenSaver" /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity

Different DEs implement different namespaces, the examples above works on Debian 9 with KDE. I suspect the freedesktop one to be the generic one.
The Gnome specific one is:

qdbus org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity

Example python script that will connect to dbus:

from dbus import SessionBus
foo = True

if foo:
    session = SessionBus()
    screensaver = session.get_object('org.freedesktop.ScreenSaver', '/org/freedesktop/ScreenSaver')
    screensaver.SimulateUserActivity()

Unfortunately this doesn't work for Ubuntu 18.04, both freedesktop and gnome namespaces are present but the freedesktop one is not implemented and the gnome one doesn't provide SimulateUserActivity.

qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.SimulateUserActivity
Error: org.freedesktop.DBus.Error.NotSupported
This method is not implemented

systemd-inhibit

This will create a inhibit lock that will persist as long as the script is running. A custom script would need to perform a loop until the condition is resolved, exit, and ultimately remove the lock.

systemd-inhibit --what=sleep --who="me" --why="because i can" --mode=block command

Scheduling could be done with systemd-run. This will create a systemd timer and a unit file that is executed every 15 minutes:

systemd-run --user --on-calendar="*:0/15" systemd-inhibit --what=sleep --who="me" --why="because i can" --mode=block command

Systemd will only start a new instance if the previous one has exited.

Timers created with systemd-run will not survive a reboot, you would have to recreate then with a startup script or write your own unit/timer combo.

Offline

#19 2018-07-13 08:28:16

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,427

Re: [Solved] Prevent systemd suspend while samba is serving files

Thanks for the relevant and informative contribution, however as it is an old thread I'm going to close it now.

If you want to archive this more visibly, you might want to contribute this info on the wiki.

Offline

Board footer

Powered by FluxBB