Files
bitburner-src/markdown/bitburner.ns.exec.md
David Walker b51ed8fd59 BUG: Fix missed cases in offline server handling (#2495)
There were two large holes in the existing offline server handling:

1. It didn't include IPs, so scripts that used IPs instead of hostnames
   would get exceptions thrown for "server not found."
2. Coverage was very low for non-Darknet APIs. Maybe most of them don't
   need to be covered, but many obvious ones like "ps", "killall" and
   "hasRootAccess" were missing. IMO the only reliable answer is one
   that enforces *all* are covered via the type system.

To accomplish the second part, helpers.getServer() was changed to return
null when a server is offline. This intentionally breaks a lot of its
utility, which was to return a server unconditionally. To compensate,
its utility was increased - it now also does unknown argument
processing, allowing it to subsume a common line that all callers were
repeating.

Some callers switched to ctx.workerScript.getServer(), because they
didn't actually need to be using helpers.getServer(). Similarly, a few
callsites switched to GetServerOrThrow(), for the cases where it should
be guaranteed that the server is valid. The rest are returning a
default/failure response when the server is offline. (Except for
contracts, which threw on failure already anyway.)
2026-02-15 10:29:47 -08:00

2.7 KiB

Home > bitburner > NS > exec

NS.exec() method

Start another script on any server.

Signature:

exec(script: string, host: string, threadOrOptions?: number | RunOptions, ...args: ScriptArg[]): number;

Parameters

Parameter

Type

Description

script

string

Filename of script to execute. This file must already exist on the target server.

host

string

Hostname/IP of the target server on which to execute the script.

threadOrOptions

number | RunOptions

(Optional) Either an integer number of threads for new script, or a RunOptions object. Threads defaults to 1.

args

ScriptArg[]

Additional arguments to pass into the new script that is being run. Note that if any arguments are being passed into the new script, then the third argument threadOrOptions must be filled in with a value.

Returns:

number

Returns the PID of a successfully started script, and 0 otherwise.

Remarks

RAM cost: 1.3 GB

Run a script as a separate process on a specified server. This is similar to the function run, except that it can be used to run a script that already exists on any server, instead of just the current server.

If the script was successfully started, then this function returns the PID of that script. Otherwise, it returns 0.

PID stands for Process ID. The PID is a unique identifier for each script across all hosts. The PID will always be a positive integer.

Running this function with 0 or fewer threads will cause a runtime error.

Example

// The simplest way to use the exec command is to call it with just the script name
// and the target server. The following example will try to run generic-hack.js
// on the foodnstuff server.
ns.exec("generic-hack.js", "foodnstuff");

// The following example will try to run the script generic-hack.js on the
// joesguns server with 10 threads.
ns.exec("generic-hack.js", "joesguns", {threads: 10});

// This last example will try to run the script foo.js on the foodnstuff server
// with 5 threads. It will also pass the number 1 and the string “test” in as
// arguments to the script.
ns.exec("foo.js", "foodnstuff", 5, 1, "test");