Breaking
April 16, 2025

Linux Fu: Stopping a Runaway Al Williams | usagoldmines.com

The best kind of Hackaday posts are the ones where there was some insurmountable problem with an elegant solution devised through deep analysis of the problem and creativity. This is not one of those posts. I’m sure you are familiar with bit rot. You know, something works for a long time and then, for no apparent reason, stops working. Well, that has been biting me, and lacking the time for the creative, elegant solution, I decided to attack it with a virtual chainsaw.

It all started with a 2022 Linux Fu about using autokey.

The Problem

I use autokey to give me emacs-style keystrokes in Web browsers and certain other programs. It intercepts keystrokes and translates them into other keystrokes. The problem is, the current Linux community hates autokey. Well, that’s not strictly true. They just love Wayland more. One reason I won’t switch from X11 is that I haven’t found a way to do something like I do with autokey. But since most of the powers-that-be have decided that X11 is bad and Wayland is good, X11 development is starting to show cracks.

In particular, autokey isn’t in the normal repositories for my distro anymore (KDE Neon). Of course, I’ve installed the latest version myself. I’m perfectly capable of doing that or even building from source. But lately, I’ve noticed my computer hangs, especially after sleeping for a long time. Also, after a long time, I notice that autokey just quits working. It is running but not working and I have to restart it. The memory consumption seems high when this happens.

You know how it is. Your system has quirks; you just live with them for a while. But eventually those paper cuts add up. I finally decided I needed to tackle the issue. But I don’t really have time to go debug autokey, especially when it takes hours for the problem to manifest.

The Chainsaw

I’ll say it upfront: Finding the memory leak would be the right thing to do. Build with debug symbols. Run the code and probe it when the problem comes up. Try to figure out what combination of X11, evdev, and whatever other hocus pocus it uses is causing this glitch.

But who’s got time for that? I decided that instead of launching autokey directly, I’d launch a wrapper script. I already had autokey removed from the KDE session so that I don’t try to start it myself and then get the system restaring it also. But now I run the wrapper instead of autokey.

So what does the wrapper do? It watches the memory consumption of autokey. Sure enough, it goes up just a little bit all the time. When the script sees it go over a threshold it kills it and restarts it. It also restarts if autokey dies, but I rarely see that.

What’s Memory Mean?

The problem is, how do you determine how much memory a process is using? Is it the amount of physical pages it has? The virtual space? What about shared libraries? In this case, I don’t really care as long as I have a number that is rising all the time that I can watch.

The /proc file system has a directory for each PID and there’s a ton of info in there. One of them is an accounting of memory. If you look at /proc/$PID/smaps for some program you’ll see something like this:

00400000-00420000 r--p 00000000 fd:0e 238814592 /usr/bin/python3.12
Size: 128 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Rss: 128 kB
Pss: 25 kB
Pss_Dirty: 0 kB
Shared_Clean: 128 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 128 kB
Anonymous: 0 kB
KSM: 0 kB
LazyFree: 0 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
FilePmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
Locked: 0 kB
THPeligible: 0
VmFlags: rd mr mw me sd 
00420000-00703000 r-xp 00020000 fd:0e 238814592 /usr/bin/python3.12
Size: 2956 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Rss: 2944 kB
Pss: 595 kB
Pss_Dirty: 0 kB
Shared_Clean: 2944 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
. . .

Note that there is a section for each executable and shared object along with lots of information. You can get all the PSS (proportional set size) numbers for each module added together like this (among other ways):


cat /proc/$PID/smaps | grep -i pss | awk '{Total+=$2} END { print Total}'

Building the Chainsaw

So armed with that code, it is pretty easy to just run the program, see if it is eating up too much memory, and restart it if it is. I also threw in some optional debugging code.

#!/bin/bash
#- Run autokey, kill it if it gets too big
#- what's too big? $MLIMIT
MLIMIT=500000 
#- how often to check (seconds)
POLL=10

