The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 1,093     Votes:  3     ✅ Solution
Tag : suspend  
Link: 🔍 See Original Answer on Ask Ubuntu ⧉ 🔗

URL: https://askubuntu.com/q/851917
Title: Stop a process when my PC sleeps
ID: /2016/11/21/Stop-a-process-when-my-PC-sleeps
Created: November 21, 2016    Edited:  November 29, 2017
Upload: September 15, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


Method 1 - using a script within /lib/systemd/system-sleep directory

Create a script in /lib/systemd/system-sleep that looks like this:

#!/bin/sh

case $1/$2 in
  pre/*)
    echo "Going to $2..."
    killall libinput-debug-events
    ;;
  post/*)
    echo "Waking up from $2..."
    # Place your post suspend (resume) commands here, or `exit 0` if no post suspend action required
    sleep 2
    libinput-gestures-setup start
    ;;
esac

To ensure the script is created with the right permissions, copy and existing script and then edit it:

cd /lib/systemd/system-sleep
sudo cp wpasupplicant tv_refresh
gksu gedit tv_refresh

The sleep 2 pause may be unnecessary for you but for my setup it was necessary restoring sound from laptop back to HDMI TV.

The echo lines are optional but are handy because they show up in /var/log/syslog.

Method 2 - using systemd services for root or user

From: (archlinux - Power Management) we get detailed instructions for suspending and resuming either under root powers or user powers.

Suspend/resume service files

Service files can be hooked into suspend.target, hibernate.target and sleep.target to execute actions before or after suspend/hibernate. Separate files should be created for user actions and root/system actions. Enable the suspend@user and resume@user services to have them started at boot. Examples:

Suspend

/etc/systemd/system/suspend@.service
[Unit]
Description=User suspend actions
Before=sleep.target

[Service]
User=%I
Type=simple
Environment=DISPLAY=:0
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'
ExecStart=/usr/bin/sflock
ExecStartPost=/usr/bin/sleep 1

[Install]
WantedBy=sleep.target

Resume

/etc/systemd/system/resume@.service
[Unit]
Description=User resume actions
After=suspend.target

[Service]
User=%I
Type=simple
ExecStartPre=/usr/local/bin/ssh-connect.sh
ExecStart=/usr/bin/mysql -e 'slave start'

[Install]
WantedBy=suspend.target 
⇧ How do I stop Ubuntu from asking for my password every time I install something? Any good application for data usage monitor?  ⇩