Skip to content

Commit

Permalink
Fix sudokill for longer usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 20, 2024
1 parent aa7510e commit 0805444
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "domcloud-bridge",
"version": "0.51.0",
"version": "0.51.1",
"description": "Deployment runner for DOM Cloud",
"main": "app.js",
"engines": {
Expand Down
15 changes: 5 additions & 10 deletions sudokill.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const { exec } = shelljs;
const opts = cli.parse({
test: ['t', 'Test mode', 'bool', false],
ignore: ['i', 'Ignore user list', 'string', ''],
verbose: ['v', 'verbose', 'bool', false],
});

const psOutput = exec('ps -eo user:20,pid,etimes,command --forest --no-headers', {
const psOutput = exec('ps -eo user:70,pid,etimes,command --forest --no-headers', {
silent: true,
fatal: true,
}).stdout.trim().split('\n');
Expand All @@ -26,11 +25,11 @@ const whoOutput = exec('who', {
fatal: true,
}).stdout.trim().split('\n');

const ignoreUsers = opts.ignore.split(',')
const ignoreUsers = opts.ignore ? opts.ignore.split(',')
.reduce((acc, cur) => {
acc[cur] = true;
return acc;
}, {});
}, {}) : {};

if (existsSync(LOGINLINGERDIR)) {
const lingerFiles = readdirSync(LOGINLINGERDIR, { withFileTypes: true });
Expand All @@ -42,7 +41,7 @@ if (existsSync(LOGINLINGERDIR)) {

ignoreUsers.root = true;

if (opts.verbose) {
if (opts.test) {
console.log('Ignoring users: ' + Object.keys(ignoreUsers).join(','));
}

Expand All @@ -51,7 +50,6 @@ const splitTest = /^([\w.-]+\+?) +(\d+) +(\d+) (.+)$/;
const lists = psOutput
.map(x => splitTest.exec(x))
.filter(x => x !== null && !ignoreUsers[x[1]]).map(match => ({
raw: match[0],
user: match[1],
pid: match[2],
etimes: parseInt(match[3]),
Expand All @@ -66,12 +64,9 @@ for (const item of whoOutput) {
let candidates = lists.filter(x => x.etimes > 10800 || (x.command[0] != ' ' && !ignoreUsers[x.user] && x.etimes > 60));

if (opts.test) {
console.log(candidates.map(x => x.raw).join('\n'));
console.table(candidates);
} else {
for (let x of candidates) {
if (opts.verbose) {
console.log(`Killing ${x.user}: ${x.pid} (${x.command})`);
}
exec(`kill -9 ${x.pid}`);
}
}

0 comments on commit 0805444

Please sign in to comment.