#- Print debug info if you want
function pdebug {
#- comment out if you don't want debugging. Leave in if you do
#- echo $1 $2 $3 $4
}

while true   # do forever
do
   PID=$(pgrep autokey-qt)  # find autokey
   pdebug "PID",$PID
   if [ ! -z "$PID" ]   # if it is there
   then
      # get the memory size
      PSS=$(cat /proc/$PID/smaps | grep -i pss | awk '{Total+=$2} END { print Total}')
      pdebug "PSS", $PSS
      echo $PSS >>/tmp/autokey-current.log
      # too big? 
      if [ "$PSS" -gt "$MLIMIT" ]
      then
         pdebug "Kill"
         echo Killed >>/tmp/autokey-current.log
         # save old log before we start another
         cp /tmp/autokey-current.log /tmp/autokey-$PID.log
         kill $PID
         PID=
         sleep 2
      fi
   fi
   if [ -z $PID ]
   then
      # if died, relaunch
      pdebug "Launch"
      autokey-qt & 2>&1 >/tmp/autokey-current.log
   fi
   pdebug "Sleep"
   sleep $POLL
done

In practice, you’ll probably want to remove the cp command that saves the old log, but while troubleshooting, it is good to see how often the process is killed. Running this once with a big number gave me an idea that PSS was about 140,000 but rising every 10 seconds. So when it gets to 500,000, it is done. That seems to work well. Obviously, you’d adjust the numbers for whatever you are doing.

Bad Chainsaw

There are lots of ways this could have been done. A systemd timer, for example. Maybe even a cgroup. But this works, and took just a few minutes. Sure, a chainsaw is a lot to just cut a 2×4, but then again, it will go through it like a hot knife through butter.

I did consider just killing autokey periodically and restarting it. The problem is I work odd hours sometimes, and that means I’d have to do something like tie it to the screensaver. But I agree there are dozens of ways to do this, including to quit using autokey. What would your solution be? Let us know in the comments. Have you ever resorted to a trick this dirty?

 

This articles is written by : Nermeen Nabil Khear Abdelmalak

All rights reserved to : USAGOLDMIES . www.usagoldmines.com

You can Enjoy surfing our website categories and read more content in many fields you may like .

Why USAGoldMines ?

USAGoldMines is a comprehensive website offering the latest in financial, crypto, and technical news. With specialized sections for each category, it provides readers with up-to-date market insights, investment trends, and technological advancements, making it a valuable resource for investors and enthusiasts in the fast-paced financial world.

Recent:

DIY AI Butler Is Simpler and More Useful Than Siri Donald Papp | usagoldmines.com

Making Parts Feeders Work Where They Weren’t Supposed To Lewin Day | usagoldmines.com

A New Kind of Bike Valve? Fenix Guthrie | usagoldmines.com

Announcing the Hackaday Pet Hacks Contest Elliot Williams | usagoldmines.com

New Brymen Bluetooth BM788BT Digital Multimeter Coming Soon John Elliot V | usagoldmines.com

Keebin’ with Kristina: the One with John Lennon’s Typewriter Kristina Panos | usagoldmines.com

Shine On You Crazy Diamond Quantum Magnetic Sensor Dan Maloney | usagoldmines.com

This Potato Virtual Assistant is Fully Baked Seth Mabbott | usagoldmines.com

Building A DIY Tornado Tower Lewin Day | usagoldmines.com

Plasmonic Modulators Directly Convert Terahertz Waves to Optical Signals Maya Posch | usagoldmines.c...

Elastic Bands Enable Touchable Volumetric Display Aaron Beckendorf | usagoldmines.com

ESP32-Powered Clock Brings Aviation Style To Your Desk Lewin Day | usagoldmines.com

Cybercriminals Hijacking Popular Crypto Software To Steal Digital Assets From Wallets: Security Rese...

Converting the C64 Mini Into A C64C Lewin Day | usagoldmines.com

A Cheap Yellow Display Makes A Video Walkie Talkie Jenny List | usagoldmines.com

