bitburner-scripts/src/utils/killall.ts
2026-05-01 20:01:43 +08:00

34 lines
876 B
TypeScript

import { NS } from "@ns";
import { scan } from "./scan";
export const killall = (ns: NS): void => {
const allHosts = scan(ns);
let hostsWithKills = 0;
let killedProcesses = 0;
allHosts.forEach((host) => {
// do not kill any scripts on home
if (host === "home") return;
// check if there are any running scripts on this host
const runningProcesses = ns.ps(host).length;
if (runningProcesses === 0) {
return;
} else {
// ns.killall returns true if it successfully stopped scripts
if (ns.killall(host)) {
hostsWithKills++;
killedProcesses += runningProcesses;
ns.tprint(`Killed ${runningProcesses} processes on ${host}`);
}
}
});
ns.tprint(`Processes killed: ${killedProcesses}`);
ns.tprint(`Hosts affected: ${hostsWithKills}`);
};
export const main = (ns: NS) => {
killall(ns);
};