Process singleton guard with lockfile
Skill kjuhwa/skills-hub/skills/resilience/process-singleton-guard-with-lockfile
Prevent multiple daemon instances using exclusive lockfile creation and PID validationFrom its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill process-singleton-guard-with-lockfileAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
1.4 KB, 221 tokens by cl100k_base, as published. Nobody here has run it
Singleton daemon guard via exclusive lockfile
Acquire a singleton lock by writing the current PID to a file with exclusive-create ({ flag: 'wx' }). If the file exists, read the PID and probe liveness with process.kill(pid, 0). Dead PID → stale lock, overwrite. Live PID → abort.
Mechanism
function acquireLock(path) {
try {
fs.writeFileSync(path, String(process.pid), { flag: 'wx' });
return true;
} catch (e) {
if (e.code !== 'EEXIST') throw e;
const pid = parseInt(fs.readFileSync(path, 'utf8'), 10);
try { process.kill(pid, 0); return false; } // alive → abort
catch { fs.writeFileSync(path, String(process.pid)); return true; } // stale → take
}
}
process.on('exit', () => fs.unlinkSync(path));
When to reuse
Background workers, evolution loops, schedulers, anything where parallel invocation would corrupt shared state.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.