Jenny’s (Not Quite) Daily Drivers: Raspberry Pi 1 Jenny List | usagoldmines.com

DIY scanning spectrometer is a bright idea Tyler August | usagoldmines.com

A Tricky Commodore PET Repair and a Lesson About Assumptions Maya Posch | usagoldmines.com

Introducing Infrared Remote Control Protocols John Elliot V | usagoldmines.com

The ProStar: the Portable Gaming System and Laptop From 1995 Maya Posch | usagoldmines.com

Hackaday Links: April 13, 2025 Dan Maloney | usagoldmines.com

3D Printed Milling Machine is Solid as a Rock Tyler August | usagoldmines.com

The Spade Hardware Description Language John Elliot V | usagoldmines.com

Satisfy Your High-Voltage Urges with This Printable Flyback Transformer Dan Maloney | usagoldmines.c...

Software Project Pieces Broken Bits Back Together Donald Papp | usagoldmines.com

Learning Linux Kernel Modules Using COM Binary Support Maya Posch | usagoldmines.com

A 17th Century Music Computer Al Williams | usagoldmines.com

The Incomplete JSON Pretty Printer (Brought To You By Vibes) Donald Papp | usagoldmines.com

Hacking a Cheap Rechargeable Lamp With Non-Standard USB-C Connector Maya Posch | usagoldmines.com

Vibe Check: False Packages a New LLM Security Risk? Tyler August | usagoldmines.com

JPMorgan Chase Refuses To Reimburse $440,000 After Customer’s Account Hammered With Illicit Withdraw...

DIY Soldering Tweezers, Extra Thrifty Donald Papp | usagoldmines.com

Biting Off More Than I Can Chew Elliot Williams | usagoldmines.com

Bank of America Discloses Data Breach After Customers’ Documents Disappear, Says Names, Addresses, A...

Tiny Pogo Robot Gets Wings, Does Flips Donald Papp | usagoldmines.com

Repairing Classic Sound Cards Jenny List | usagoldmines.com

Tracing the #!: How the Linux Kernel Handles the Shebang Maya Posch | usagoldmines.com

Creating a Somatosensory Pathway From Human Stem Cells Maya Posch | usagoldmines.com

Bank of America Handing $2,850,000 To Customers in Settlement Over Alleged Illegal Fees and Account ...

Gemini 2.0 + Robotics = Slam Dunk? John Elliot V | usagoldmines.com

A Mouse, No Hands! Jenny List | usagoldmines.com

Hackaday Podcast Episode 316: Soft Robots, Linux the Hard Way, Cellphones into SBCs, and the Circuit...

GPS Broken? Try TV! Al Williams | usagoldmines.com

Audio Effects Applied to Text Al Williams | usagoldmines.com

This Week in Security: AI Spam, SAP, and Ivanti Jonathan Bennett | usagoldmines.com

The Jupiter Ace Remembered Al Williams | usagoldmines.com

Brush Up on Your Trade Craft With This Tiny FM Bug Dan Maloney | usagoldmines.com

Farewell Economy 7, a Casualty of the Long Wave Switch-Off Jenny List | usagoldmines.com

Using Integer Addition to Approximate Float Multiplication Maya Posch | usagoldmines.com

Windows on ARM on Arm Bryan Cockfield | usagoldmines.com

A New Mechanical Keyboard for an Old Computer Bryan Cockfield | usagoldmines.com

Improving Magnetoplasmadynamic Ion Thrusters With Superconductors Maya Posch | usagoldmines.com

Ask Hackaday: What’s a Sun-Like Star? Al Williams | usagoldmines.com

Clever Engineering Leaves Appliance Useless Tom Nardi | usagoldmines.com

Everyone’s Talking GPMI, Should you? Jenny List | usagoldmines.com

Making Liquid Oxygen: Far From Easy but Worth the Effort Dan Maloney | usagoldmines.com

Ask Hackaday: Vibe Coding Jenny List | usagoldmines.com

