[SOLVED] Trying to run a python program from the suspend sleep/wake event

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
NeilUK
Level 2
Level 2
Posts: 54
Joined: Fri Aug 16, 2013 3:30 am

[SOLVED] Trying to run a python program from the suspend sleep/wake event

Post by NeilUK »

I am trying to use MATE 21.1 on a Medion laptop for my wife. It has an issue that the sound hardware stops working after a suspend. This is fixed with the following file placed in /usr/lib/systemd/system-sleep. The echo commands remove and re-enumerate the PCI sound hardware. The touch commands are just paranoid debugging.

Code: Select all

#!/bin/bash

# put this script into /usr/bin/systemd/system-sleep/ or /usr/lib/systemd/system-sleep/
# owned by root and executable

if [ "${1}" == "pre" ]; then
  /usr/bin/touch /tmp/got_suspended.txt
elif [ "${1}" == "post" ]; then
  echo 1 > /sys/bus/pci/devices/0000:00:1f.3/remove
  echo 1 > /sys/bus/pci/rescan
  /usr/bin/touch /tmp/got_resumed.txt
fi
Unfortunately if an application like Zoom is running, although it happily carries on after a suspend, it loses access to the audio hardware. I figure it's safer just to kill zoom during a suspend, and let my wife restart it, than to have her left with an application that looks as if it's working on video, but not working on sound.

I therefore tried to automate a 'zoom kill', using the same run-on-suspend mechanism.

I've probably gone the long way round in trying to do this. However, I've written a short python program that uses wmctrl to list the open windows, and xdotool to kill one of them (I couldn't figure out how to use bash to do this, wmctrl won't kill windows, and I couldn't figure out how xdotool lists windows). When run by itself, the program works fine, killing any window with the string 'zoom' in the window title.

Code: Select all

with open(r'/tmp/abc123.txt', 'wt') as fout:
    fout.write('got into program\n')

import subprocess as sp

# list all the windows
# wmctrl returns bytes, lines separated by \n
# the first 10 character positions in each line are the window number
# the window title appears later in the string

result = sp.run(['wmctrl', '-l'], capture_output=True)

output = result.stdout.decode()
lines = output.split('\n')

for line in lines:
    if line.strip():        
        if 'zoom' in line.lower():               
            sp.run(['xdotool', 'windowkill', line[:10]])
However, when I simply add 'python3 zoom_kill.py' to the script above, after the /usr/bin/touch /tmp/got_resumed.txt, the debug /tmp/file write in the program doesn't happen, and zoom windows don't get closed. Those got_suspended.txt files get touched, and the audio works.

OK, the beginner question - as it appears the python program doesn't run, presumably the system sends an error message somewhere. How do I access the error message? How do I debug this failure?

Perhaps more complicated questions - is there a better way to do what I'm trying to do? Or can you suggest why my attempt this way isn't working?
Last edited by LockBot on Sun Sep 24, 2023 10:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Trying to run a python program from the suspend sleep/wake event

Post by rene »

I don't know what the process name of "zoom" is but can't you just killall zoom or alike?

As to the python script not working I can't immediately tell you why: xdotool is not standard installed but I'll assume you have it. The method via wmctrl/xdotool seems particularly funky anyway though, so it might be best to not be concerned with it. Should you insist: I assume that post-suspend e.g. journalctl -b | tail will show something.
NeilUK
Level 2
Level 2
Posts: 54
Joined: Fri Aug 16, 2013 3:30 am

Re: Trying to run a python program from the suspend sleep/wake event

Post by NeilUK »

Thanks Rene,
can't you just killall zoom or alike
well yes, if I knew what I was doing. Much easier. I've used pkill -i zoom

And journalctl - I've got sensible debug messages from that, I think it is the 'current directory isn't on the path' issue, but no need to do that roundabout thing now.

What sites do I need to visit, searches to make, books to read, to find out about journalctl and the like, before I need them?
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Trying to run a python program from the suspend sleep/wake event

Post by rene »

NeilUK wrote: Sat Mar 25, 2023 1:12 pm What sites do I need to visit, searches to make, books to read, to find out about journalctl and the like, before I need them?
I advise attentively reading the 500-page manual that came in the box with your copy of Linux Mint -- but should you have misplaced that a decade or so of hanging around Linux forums on the internet can be helpful.

I.e., the documentation situation for "Linux" isn't good and frankly it's only experience that helps. Internet-based information tends to be obsolete three months from publication or not applicable to your specific sector of the Linux fragmentation, but given that you need the information in the first place, you don't know that. Long ago e.g. SUSE would in fact ship its distribution in a big cardboard box with CDs and a thick paper manual. Now not so much: if you're the type that likes things neatly structured and documented you're in for some initial frustrations.
Locked

Return to “Beginner Questions”