@kaynooo/cli-proc 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pid-cli.js +322 -76
- package/dist/ports-cli.js +344 -92
- package/dist/proc-cli.js +371 -117
- package/dist/procs-cli.js +336 -88
- package/package.json +2 -2
- package/src/pid.ts +4 -3
- package/src/ports.ts +11 -9
- package/src/procs.ts +10 -7
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-proc",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.2",
|
|
5
5
|
"description": "pid, ports, procs: one process inspected, who listens on a port, and leftover or heavy dev processes cleaned, read straight from /proc (Linux).",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"procs": "$npm_execpath src/procs-cli.ts"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@kaynooo/cli-kit": "0.
|
|
38
|
+
"@kaynooo/cli-kit": "0.38.3"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/pid.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Limit, Socket } from './parse'
|
|
2
3
|
import type { ProcInfo } from './read'
|
|
3
4
|
import process from 'node:process'
|
|
@@ -9,9 +10,8 @@ import { bootMs, fdCount, readProc, readSockets, readText, socketsOf, userName }
|
|
|
9
10
|
const usage = `pid <pid>
|
|
10
11
|
|
|
11
12
|
Shows what a process is: its executable, working directory, command line, parent chain up to init, user, start time and age, memory, open files and limits, the TCP and UDP ports it listens on and its cgroup. Everything is read from /proc; what the calling user cannot read of another user's process is left out and named at the end.
|
|
12
|
-
The environment is not shown unless --env is given, with every value that looks like a secret masked
|
|
13
|
-
|
|
14
|
-
Exits 0 when it showed the process, 1 when no process has this pid, 2 on a usage error.`
|
|
13
|
+
The environment is not shown unless --env is given, with every value that looks like a secret masked.`
|
|
14
|
+
const EXITS: ExitRow[] = [[0, 'it showed the process'], [1, 'no process has this pid'], [2, 'a usage error']]
|
|
15
15
|
|
|
16
16
|
const DIGITS = /^\d+$/
|
|
17
17
|
|
|
@@ -166,6 +166,7 @@ function render(info: Report, showEnv: boolean): string {
|
|
|
166
166
|
|
|
167
167
|
export async function pid(args: string[]): Promise<number> {
|
|
168
168
|
const { values, positionals } = parse(args, usage, options, {
|
|
169
|
+
exits: EXITS,
|
|
169
170
|
positionals: ['<pid>'],
|
|
170
171
|
replaces: 'cat /proc/<pid>/{cmdline,environ,limits}, readlink /proc/<pid>/{exe,cwd}, the fish pid function',
|
|
171
172
|
examples: [
|
package/src/ports.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Container, Protocol, Socket } from './parse'
|
|
2
3
|
import type { ProcInfo } from './read'
|
|
3
4
|
import { basename } from 'node:path'
|
|
4
5
|
import process from 'node:process'
|
|
5
|
-
import { badUsage, duration, fail, jsonOption, paint, parse, printJson, table, truncate, watching, watchOption, wholeNumber, withoutWatch } from '@kaynooo/cli-kit'
|
|
6
|
+
import { badUsage, duration, fail, jsonOption, paint, parse, printJson, problem, table, truncate, watching, watchOption, wholeNumber, withoutWatch } from '@kaynooo/cli-kit'
|
|
6
7
|
import { confirm } from '@kaynooo/cli-kit/prompt'
|
|
7
8
|
import { dockerContainers } from './docker'
|
|
8
9
|
import { protectionOf, terminate } from './kill'
|
|
@@ -13,9 +14,8 @@ const usage = `ports [port]
|
|
|
13
14
|
|
|
14
15
|
Lists what listens on this machine, TCP and UDP over IPv4 and IPv6, one row per port with its bound address, the owning process, its command and its working directory. A port Docker publishes is held by docker-proxy, and shows as its compose project and service when docker is installed; the owner of a socket of another user cannot be read and shows as -.
|
|
15
16
|
\`ports <port>\` keeps that port alone and exits 1 when nothing listens on it.
|
|
16
|
-
\`ports <port> --kill\` stops its owner with SIGTERM, and with --force SIGKILL once --grace has passed, after asking on a terminal unless --yes is given. It refuses processes of other users, pid 1, this command and its ancestors, and docker-proxy, for which it names the container to stop instead
|
|
17
|
-
|
|
18
|
-
Exits 0 when it listed or stopped, 1 when nothing listens on the given port or the owner could not be stopped, 2 on a usage error.`
|
|
17
|
+
\`ports <port> --kill\` stops its owner with SIGTERM, and with --force SIGKILL once --grace has passed, after asking on a terminal unless --yes is given. It refuses processes of other users, pid 1, this command and its ancestors, and docker-proxy, for which it names the container to stop instead.`
|
|
18
|
+
const EXITS: ExitRow[] = [[0, 'it listed or stopped'], [1, 'nothing listens on the given port or the owner could not be stopped'], [2, 'a usage error']]
|
|
19
19
|
|
|
20
20
|
const options = {
|
|
21
21
|
json: jsonOption('[{ port, protocol, addresses, pid, command, cwd, container }]'),
|
|
@@ -23,6 +23,7 @@ const options = {
|
|
|
23
23
|
watch: watchOption('the ports'),
|
|
24
24
|
force: { type: 'boolean', description: 'with --kill, SIGKILL what SIGTERM did not stop within --grace' },
|
|
25
25
|
yes: { type: 'boolean', short: 'y', description: 'with --kill, do not ask first' },
|
|
26
|
+
quiet: { type: 'boolean', short: 'q', description: 'with --kill, print nothing about what was stopped' },
|
|
26
27
|
grace: { type: 'string', description: 'with --force, how long SIGTERM gets before SIGKILL (5s)' },
|
|
27
28
|
} as const
|
|
28
29
|
|
|
@@ -103,7 +104,7 @@ function render(rows: PortRow[]): string {
|
|
|
103
104
|
return table([['PORT', 'PROTO', 'ADDRESS', 'PID', 'COMMAND', 'CWD'], ...cells], { header: true })
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>, values: { force?: boolean | undefined, yes?: boolean | undefined, grace?: string | undefined }): Promise<number> {
|
|
107
|
+
async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>, values: { force?: boolean | undefined, yes?: boolean | undefined, quiet?: boolean | undefined, grace?: string | undefined }): Promise<number> {
|
|
107
108
|
const guard = { uid: process.getuid?.() ?? -1, mine: new Set(ancestry(process.pid, procs)) }
|
|
108
109
|
const targets = new Map<number, PortRow>()
|
|
109
110
|
|
|
@@ -111,7 +112,7 @@ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>,
|
|
|
111
112
|
const reason = refusal(row, row.pid === undefined ? undefined : procs.get(row.pid), guard)
|
|
112
113
|
|
|
113
114
|
if (reason !== undefined)
|
|
114
|
-
|
|
115
|
+
problem(`port ${port}: ${reason}`)
|
|
115
116
|
else if (row.pid !== undefined)
|
|
116
117
|
targets.set(row.pid, row)
|
|
117
118
|
}
|
|
@@ -131,17 +132,18 @@ async function stop(port: number, rows: PortRow[], procs: Map<number, ProcInfo>,
|
|
|
131
132
|
|
|
132
133
|
const outcome = await terminate([...targets.keys()], { escalate: values.force === true, graceMs: duration(values.grace, 'grace', 5000) })
|
|
133
134
|
|
|
134
|
-
for (const pid of [...outcome.terminated, ...outcome.killed])
|
|
135
|
+
for (const pid of values.quiet === true ? [] : [...outcome.terminated, ...outcome.killed])
|
|
135
136
|
console.log(`stopped ${pid}${outcome.killed.includes(pid) ? ' (SIGKILL)' : ''}`)
|
|
136
137
|
|
|
137
138
|
for (const pid of outcome.survived)
|
|
138
|
-
|
|
139
|
+
problem(`${pid} is still running${values.force === true ? '' : ', pass --force to SIGKILL it'}`)
|
|
139
140
|
|
|
140
141
|
return outcome.survived.length === 0 ? 0 : 1
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
export async function ports(args: string[]): Promise<number> {
|
|
144
145
|
const { values, positionals } = parse(args, usage, options, {
|
|
146
|
+
exits: EXITS,
|
|
145
147
|
positionals: ['[port]'],
|
|
146
148
|
replaces: 'ss -ltnp | grep, lsof -i, fuser -k and lookups of docker-proxy by hand',
|
|
147
149
|
examples: [
|
|
@@ -175,7 +177,7 @@ export async function ports(args: string[]): Promise<number> {
|
|
|
175
177
|
const rows = buildRows(wanted, owners, byPid, needsDocker ? await dockerContainers() : [])
|
|
176
178
|
|
|
177
179
|
if (port !== undefined && rows.length === 0) {
|
|
178
|
-
|
|
180
|
+
problem(`nothing listens on port ${port}`)
|
|
179
181
|
|
|
180
182
|
return 1
|
|
181
183
|
}
|
package/src/procs.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Group } from './select'
|
|
2
3
|
import { existsSync } from 'node:fs'
|
|
3
4
|
import { homedir } from 'node:os'
|
|
4
5
|
import process from 'node:process'
|
|
5
|
-
import { duration, fail, formatFileSize, formatSeconds, jsonOption, paint, parse, parseSize, printJson, table, truncate, watching, watchOption, withoutWatch } from '@kaynooo/cli-kit'
|
|
6
|
+
import { duration, fail, formatFileSize, formatSeconds, jsonOption, paint, parse, parseSize, printJson, problem, table, truncate, watching, watchOption, withoutWatch } from '@kaynooo/cli-kit'
|
|
6
7
|
import { terminate } from './kill'
|
|
7
8
|
import { maskArgs } from './mask'
|
|
8
9
|
import { ancestry, snapshot } from './read'
|
|
@@ -12,15 +13,15 @@ const usage = `procs
|
|
|
12
13
|
|
|
13
14
|
Lists the leftover or heavy processes of the calling user that are worth cleaning, grouped by origin: the project folder their working directory is in, a browser profile for a chromium and its helpers, else their command. Each group shows its process count, total resident memory and oldest age.
|
|
14
15
|
A process is listed when it is a dev tool (bun, node, vite, nuxt, next, chromium and chrome, playwright and webview browsers and their helpers, language servers, test runners) and either an orphan, reparented to init or to the user's systemd, or heavy (500M resident by default). The processes below a listed one, and those sharing a browser profile with it, are listed too, so a chromium whose main process was killed groups with its leftover helpers.
|
|
15
|
-
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users
|
|
16
|
-
|
|
17
|
-
Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL, 2 on a usage error.`
|
|
16
|
+
\`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users.`
|
|
17
|
+
const EXITS: ExitRow[] = [[0, 'it listed or stopped everything'], [1, 'a process survived SIGKILL'], [2, 'a usage error']]
|
|
18
18
|
|
|
19
19
|
const options = {
|
|
20
20
|
'json': jsonOption('{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }'),
|
|
21
21
|
'kill': { type: 'boolean', conflicts: ['watch'], description: 'stop the listed processes: a dry run without --yes' },
|
|
22
22
|
'watch': watchOption('the processes'),
|
|
23
23
|
'yes': { type: 'boolean', short: 'y', description: 'with --kill, really stop them, without asking' },
|
|
24
|
+
'quiet': { type: 'boolean', short: 'q', description: 'with --kill --yes, print nothing about what was stopped' },
|
|
24
25
|
'min-mem': { type: 'string', description: 'heavy from this resident size (500M), and the least a left over process must use to be listed; a bare number counts megabytes' },
|
|
25
26
|
'older': { type: 'string', description: 'only processes at least this old: 30m, 2h, 1d' },
|
|
26
27
|
'grace': { type: 'string', description: 'how long SIGTERM gets before SIGKILL (5s)' },
|
|
@@ -61,6 +62,7 @@ function render(groups: Group[]): string {
|
|
|
61
62
|
|
|
62
63
|
export async function procs(args: string[]): Promise<number> {
|
|
63
64
|
const { values } = parse(args, usage, options, {
|
|
65
|
+
exits: EXITS,
|
|
64
66
|
replaces: 'pkill -f and pgrep -f one-liners (which match their own shell), ps aux | grep for leftover chromium, bun or vite processes',
|
|
65
67
|
examples: [
|
|
66
68
|
['procs', 'leftover and heavy dev processes, by origin'],
|
|
@@ -107,13 +109,14 @@ export async function procs(args: string[]): Promise<number> {
|
|
|
107
109
|
|
|
108
110
|
const outcome = await terminate(pids, { escalate: true, graceMs: duration(values.grace, 'grace', 5000) })
|
|
109
111
|
|
|
110
|
-
|
|
112
|
+
if (values.quiet !== true || values.json === true)
|
|
113
|
+
show({ stopped: [...outcome.terminated, ...outcome.killed], killed: outcome.killed, survived: outcome.survived })
|
|
111
114
|
|
|
112
|
-
if (values.json !== true)
|
|
115
|
+
if (values.json !== true && values.quiet !== true)
|
|
113
116
|
console.log(`stopped ${outcome.terminated.length + outcome.killed.length} of ${pids.length}${outcome.killed.length === 0 ? '' : `, ${outcome.killed.length} needed SIGKILL`}`)
|
|
114
117
|
|
|
115
118
|
if (outcome.survived.length > 0)
|
|
116
|
-
|
|
119
|
+
problem(`still running: ${outcome.survived.join(' ')}`)
|
|
117
120
|
|
|
118
121
|
return outcome.survived.length === 0 ? 0 : 1
|
|
119
122
|
}
|