js-apis-process.md 11.0 KB
Newer Older
W
wusongqing 已提交
1
# Obtaining Process Information
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5

W
wusongqing 已提交
6 7

## Modules to Import
Z
zengyawen 已提交
8 9 10 11 12

```
import process from '@ohos.process';
```

W
wusongqing 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## System Capabilities

SystemCapability.Utils.Lang

## Attributes

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| egid | number | Yes| No| Effective group identifier (EGID) of a process.|
| euid | number | Yes| No| Effective user identifier (EUID) of a process.|
| gid | number | Yes| No| Group identifier (GID) of a process.|
| uid | number | Yes| No| User identifier (UID) of a process.|
| groups | number[] | Yes| No| Array with supplementary group IDs.|
| pid | number | Yes| No| Process ID (PID) of a process.|
| ppid | number | Yes| No| Parent process ID (PPID) of a process.|
| tid<sup>8+</sup> | number | Yes| No| Thread ID (TID) of a process.|


## ChildProcess

Allows a process to obtain the standard input and output of its child processes, send signals, and close its child processes.


### Attributes

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| pid | number | Yes| No| PID of the child process.|
| ppid | number | Yes| No| PPID of the child process.|
| exitCode | number | Yes| No| Exit code of the child process.|
| killed | boolean | Yes| No| Whether the parent process successfully sends a signal to the child process to terminate it.|


### wait

wait(): Promise&lt;number&gt;
Z
zengyawen 已提交
49 50 51

Waits until the child process ends. This method uses a promise to return the exit code of the child process.

W
wusongqing 已提交
52
**Return value**
Z
zengyawen 已提交
53

W
wusongqing 已提交
54 55 56
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the exit code of the child process.|
Z
zengyawen 已提交
57

W
wusongqing 已提交
58
**Example**
Z
zengyawen 已提交
59

W
wusongqing 已提交
60 61 62 63 64 65 66
```
var child = process.runCmd('ls');
var result = child.wait();
result.then(val=>{
    console.log("result = " + val);
})
```
Z
zengyawen 已提交
67 68


W
wusongqing 已提交
69
### getOutput
Z
zengyawen 已提交
70

W
wusongqing 已提交
71
getOutput(): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
72 73 74

Obtains the standard output of the child process.

W
wusongqing 已提交
75
**Return value**
Z
zengyawen 已提交
76

W
wusongqing 已提交
77 78 79
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.|
Z
zengyawen 已提交
80

W
wusongqing 已提交
81
**Example**
Z
zengyawen 已提交
82

W
wusongqing 已提交
83 84 85 86 87 88 89
```
var child = process.runCmd('ls');
var result = child.wait();
child.getOutput.then(val=>{
    console.log("child.getOutput = " + val);
})
```
Z
zengyawen 已提交
90 91


W
wusongqing 已提交
92
### getErrorOutput
Z
zengyawen 已提交
93

W
wusongqing 已提交
94
getErrorOutput(): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
95 96 97

Obtains the standard error output of the child process.

W
wusongqing 已提交
98
**Return value**
Z
zengyawen 已提交
99

W
wusongqing 已提交
100 101 102
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.|
Z
zengyawen 已提交
103

W
wusongqing 已提交
104
**Example**
Z
zengyawen 已提交
105

W
wusongqing 已提交
106 107 108 109 110 111 112
```
var child = process.runCmd('madir test.text');
var result = child.wait();
child.getErrorOutput.then(val=>{
    console.log("child.getErrorOutput= " + val);
})
```
Z
zengyawen 已提交
113 114


W
wusongqing 已提交
115
### close
Z
zengyawen 已提交
116

W
wusongqing 已提交
117
close():  void
Z
zengyawen 已提交
118 119 120

Closes the child process in running.

W
wusongqing 已提交
121
**Example**
Z
zengyawen 已提交
122

W
wusongqing 已提交
123 124 125 126
```
var child = process.runCmd('sleep 5; ls');
child.close();
```
Z
zengyawen 已提交
127 128


W
wusongqing 已提交
129
### kill
Z
zengyawen 已提交
130

W
wusongqing 已提交
131
kill(signal: number | string): void
Z
zengyawen 已提交
132 133 134

Sends a signal to the specified child process to terminate it.

W
wusongqing 已提交
135
**Parameters**
Z
zengyawen 已提交
136

W
wusongqing 已提交
137 138 139
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.|
Z
zengyawen 已提交
140

W
wusongqing 已提交
141
**Example**
Z
zengyawen 已提交
142

W
wusongqing 已提交
143 144 145 146
```
var child = process.runCmd('sleep 5; ls');
child.kill(9);
```
Z
zengyawen 已提交
147

Z
zengyawen 已提交
148

W
wusongqing 已提交
149
## process.isIsolatedProcess<sup>8+</sup>
Z
zengyawen 已提交
150

W
wusongqing 已提交
151
isIsolatedProcess(): boolean
Z
zengyawen 已提交
152

