HSTask
TypeObject representing an external process task
Properties
Methods
start() -> HSTask
Start the task
Declaration
start() -> HSTask
Returns
The task object for chaining
Notes
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.
Example
const t = hs.task.create("/bin/echo", ["hi"])
t.start()
terminate() -> None
Terminate the task (send SIGTERM)
Declaration
terminate() -> None
Returns
None
Example
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.terminate()
kill9() -> None
Terminate the task with extreme prejudice (send SIGKILL)
Declaration
kill9() -> None
Returns
None
Example
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.kill9()
interrupt() -> None
Interrupt the task (send SIGINT)
Declaration
interrupt() -> None
Returns
None
Example
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.interrupt()
pause() -> None
Pause the task (send SIGSTOP)
Declaration
pause() -> None
Returns
None
Example
const t = hs.task.create("/bin/sleep", ["60"]).start()
t.pause()
resume() -> None
Resume the task (send SIGCONT)
Declaration
resume() -> None
Returns
None
Example
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
Declaration
blockUntilExit(timeout) -> boolean
Parameters
| 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. |
Returns
boolean
true if the task isn't running, false if it was still running when the time ran outNotes
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`.
Example
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
Declaration
waitUntilExit(timeout) -> Promise<boolean>
Parameters
| 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. |
Returns
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 outExample
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
Declaration
sendInput(data) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| data | string | The string data to write |
Returns
None
Notes
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()`.
Example
const t = hs.task.create("/usr/bin/cat", []).start()
t.sendInput("hello\n")
closeInput() -> None
Close the task's stdin
Declaration
closeInput() -> None
Returns
None
Notes
Any data passed to `sendInput()` is still written before stdin is closed.
Example
const t = hs.task.create("/usr/bin/cat", []).start()
t.sendInput("hello\n")
t.closeInput()