The officially official Devuan Forum!

You are not logged in.

#1 2021-09-04 15:00:43

xsR_head
Member
Registered: 2021-08-24
Posts: 13  

[SOLVED] How to run a script on suspend/resume?

Hello, I want to run a script on suspend and resume but all the guides I find, refer me to systemd stuff (I have SysVinit). Also there are some guides referring to pm-utils, however I don’t want to install pm-utils since then I will have to write a script so my laptop suspends on lid close with pm-utils instead of whatever it now uses (which would be basically the same topic I posted). Then I would have to install smth like xss-lock so it suspends after user inactivity etc etc etc.
[SOLVED] Here is manual for elogind (look for "Hook directories"): https://manpages.debian.org/bullseye/el … .1.en.html [SOLVED]

Last edited by xsR_head (2021-09-05 00:14:09)

Offline

#2 2021-09-04 19:35:11

Marjorie
Member
From: Teignmouth, UK
Registered: 2019-06-09
Posts: 221  

Re: [SOLVED] How to run a script on suspend/resume?

Yes, if you are using sysvinit then I'd anticipate that you can use pm-utils to suspend and hibernate . Which is what I use.

Installing the pm-utils package installs both binaries and the scripts that are run when you put your machine to sleep/wake it up again.

As root/sudo you can run pm-suspend, pm-hibernate or pm-suspend-hybrid.

I've added the names of the first two of these to my sudoers file so that I can run them from a power menu button on my cinnamon desktop.

The pm-utils configuration scripts are in /etc/pm/ and /usr/lib/pm-utils.

In the former you will have the /etc/pm/config.d/defaults script that (inter-alia) sets the hibernate mode.

This is mine:

##########################################################
# DO NOT EDIT THE FILE in /usr/lib/pm-utils/             #
#                                                        #
# Edit this in /etc/pm/config.d/ instead!                #
##########################################################

# Default method to power down the system when hibernating.
# If commented out, the system will use the kernel default as a 
# default value.
#
# Check /sys/power/disk for valid values.  The default value
# will be surrounded by [square brackets].
HIBERNATE_MODE="platform"

# Whether we need to post the video card when resuming from
# hibernate. You should not normally need to set this.  
# In the future pm-utils will get this setting from HAL.
#
# Valid values are "no" and "yes"
# HIBERNATE_RESUME_POST_VIDEO="no"

# The default sleep/wake system to use.  Valid values are:
#   kernel    The built-in kernel suspend/resume support.
#             Use this if nothing else is supported on your system.
#   uswsusp   If your system has support for the userspace
#             suspend programs (s2ram/s2disk/s2both), then use this.
#   tuxonice  If your system has support for tuxonice, use this.
#
# The system defaults to "kernel" if this is commented out.
SLEEP_MODULE="kernel"

# These variables will be handled specially when we load files in 
# /etc/pm/config.d.
# Multiple declarations of these environment variables will result in 
# their contents being concatenated instead of being overwritten.

# If you need to unload any modules to suspend/resume, add them here.
SUSPEND_MODULES="ath9k ath9k_common ath9k_hw ath"

# If you want to keep hooks from running, add their names here.
# HOOK_BLACKLIST=""

# If you want to unconditionally add parameters to the commandline,
# add them here.
# ADD_PARAMETERS=""

# If you want to ignore commandline parameters, add them here.
# DROP_PARAMETERS=""

# If you need to synchronize the system clock across a suspend/resume or
# hibernate/thaw cycle, set this variable.
# NEED_CLOCK_SYNC="true"

My atheros network card refuses to go to sleep so I unload the relevant modules and reload then on waking, hence the SUSPEND_MODULES= line.

I aso have supplementary scripts:

etc/pm/sleep.d/10_unattended-upgrades-hibernate that stops hibernation until unattended upgrades has finished.

and

etc/pm/sleep.d/password-protect-waking that calls a locked screensaver on waking.

In the latter you will find the default scripts that are run when you put your computer to sleep and are then run in reverse when you wake it up. You can also add your own.

I  have an additional script  /usr/lib/pm-utils/sleep.d/95anacron that start and stops anacron when I sleep the computer. The purpose of this is to defer scripts run by anacron until the computer is awake (regular cron scripts simply don't run if the computer is asleep).

#!/bin/sh

# This script makes anacron jobs stop/start when a workstation
# enters/exits a suspended/hibernated state.
# 

case $1 in
    resume|thaw)
	/usr/sbin/invoke-rc.d anacron start >/dev/null   
	;;
    suspend|hibernate)
	/usr/sbin/invoke-rc.d anacron stop >/dev/null   
	;;