Ask Hackaday: Vibe Coding Jenny List | usagoldmines.com

Going to the Top with a Raspberry Pi Elevator Al Williams | usagoldmines.com

FLOSS Weekly Episode 828: Incus Inception Jonathan Bennett | usagoldmines.com

Self-Hosting a Cluster on Old Phones Bryan Cockfield | usagoldmines.com

You Shouldn’t Build an X-Ray Machine, But You Could Al Williams | usagoldmines.com

In 2025, The Philly Maker Faire Finds Its Groove Tom Nardi | usagoldmines.com

Forget Propellers, Embrace Tentacle-based Locomotion Donald Papp | usagoldmines.com

Better Bearings Take the Wobble Out of Premium Scroll Wheel Dan Maloney | usagoldmines.com

Salamander Robot is Squishy Al Williams | usagoldmines.com

Dozens of Solenoids Turn Vintage Typewriter into a Printer Dan Maloney | usagoldmines.com

ASCII to Mainframe Al Williams | usagoldmines.com

Designing A Tone Control Properly Jenny List | usagoldmines.com

The Computers of EPCOT Al Williams | usagoldmines.com

FreeDOS 1.4 Released Maya Posch | usagoldmines.com

Turning Old Cellphones into SBCs Elliot Williams | usagoldmines.com

LayerLapse Simplifies 3D Printer Time-lapse Shots Tom Nardi | usagoldmines.com

Printed Robotic Arm Pumps Up With Brushless Motors Tom Nardi | usagoldmines.com

The 1980s Computer, French Style Jenny List | usagoldmines.com

Simple Antenna Makes for Better ESP32-C3 WiFi Dan Maloney | usagoldmines.com

Atomic Clock Trades Receiver for an ESP8266 Tom Nardi | usagoldmines.com

Buyer Beware: Cheap Power Strips Hold Hidden Horrors Dan Maloney | usagoldmines.com

A Tiny Tape Synth Jenny List | usagoldmines.com

London Police Team Up With International Law Firm After Elderly Victim Defrauded for $2,580,000 Rhod...

Keebin’ with Kristina: the One Where We Embrace the Jank Kristina Panos | usagoldmines.com

Fiber Laser Gives DIY PCBs a Professional Finish Tom Nardi | usagoldmines.com

Why USB-C Splitters Can Cause Magic Smoke Release Maya Posch | usagoldmines.com

Which Browser Should I Use In 2025? Jenny List | usagoldmines.com

Tracking the ISS Made Easy Elliot Williams | usagoldmines.com

A Tale Of Nuclear Shenanigans From Down Under Jenny List | usagoldmines.com

Pluto’s Not a Planet, But It Is a Spectrum Analyzer Al Williams | usagoldmines.com

Command and Conquer Ported to the Pi Pico 2 Tom Nardi | usagoldmines.com

Hackers Hammer Android and iPhone Users As Bank Account Attacks Surge 258% in One Year: Kaspersky Ma...

Reverse Engineering the IBM PC110, One PCB at a Time Tom Nardi | usagoldmines.com

An Amstrad PCW Receives A Bit Of Love Jenny List | usagoldmines.com

Emulating iPhone on QEMU Al Williams | usagoldmines.com

Celebrating 30 Years of Windows 95 at VCF Adam Fabio | usagoldmines.com

A Low F Number Lens, From Scratch Jenny List | usagoldmines.com

Ben Eater vs. Microsoft BASIC Elliot Williams | usagoldmines.com

$4,400 Drained From Bank of America Account After Owner Is Violently Robbed of His Unlocked Phone – ...

Inside an Edison Phonograph Al Williams | usagoldmines.com

How Shrinking Feature Size Made Modern Wireless Work Elliot Williams | usagoldmines.com

How a Tiny Relay Became a USB Swiss Army Knife Heidi Ulrich | usagoldmines.com

Wells Fargo Ordered To Pay $832,000,000 in Damages After Jury Finds Lender Mismanaged and Charged Un...

Leave a Reply