@jsh/process

@jsh/process

Since v8.0.52

The @jsh/process module is specifically designed for use in JSH applications and is not available in the SCRIPT() function within TQL, unlike other JSH modules.

pid()

Get the process id of the current process.

Syntax

pid()
Parameters

None.

Return value

A number value that represents the process ID.

Usage example

1
2
const m = require("@jsh/process")
console.log("my pid =", m.pid())

ppid()

Get the process id of the parent process.

Syntax

ppid()
Parameters

None.

Return value

A number value that represents the parent process ID.

Usage example

1
2
const m = require("@jsh/process")
console.log("parent pid =", m.ppid())

args()

Get command line arguments

Syntax

args()
Parameters

None.

Return value

String[]

Usage example

1
2
3
4
p = require("@jsh/process");
args = p.args();
x = parseInt(args[1]);
console.log("x =", x);

cwd()

Get the current working directory

Syntax

cwd()
Parameters

None.

Return value

String

Usage example

1
2
p = require("@jsh/process");
console.log("cwd :", p.cwd());

cd()

Change the current working directory.

Syntax

cd(path)
Parameters

path : directory path to move

Return value

None.

Usage example

1
2
3
p = require("@jsh/process");
p.cd('/dir/path');
console.log("cwd :", p.cwd());

readDir()

Read files and sub-directories of the given directory.

Syntax

readDir(path, callback)
Parameters
  • path: String path to the directory
  • callback: function (DirEntry) [undefined|Boolean] callback function. if it returns false, the iteration will stop.
Return value

None.

Usage example

DirEntry

PropertyTypeDescription
nameString
isDirBoolean
readOnlyBoolean
typeString
sizeNumber
virtualBoolean

print()

Write arguments into the output, the default output is the log file or stdout if log filename is not set.

Syntax

print(...args)
Parameters

args ...any Variable length of argument to write.

Return value

None.

Usage example

1
2
p = require("@jsh/process")
p.print("Hello", "World!", "\n")

exec()

Run another JavaScript application.

Syntax

exec(cmd, ...args)
Parameters

cmd String .js file path to run args ...String arguments to pass to the cmd.

Return value

None.

Usage example

1
2
p = require("@jsh/process")
p.exec("/sbin/hello.js")

daemonize()

Run the current script file as a daemon process with its parent process ID set to 1.

Syntax

daemonize()
Parameters

None.

Return value

None.

Usage example

1
2
3
4
5
6
7
p = require("@jsh/process")
p.daemonize()
if( p.ppid() == 1) {
    doBackgroundJob()
} else {
    p.print("daemonize self, then exit")
}

sleep()

Pause the current control flow. This function is provided in the process module for convenience and has an equivalent in the @jsh/system module.

Syntax

sleep(sec)
Parameters

sec Number sleep duration in seconds.

Return value

None.

Usage example

1
2
p = require("@jsh/process")
p.sleep(10)

kill()

Terminate a process using the specified process ID (pid).

Syntax

kill(pid)
Parameters

pid Number pid of target process.

Return value

None.

Usage example

1
2
p = require("@jsh/process")
p.kill(123)

ps()

List all currently running processes.

Syntax

ps()
Parameters

None.

Return value

Process[]: Array of Process objects.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
p = require("@jsh/process")
list = p.ps()
for( const x of list ) {
    console.log(
        p.pid, 
        (p.ppid == 0xFFFFFFFF ? "-" : p.ppid), 
        p.user, 
        p.name, 
        p.uptime)
}

Process

Process information that returned by ps().

Properties

PropertyTypeDescription
pidNumberprocess ID
ppidNumberprocess ID of the parent
userStringusername (e.g: sys)
nameStringScript file name
uptimeStringElapse duration since started

addCleanup()

Add a function to execute when the current JavaScript VM terminates.

Syntax

addCleanup(fn)
Parameters

fn ()=>{} callback function

Return value

Number A token for remove the cleanup callback.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
p = require("@jsh/process")
p.addCleanup(()=>{ console.log("terminated") })
for(i = 0; i < 3; i++) {
    console.log("run -", i)
}

// run - 0
// run - 1
// run - 2
// terminated

removeCleanup()

Remove a previously registered cleanup callback using the provided token.

Syntax

removeCleanup(token)
Parameters

token Number token that returned by addCleanup().

Return value

None.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
p = require("@jsh/process")
token = p.addCleanup(()=>{ console.log("terminated") })
for(i = 0; i < 3; i++) {
    console.log("run -", i)
}
p.removeCleanup(token)

// run - 0
// run - 1
// run - 2
Last updated on