@jsh/process
Since v8.0.52The @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
|
|
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
|
|
args()
Get command line arguments
Syntax
args()
Parameters
None.
Return value
String[]
Usage example
|
|
cwd()
Get the current working directory
Syntax
cwd()
Parameters
None.
Return value
String
Usage example
|
|
cd()
Change the current working directory.
Syntax
cd(path)
Parameters
path
: directory path to move
Return value
None.
Usage example
|
|
readDir()
Read files and sub-directories of the given directory.
Syntax
readDir(path, callback)
Parameters
path
:String
path to the directorycallback
: function (DirEntry) [undefined|Boolean] callback function. if it returns false, the iteration will stop.
Return value
None.
Usage example
|
|
DirEntry
Property | Type | Description |
---|---|---|
name | String | |
isDir | Boolean | |
readOnly | Boolean | |
type | String | |
size | Number | |
virtual | Boolean |
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
|
|
println()
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
|
|
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
|
|
daemonize()
Run the current script file as a daemon process with its parent process ID set to 1
.
Syntax
daemonize(opts)
Parameters
opts
Object
Options
Property | Type | Description |
---|---|---|
reload | Boolean | enable hot-reload |
If reload
is set to true
, the daemon process starts with a source code change watcher.
When the main source code file is modified, the current daemon process is stopped and restarted immediately to apply the changes.
This feature is useful during development and testing
but should not be enabled in production environments, as it requires an additional system resources to monitor file changes.
Return value
None.
Usage example
|
|
isDaemon()
Returns true
if the parent process ID (ppid()
) is 1
. This is equivalent to the condition ppid() == 1
.
Syntax
isDaemon()
Parameters
None.
Return value
Boolean
isOrphan()
Returns true
if the parent process ID is not assigned. This is equivalent to the condition ppid() == 0xFFFFFFFF
.
Syntax
isOrphan()
Parameters
None.
Return value
Boolean
schedule()
Run the callback function according to the specified schedule.
The control flow remains blocked until the token’s stop()
method is invoked.
Syntax
schedule(spec, callback)
Parameters
spec
String
schedule spec. Refer to Timer Schedule Spec..callback
(time_epoch, token) => {}
The first parameter,time_epoch
, is UNIX epoch timestamp in milliseconds unit. A callback function where the second parameter,token
, can be used to stop the schedule.
Return value
None.
Usage example
|
|
sleep()
Pause the current control flow.
Syntax
sleep(duration)
Parameters
duration
Number
sleep duration in milliseconds.
Return value
None.
Usage example
|
|
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
|
|
ps()
List all currently running processes.
Syntax
ps()
Parameters
None.
Return value
Object[]
: Array of Process objects.
Usage example
|
|
Process
Process information that returned by ps()
.
Property | Type | Description |
---|---|---|
pid | Number | process ID |
ppid | Number | process ID of the parent |
user | String | username (e.g: sys ) |
name | String | Script file name |
uptime | String | Elapse 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
|
|
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
|
|