esac

and  /usr/lib/pm-utils/sleep.d/wpa-supplicant that notifies wpa-supplicant of pm actions.

#!/bin/sh

# /etc/pm/sleep.d/60_wpa_supplicant
# Action script to notify wpa_supplicant of pm-action events.

PATH=/sbin:/usr/sbin:/bin:/usr/bin

WPACLI=wpa_cli

case "$1" in
	suspend|hibernate)
		$WPACLI suspend
		;;
	resume|thaw)
		$WPACLI resume
		;;
esac

exit 0

Last edited by Marjorie (2021-09-04 20:15:45)

Offline

#3 2021-09-04 20:14:26

Marjorie
Member
From: Teignmouth, UK
Registered: 2019-06-09
Posts: 221  

Re: [SOLVED] How to run a script on suspend/resume?

Just to add.

In order to get hibernation resume to work you'll need to tell the OS where the swap partition is that you will use to store/restore your resume image on.

Edit /etc/default'grub/ to add or amend the line: 
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=UUID=1e9fb381-0966-401f-b649-7958a6df7307"
to name the actual UUID of your swap partition. Obviously the swap partition will have to be large enough to store your ram image.

Then run (as root/sudo) update-grub to update /boot/grub/grub.cfg and reboot.

Offline

#4 2021-09-04 21:31:37

xsR_head
Member
Registered: 2021-08-24
Posts: 13  

Re: [SOLVED] How to run a script on suspend/resume?

Thanks for reply and sorry for being unclear; I dont want to install pm-utils since then I will have to write a script so my laptop suspends on lid close with pm-utils instead of whatever it now uses (which would be basically the same topic I posted). Then I would have to install smth like xss-lock so it suspends after user inactivity etc etc etc.

Added clarification to the post

Last edited by xsR_head (2021-09-04 21:39:11)

Offline

#5 2021-09-04 22:56:34

Marjorie
Member
From: Teignmouth, UK
Registered: 2019-06-09
Posts: 221  

Re: [SOLVED] How to run a script on suspend/resume?

Your issue seems to relate to runing scripts that capture the lid open/closing event and running an appropriate command.

This has to happen before you call your suspend/hibernate/resume command and therefore related to acpi/acpid and acpi scripts not pm-utils and its scripts which obviously only run once suspend/resume have been called.

I don't have a laptop myself so I have no personal experience of how to do this.

However there is some discussion (and further links) here that might be able to help:

https://askubuntu.com/questions/517683/ … ect=1&lq=1

Ubuntu 14 was the last sysvinit version of Ubuntu before they went over to the systemd darkside.

Offline

#6 2021-09-04 23:36:38

xsR_head
Member
Registered: 2021-08-24
Posts: 13  

Re: [SOLVED] How to run a script on suspend/resume?

Aight, I searched through gentoo forums. Basically you need to place a script in

/lib/elogind/system-sleep/

then

chmod +x

your script.

Offline

#7 2021-09-05 00:10:47

xsR_head
Member
Registered: 2021-08-24
Posts: 13  

Re: [SOLVED] How to run a script on suspend/resume?

Here is manual for elogind (look for "Hook directories"):
https://manpages.debian.org/bullseye/el … .1.en.html

Last edited by xsR_head (2021-09-05 00:14:24)

Offline

#8 2021-09-05 13:17:17

xsR_head
Member
Registered: 2021-08-24
Posts: 13  

Re: [SOLVED] How to run a script on suspend/resume?

Here is a script for locking on suspend for someone whose screenlocker/saver shows desktop for a brief time before locking:

#!/bin/sh
case $1 in
	pre)
		light-locker-command -l  
		/bin/sleep 5
  	;;
        post)
                #something on resume (e.g I restart brcmfmac driver)
              	rmmod brcmfmac && modprobe brcmfmac
		/bin/sleep 5
        ;;
esac

P.S:
For it to work you have to have a screensaver running in the background (light-locker in my case)
Also change

light-locker-command -l

to whatever locking command you are using.

Hope this helps someone

Last edited by xsR_head (2021-09-15 12:07:01)

Offline

Board footer