W
wusongqing 已提交
153
Checks whether this process is isolated.
Z
zengyawen 已提交
154

W
wusongqing 已提交
155
**Return value**
Z
zengyawen 已提交
156

W
wusongqing 已提交
157 158 159
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
Z
zengyawen 已提交
160

W
wusongqing 已提交
161
**Example**
Z
zengyawen 已提交
162

W
wusongqing 已提交
163 164 165
```
var result = process.isIsolatedProcess();
```
Z
zengyawen 已提交
166 167


W
wusongqing 已提交
168
## process.isAppUid<sup>8+</sup>
Z
zengyawen 已提交
169

W
wusongqing 已提交
170
isAppUid(v:number): boolean
Z
zengyawen 已提交
171 172 173

Checks whether a UID belongs to this app.

W
wusongqing 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| UID.|

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the UID is the app's UID; returns **false** otherwise.|

**Example**

```
var result = process.isAppUid(688);
```


## process.is64Bit<sup>8+</sup>

is64Bit(): boolean

Checks whether this process is running in a 64-bit environment.

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the process is running in a 64-bit environment; returns **false** otherwise.|

**Example**

```
var ressult = process.is64Bit();
```


## process.getUidForName<sup>8+</sup>

getUidForName(v:string): number
Z
zengyawen 已提交
215 216 217

Obtains the process UID based on the process name.

W
wusongqing 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | string | Yes| Name of a process.|

**Return value**

| Type| Description|
| -------- | -------- |
| number | Process UID.|

**Example**

```
var pres = process.getUidForName("tool")
```


## process.getThreadPriority<sup>8+</sup>

getThreadPriority(v:number): number
Z
zengyawen 已提交
240 241 242

Obtains the thread priority based on the specified TID.

W
wusongqing 已提交
243
**Parameters**
Z
zengyawen 已提交
244

W
wusongqing 已提交
245 246 247
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
Z
zengyawen 已提交
248

W
wusongqing 已提交
249
**Return value**
Z
zengyawen 已提交
250

W
wusongqing 已提交
251 252 253
| Type| Description|
| -------- | -------- |
| number | Priority of the thread.|
Z
zengyawen 已提交
254

W
wusongqing 已提交
255
**Example**
Z
zengyawen 已提交
256

W
wusongqing 已提交
257 258 259 260
```
var tid = process.getTid();
var pres = process.getThreadPriority(tid);
```
Z
zengyawen 已提交
261 262


W
wusongqing 已提交
263
## process.getStartRealtime<sup>8+</sup>
Z
zengyawen 已提交
264

X
xdmal 已提交
265
getStartRealtime(): number
W
wusongqing 已提交
266 267

Obtains the duration, in milliseconds, from the time the system starts to the time the process starts.
Z
zengyawen 已提交
268

W
wusongqing 已提交
269 270 271 272 273 274 275 276 277 278 279
**Return value**

| Type| Description|
| -------- | -------- |
| number | Duration obtained.|

**Example**

```
var realtime = process.getStartRealtime();
```
Z
zengyawen 已提交
280

W
wusongqing 已提交
281
## process.getPastCputime<sup>8+</sup>
Z
zengyawen 已提交
282

X
xdmal 已提交
283
getPastCputime(): number
Z
zengyawen 已提交
284

W
wusongqing 已提交
285
Obtains the CPU time (in milliseconds) from the time the process starts to the current time.
Z
zengyawen 已提交
286

W
wusongqing 已提交
287
**Return value**
Z
zengyawen 已提交
288

W
wusongqing 已提交
289 290 291
| Type| Description|
| -------- | -------- |
| number | CPU time obtained.|
Z
zengyawen 已提交
292

W
wusongqing 已提交
293
**Example**
Z
zengyawen 已提交
294

W
wusongqing 已提交
295 296 297
```
var result = process.getPastCputime() ;
```
Z
zengyawen 已提交
298 299


W
wusongqing 已提交
300
## process.getSystemConfig<sup>8+</sup>
Z
zengyawen 已提交
301

W
wusongqing 已提交
302
getSystemConfig(name:number): number
Z
zengyawen 已提交
303 304 305

Obtains the system configuration.

W
wusongqing 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | number | Yes| System configuration parameter name.|

**Return value**

| Type| Description|
| -------- | -------- |
| number | System configuration obtained.|

**Example**

```
var _SC_ARG_MAX = 0
var pres = process.getSystemConfig(_SC_ARG_MAX)
```


## process.getEnvironmentVar<sup>8+</sup>

getEnvironmentVar(name:string): string
Z
zengyawen 已提交
329 330 331

Obtains the value of an environment variable.

W
wusongqing 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Environment variable name.|

**Return value**

| Type| Description|
| -------- | -------- |
| string | Value of the environment variable.|

**Example**

```
var pres = process.getEnvironmentVar("PATH")
```


