API Docs

Object representing an external process task

Properties

isRunning

boolean

Check if the task is currently running

pid

number

The process ID of the running task

environment

{[key: string]: string}

The environment variables for the task

workingDirectory

string

The working directory for the task

terminationStatus

NSNumber

The termination status of the task

Methods

start() -> HSTask

Start the task

start() -> HSTask
The task object for chaining
A running task, and its callbacks, stay alive until it terminates, even if you don't keep a reference to it. Use `terminate()` to stop it early.
const t = hs.task.create("/bin/echo", ["hi"])
t.start()

terminate() -> None

Terminate the task (send SIGTERM)

terminate() -> None
None
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.terminate()

kill9() -> None

Terminate the task with extreme prejudice (send SIGKILL)

kill9() -> None
None
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.kill9()

interrupt() -> None

Interrupt the task (send SIGINT)

interrupt() -> None
None
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.interrupt()

pause() -> None

Pause the task (send SIGSTOP)

pause() -> None
None
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.pause()

resume() -> None

Resume the task (send SIGCONT)

resume() -> None
None
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.pause(); t.resume()

blockUntilExit(timeout) -> boolean

Wait for the task to exit, blocking Hammerspoon until it does

blockUntilExit(timeout) -> boolean
Name Type Description
timeout optional number The most time to wait, in seconds. Leave it out (or pass null or Infinity) to wait for as long as the task runs. 0 or a negative number doesn't wait.
boolean
true if the task isn't running, false if it was still running when the time ran out
Nothing else in your configuration runs while this waits: timers, hotkeys and other callbacks are delayed until it returns. Use `waitUntilExit()` to wait without blocking. A task that never finishes blocks Hammerspoon forever unless you give a timeout, so always give one where that would be a problem, e.g. in `hs.onShutdown`.
const t = hs.task.create("/bin/echo", ["hi"]).start()
t.blockUntilExit()

const slow = hs.task.create("/bin/sleep", ["60"]).start()
if (!slow.blockUntilExit(2)) {
    slow.terminate()
}

waitUntilExit(timeout) -> Promise<boolean>

Wait for the task to exit, without blocking Hammerspoon

waitUntilExit(timeout) -> Promise<boolean>
Name Type Description
timeout optional number The most time to wait, in seconds. Leave it out (or pass null or Infinity) to wait for as long as the task runs. 0 or a negative number doesn't wait.
Promise<boolean>
A Promise that resolves to true once the task isn't running (after its termination callback has been called, so all its output has been delivered), or to false if it was still running when the time ran out
const t = hs.task.create("/bin/echo", ["hi"]).start()
t.waitUntilExit().then(() => console.log("Exit status: " + t.terminationStatus))

const slow = hs.task.create("/bin/sleep", ["60"]).start()
slow.waitUntilExit(2).then(exited => {
    if (!exited) slow.terminate()
})

sendInput(data) -> None

Write data to the task's stdin

sendInput(data) -> None
Name Type Description
data string The string data to write
None
The data is written in the background, in the order `sendInput()` was called, so a large write doesn't block Hammerspoon while the task reads it. It has no effect before the task has started, after it has finished, or after `closeInput()`.
const t = hs.task.create("/usr/bin/cat", []).start()
t.sendInput("hello\n")

closeInput() -> None

Close the task's stdin

closeInput() -> None
None
Any data passed to `sendInput()` is still written before stdin is closed.
const t = hs.task.create("/usr/bin/cat", []).start()
t.sendInput("hello\n")
t.closeInput()