Skip to content

Instantly share code, notes, and snippets.

@troglobit
Created May 3, 2021 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troglobit/24411dca2204bb03ae6daf10b99419c0 to your computer and use it in GitHub Desktop.
Save troglobit/24411dca2204bb03ae6daf10b99419c0 to your computer and use it in GitHub Desktop.
Fixed runonce.lua for AwesomeWM
-- @author Peter J. Kranz (Absurd-Mind, peter@myref.net)
-- Any questions, criticism or praise just drop me an email
local awful = require("awful")
local M = {}
-- get the current Pid of awesome
local function getCurrentPid()
-- get awesome pid from pgrep
local fpid = io.popen("pgrep -u " .. os.getenv("USER") .. " -o awesome")
local pid = fpid:read("*n")
fpid:close()
-- sanity check
if pid == nil then
return -1
end
return pid
end
local function getOldPid(filename)
-- open file
local pidFile = io.open(filename)
if pidFile == nil then
return -1
end
-- read number
local pid = pidFile:read("*n")
pidFile:close()
-- sanity check
if pid <= 0 then
return -1
end
return pid;
end
local function writePid(filename, pid)
local pidFile = io.open(filename, "w+")
pidFile:write(pid)
pidFile:close()
end
local function shallExecute(oldPid, newPid)
-- simple check if equivalent
if oldPid == newPid then
return false
end
return true
end
local function getPidFile()
local host = io.lines("/proc/sys/kernel/hostname")()
return awful.util.getdir("cache") .. "/awesome." .. host .. ".pid"
end
-- run Once per real awesome start (config reload works)
-- does not cover "pkill awesome && awesome"
function M.run(shellCommand)
-- check and Execute
if shallExecute(M.oldPid, M.currentPid) then
awful.util.spawn_with_shell(shellCommand)
end
end
M.pidFile = getPidFile()
M.oldPid = getOldPid(M.pidFile)
M.currentPid = getCurrentPid()
writePid(M.pidFile, M.currentPid)
return M
@AdrianVollmer
Copy link

So, pgrep -o $USER awesome yields nothing on my new system. There is no process called awesome, so getCurrentPid returns -1.

There is, however, a process called x-window-manager. This patch seems to make it work again:

--- runonce.lua 2023-03-16 09:25:05.306980790 +0100
+++ runonce.lua.fixed   2023-03-16 09:25:03.135007728 +0100
@@ -8,7 +8,7 @@
 -- get the current Pid of awesome
 local function getCurrentPid()
     -- get awesome pid from pgrep
-    local fpid = io.popen("pgrep -u " .. os.getenv("USER") .. " -o awesome")
+    local fpid = io.popen("pgrep -u " .. os.getenv("USER") .. " -o x-window-manager -f")
     local pid = fpid:read("*n")
     fpid:close()

Is this the right fix? Anyone know what is going on?

@troglobit
Copy link
Author

The code above is just an example, YMMV. The x-window-manager is a shortcut/alternative on Debian/Ubuntu derived systems. Usually ends up as a symlink in /etc/alternatives.

@AdrianVollmer
Copy link

Gotcha, thanks. The script worked fine on my old system and apparently does not on my new system. It's both Debian, so that's why I was confused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment