How to prevent Linux Mint going to sleep while Samba sharing is active?

0

My desktop is configured to go to sleep after 30 minutes of inactivity. I'm sharing movies from it's hard drive to home network with Samba. Problem is that it suspends after 30 minutes although I'm streaming a movie on my home network. Apparently, Samba sharing is not registered as an activity. How can I make it see that Samba share in in progress and postpone suspend? OS is Linux Mint.

Sinisa

Posted 2019-11-07T07:51:06.533

Reputation: 1

Answers

1

Caffeine is a utility available in the package manager that serves a similar function as the scripts. It periodically injects meaningless keyboard activity so the system doesn't appear as idle. Caffeine sticks an icon in the system tray that you can toggle on and off, which is convenient. The publisher's description refers to it working when in full-screen mode. When I last used it, I don't think it required full-screen mode. But worst case, make the application full-screen in that workspace.

fixer1234

Posted 2019-11-07T07:51:06.533

Reputation: 24 254

0

The article Ubuntu – Prevent sleep while sharing files over samba proposes a script that will prevent Ubuntu from sleeping while sharing files, using the smbstatus command:

#!/usr/bin/env bash

# sleep prevent command - "gnome-screensaver-command --poke" resets idle timer
PREVENTSLEEP='gnome-screensaver-command --poke'

# gnome-screensaver-command uses X11 which expects this ENV to be set
export DISPLAY=:0.0

smbstatus | grep -q "DENY_WRITE"
if [ $? == 0 ]; then $PREVENTSLEEP ; fi

The article proposes using crontab for the periodic execution of the script.

harrymc

Posted 2019-11-07T07:51:06.533

Reputation: 306 093

I'm trying that but it doesn't quite work. I don't have 'gnome-screensaver-command --poke' on Linux Mint. So I tried to modify it a little but I'm quite a noob for this:

#!/usr/bin/env bash

sleep prevent command - "gnome-screensaver-command --poke" resets idle timer

PREVENTSLEEP= xdotool key a

gnome-screensaver-command uses X11 which expects this ENV to be set

export DISPLAY=:0.0

DO NOT EDIT

smbstatus | grep -q "DENY_NONE" if [ $? == 0 ]; then $PREVENTSLEEP ; fi

This doesn't work as expected. No matter what smbstatus is it always runs xdotool command. – Sinisa – 2019-11-07T09:34:21.597

0

I did it! I'll explain how, maybe it will help others.

Install xdotool

sudo apt install xdotool

Then, make a script in /usr/local/bin/

sudo xed /usr/local/bin/checksmb.sh

Copy this to xed:

#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/sinisab89/.Xauthority 

if [ `sudo smbstatus | grep DENY | wc -l` != 0 ]
then
xdotool key F9
exit 0
fi

Save file.

DISPLAY and XAUTHORITY is needed for CRON to function properly. smbstatus only runs with sudo. I chose F9 key, maybe there are better options. Script reads smbstatus and if there are more than 0 "DENY" entries in the report (active connections in Samba) simulates F9 key press which resets system idle timer and prevents suspend. If there are 0 "DENY" entries script does nothing.

Make checksmb.sh executable

sudo chmod +x /usr/local/bin/checksmb.sh

Now set up CRON to execute script every x minutes. I chose 20 minutes.

sudo crontab -e

Scroll to the bottom and add

*/20 * * * * /usr/local/bin/checksmb.sh >> /home/YOUR USERNAME/cron.log 2>&1

/home/YOUR USERNAME/cron.log 2>&1 makes a cron.log file in your home folder. You can use it for troubleshooting if the script is not executed properly.

With this command you can see if CRON is executing script at given time

less /var/log/syslog | grep checksmb

This is not the most elegant solution but it works. If someone knows how, for example, video players prevent system from sleeping it would be nice to modify this script so it doesn't use xdotool. There has to be some system suspend inhibitor but I just couldn't find anything online.

I think Caffeine requires user input and doesn't run automatically. Sry I'm replying as this but it doesn't allow me to comment...

Sinisa Brankovic

Posted 2019-11-07T07:51:06.533

Reputation: 1