## process.runCmd

runCmd(command: string, options?: { timeout : number, killSignal : number | string, maxBuffer : number }) : ChildProcess

Forks a new process to run a shell command and returns the **ChildProcess** object.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| command | string | Yes| Shell command to run.|
| options | Object | No| Related parameters.|

**Table 1** options

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| timeout | number | No| Maximum running time (in ms) of the child process. When the running time of the child process exceeds the value of this parameter, the parent process sends a **killSignal** to the child process to terminate it. The default value is **0**.|
| killSignal | number&nbsp;&nbsp;\|&nbsp;string | No| Signal sent to the child process when the running time of a child process exceeds the timeout period. The default value is **SIGTERM**.|
| maxBuffer | number | No| Maximum buffer size for the standard input and output of the child process. When the size is exceeded, the child process will be terminated. The default value is **1024 \* 1024**.|

**Return value**

| Type| Description|
| -------- | -------- |
| [ChildProcess](#childprocess) | **ChildProcess** object.|

**Example**

```
var child = process.runCmd('ls', { maxBuffer : 2 });
var result = child.wait();
child.getOutput.then(val=>{
    console.log("child.getOutput = " + val);
})
```


## process.abort

abort(): void
Z
zengyawen 已提交
392 393 394

Aborts a process and generates a core file. This method will cause a process to exit immediately. Exercise caution when using this method.

W
wusongqing 已提交
395
**Example**
Z
zengyawen 已提交
396

W
wusongqing 已提交
397 398 399
```
process.abort();
```
Z
zengyawen 已提交
400 401


W
wusongqing 已提交
402
## process.on
Z
zengyawen 已提交
403

W
wusongqing 已提交
404
on(type: string, listener: EventListener): void
Z
zengyawen 已提交
405 406 407

Stores the events triggered by the user.

W
wusongqing 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the events to store. |
| listener | EventListener | Yes| Callback invoked to return the event.|

**Table 2** EventListener

| Name| Description|
| -------- | -------- |
| EventListener&nbsp;=&nbsp;(evt:&nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|

**Example**

```
process.on("data", (e)=>{
    console.log("data callback");
})
```


## process.off

off(type: string): boolean
Z
zengyawen 已提交
433 434 435

Deletes the event stored by the user.

W
wusongqing 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to delete.|

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the event is deleted; returns **false** otherwise.|

**Example**

```
process.on("data", (e)=>{
    console.log("data callback");
})
var result = process.off("data");
```


## process.exit

exit(code: number): void
Z
zengyawen 已提交
461

Z
zengyawen 已提交
462
Terminates this process.
Z
zengyawen 已提交
463

W
wusongqing 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| code | number | Yes| Exit code of the process.|

**Example**

```
process.exit(0);
```


## process.cwd

cwd(): string
Z
zengyawen 已提交
480

Z
zengyawen 已提交
481
Obtains the working directory of this process.
Z
zengyawen 已提交
482

W
wusongqing 已提交
483
**Example**
Z
zengyawen 已提交
484

W
wusongqing 已提交
485 486 487
```
var path = process.cwd();
```
Z
zengyawen 已提交
488 489


W
wusongqing 已提交
490
## process.chdir
Z
zengyawen 已提交
491

W
wusongqing 已提交
492
chdir(dir: string): void
Z
zengyawen 已提交
493

Z
zengyawen 已提交
494
Changes the working directory of this process.
Z
zengyawen 已提交
495

W
wusongqing 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| dir | string | Yes| Path|

**Example**

```
process.chdir('/system');
```


## process.uptime

uptime(): number
Z
zengyawen 已提交
512

Z
zengyawen 已提交
513
Obtains the running time of this process.
Z
zengyawen 已提交
514

W
wusongqing 已提交
515
**Return value**
Z
zengyawen 已提交
516

W
wusongqing 已提交
517 518 519
| Type| Description|
| -------- | -------- |
| number | Running time of the process, in seconds.|
Z
zengyawen 已提交
520

W
wusongqing 已提交
521
**Example**
Z
zengyawen 已提交
522

W
wusongqing 已提交
523 524 525
```
var time = process.uptime();
```
Z
zengyawen 已提交
526 527


W
wusongqing 已提交
528
## process.kill
Z
zengyawen 已提交
529

W
wusongqing 已提交
530
kill(pid: number, signal: number): boolean
Z
zengyawen 已提交
531 532 533

Sends a signal to the specified process to terminate it.

W
wusongqing 已提交
534
**Parameters**
Z
zengyawen 已提交
535

W
wusongqing 已提交
536 537 538 539
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pid | number | Yes| PID of the process, to which the signal will be sent.|
| signal | number | Yes| Signal to send.|
Z
zengyawen 已提交
540

W
wusongqing 已提交
541 542 543 544 545 546 547 548 549 550 551
**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|

**Example**
```
var pres = process.pid
var result = that.kill(pres, 28)
```