js-apis-process.md 13.7 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

## Attributes

W
wusongqing 已提交
16 17
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
18 19
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
20 21 22
| egid | number | Yes| No| Effective group identifier (EGID) of a process. This is a system API and cannot be called by third-party applications.|
| euid | number | Yes| No| Effective user identifier (EUID) of a process. This is a system API and cannot be called by third-party applications.|
| gid | number | Yes| No| Group identifier (GID) of a process. This is a system API and cannot be called by third-party applications.|
W
wusongqing 已提交
23
| uid | number | Yes| No| User identifier (UID) of a process.|
W
wusongqing 已提交
24
| groups | number[] | Yes| No| Array with supplementary group IDs. This is a system API and cannot be called by third-party applications.|
W
wusongqing 已提交
25
| pid | number | Yes| No| Process ID (PID) of a process.|
W
wusongqing 已提交
26
| ppid | number | Yes| No| Parent process ID (PPID) of a process. This is a system API and cannot be called by third-party applications.|
W
wusongqing 已提交
27 28 29 30 31 32 33 34 35 36
| 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

W
wusongqing 已提交
37 38
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
39 40
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
41 42 43 44
| pid | number | Yes| No| PID of the child process. This is a system API and cannot be called by third-party applications.|
| ppid | number | Yes| No| PPID of the child process. This is a system API and cannot be called by third-party applications.|
| exitCode | number | Yes| No| Exit code of the child process. This is a system API and cannot be called by third-party applications.|
| killed | boolean | Yes| No| Whether the parent process successfully sends a signal to the child process to terminate it. This is a system API and cannot be called by third-party applications.|
W
wusongqing 已提交
45 46 47 48 49


### wait

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

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

W
wusongqing 已提交
53 54
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
55 56
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
57
**Return value**
Z
zengyawen 已提交
58

W
wusongqing 已提交
59 60 61
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the exit code of the child process.|
Z
zengyawen 已提交
62

W
wusongqing 已提交
63
**Example**
Z
zengyawen 已提交
64

65
```js
W
wusongqing 已提交
66 67 68 69 70 71
var child = process.runCmd('ls');
var result = child.wait();
result.then(val=>{
    console.log("result = " + val);
})
```
Z
zengyawen 已提交
72 73


W
wusongqing 已提交
74
### getOutput
Z
zengyawen 已提交
75

W
wusongqing 已提交
76
getOutput(): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
77 78 79

Obtains the standard output of the child process.

W
wusongqing 已提交
80 81
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
82 83
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
84
**Return value**
Z
zengyawen 已提交
85

W
wusongqing 已提交
86 87 88
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.|
Z
zengyawen 已提交
89

W
wusongqing 已提交
90
**Example**
Z
zengyawen 已提交
91

92
```js
W
wusongqing 已提交
93 94
var child = process.runCmd('ls');
var result = child.wait();
S
shikai-123 已提交
95
child.getOutput().then(val=>{
W
wusongqing 已提交
96 97 98
    console.log("child.getOutput = " + val);
})
```
Z
zengyawen 已提交
99 100


W
wusongqing 已提交
101
### getErrorOutput
Z
zengyawen 已提交
102

W
wusongqing 已提交
103
getErrorOutput(): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
104 105 106

Obtains the standard error output of the child process.

W
wusongqing 已提交
107 108
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
109 110
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
111
**Return value**
Z
zengyawen 已提交
112

W
wusongqing 已提交
113 114 115
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.|
Z
zengyawen 已提交
116

W
wusongqing 已提交
117
**Example**
Z
zengyawen 已提交
118

119
```js
W
wusongqing 已提交
120 121
var child = process.runCmd('madir test.text');
var result = child.wait();
S
shikai-123 已提交
122
child.getErrorOutput().then(val=>{
W
wusongqing 已提交
123 124 125
    console.log("child.getErrorOutput= " + val);
})
```
Z
zengyawen 已提交
126 127


W
wusongqing 已提交
128
### close
Z
zengyawen 已提交
129

X
xdmal 已提交
130
close(): void
Z
zengyawen 已提交
131 132 133

Closes the child process in running.

W
wusongqing 已提交
134 135
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
136 137
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
138
**Example**
Z
zengyawen 已提交
139

140
```js
W
wusongqing 已提交
141 142 143
var child = process.runCmd('sleep 5; ls');
child.close();
```
Z
zengyawen 已提交
144 145


W
wusongqing 已提交
146
### kill
Z
zengyawen 已提交
147

W
wusongqing 已提交
148
kill(signal: number | string): void
Z
zengyawen 已提交
149 150 151

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

W
wusongqing 已提交
152 153
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
154 155
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
156
**Parameters**
Z
zengyawen 已提交
157

W
wusongqing 已提交
158 159 160
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.|
Z
zengyawen 已提交
161

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

164
```js
W
wusongqing 已提交
165 166 167
var child = process.runCmd('sleep 5; ls');
child.kill(9);
```
Z
zengyawen 已提交
168

Z
zengyawen 已提交
169

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

W
wusongqing 已提交
172
isIsolatedProcess(): boolean
Z
zengyawen 已提交
173

W
wusongqing 已提交
174
Checks whether this process is isolated.
Z
zengyawen 已提交
175

W
wusongqing 已提交
176 177
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
178
**Return value**
Z
zengyawen 已提交
179

W
wusongqing 已提交
180 181 182
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
Z
zengyawen 已提交
183

W
wusongqing 已提交
184
**Example**
Z
zengyawen 已提交
185

186
```js
W
wusongqing 已提交
187 188
var result = process.isIsolatedProcess();
```
Z
zengyawen 已提交
189 190


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

X
xdmal 已提交
193
isAppUid(v: number): boolean
Z
zengyawen 已提交
194 195 196

Checks whether a UID belongs to this app.

W
wusongqing 已提交
197 198
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
**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**

213
```js
W
wusongqing 已提交
214 215 216 217 218 219 220 221 222 223
var result = process.isAppUid(688);
```


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

is64Bit(): boolean

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

W
wusongqing 已提交
224 225
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
226 227 228 229 230 231 232 233
**Return value**

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

**Example**

234
```js
N
Neil Chen 已提交
235
var result = process.is64Bit();
W
wusongqing 已提交
236 237 238 239 240
```


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

X
xdmal 已提交
241
getUidForName(v: string): number
Z
zengyawen 已提交
242 243 244

Obtains the process UID based on the process name.

W
wusongqing 已提交
245 246
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
**Parameters**

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

**Return value**

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

**Example**

261
```js
W
wusongqing 已提交
262 263 264 265 266 267
var pres = process.getUidForName("tool")
```


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

X
xdmal 已提交
268
getThreadPriority(v: number): number
Z
zengyawen 已提交
269 270 271

Obtains the thread priority based on the specified TID.

W
wusongqing 已提交
272 273
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
274
**Parameters**
Z
zengyawen 已提交
275

W
wusongqing 已提交
276 277 278
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
Z
zengyawen 已提交
279

W
wusongqing 已提交
280
**Return value**
Z
zengyawen 已提交
281

W
wusongqing 已提交
282 283 284
| Type| Description|
| -------- | -------- |
| number | Priority of the thread.|
Z
zengyawen 已提交
285

W
wusongqing 已提交
286
**Example**
Z
zengyawen 已提交
287

288
```js
S
shikai-123 已提交
289
var tid = process.tid;
W
wusongqing 已提交
290 291
var pres = process.getThreadPriority(tid);
```
Z
zengyawen 已提交
292 293


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

X
xdmal 已提交
296
getStartRealtime(): number
W
wusongqing 已提交
297 298

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

W
wusongqing 已提交
300 301
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
302 303 304 305 306 307 308 309
**Return value**

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

**Example**

310
```js
W
wusongqing 已提交
311 312
var realtime = process.getStartRealtime();
```
Z
zengyawen 已提交
313

X
xdmal 已提交
314
## process.getPastCpuTime<sup>8+</sup>
Z
zengyawen 已提交
315

X
xdmal 已提交
316
getPastCpuTime(): number
Z
zengyawen 已提交
317

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

W
wusongqing 已提交
320 321
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
322
**Return value**
Z
zengyawen 已提交
323

W
wusongqing 已提交
324 325 326
| Type| Description|
| -------- | -------- |
| number | CPU time obtained.|
Z
zengyawen 已提交
327

W
wusongqing 已提交
328
**Example**
Z
zengyawen 已提交
329

330
```js
X
xdmal 已提交
331
var result = process.getPastCpuTime() ;
W
wusongqing 已提交
332
```
Z
zengyawen 已提交
333 334


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

X
xdmal 已提交
337
getSystemConfig(name: number): number
Z
zengyawen 已提交
338 339 340

Obtains the system configuration.

W
wusongqing 已提交
341 342
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356
**Parameters**

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

**Return value**

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

**Example**

357
```js
W
wusongqing 已提交
358 359 360 361 362 363 364
var _SC_ARG_MAX = 0
var pres = process.getSystemConfig(_SC_ARG_MAX)
```


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

X
xdmal 已提交
365
getEnvironmentVar(name: string): string
Z
zengyawen 已提交
366 367 368

Obtains the value of an environment variable.

W
wusongqing 已提交
369 370
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384
**Parameters**

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

**Return value**

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

**Example**

385
```js
W
wusongqing 已提交
386 387 388 389 390 391
var pres = process.getEnvironmentVar("PATH")
```


## process.runCmd

S
shikai-123 已提交
392
runCmd(command: string, options?: { timeout : number, killSignal : number | string, maxBuffer : number }): ChildProcess
W
wusongqing 已提交
393 394 395

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

W
wusongqing 已提交
396 397
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
398 399
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
**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**

423
```js
W
wusongqing 已提交
424 425 426 427 428 429 430 431 432 433 434
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 已提交
435 436 437

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 已提交
438 439
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
440
**Example**
Z
zengyawen 已提交
441

442
```js
W
wusongqing 已提交
443 444
process.abort();
```
Z
zengyawen 已提交
445 446


W
wusongqing 已提交
447
## process.on
Z
zengyawen 已提交
448

W
wusongqing 已提交
449
on(type: string, listener: EventListener): void
Z
zengyawen 已提交
450 451 452

Stores the events triggered by the user.

W
wusongqing 已提交
453 454
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
455 456
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
457 458 459 460 461 462 463 464 465 466 467
**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|
| -------- | -------- |
X
xdmal 已提交
468
| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|
W
wusongqing 已提交
469 470 471

**Example**

472
```js
W
wusongqing 已提交
473 474 475 476 477 478 479 480 481
process.on("data", (e)=>{
    console.log("data callback");
})
```


## process.off

off(type: string): boolean
Z
zengyawen 已提交
482 483 484

Deletes the event stored by the user.

W
wusongqing 已提交
485 486
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
487 488
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502
**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**

503
```js
W
wusongqing 已提交
504 505 506 507 508 509 510 511 512 513
process.on("data", (e)=>{
    console.log("data callback");
})
var result = process.off("data");
```


## process.exit

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

Z
zengyawen 已提交
515
Terminates this process.
Z
zengyawen 已提交
516

W
wusongqing 已提交
517 518 519 520
Exercise caution when using this API.

**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
521 522 523 524 525 526 527 528
**Parameters**

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

**Example**

529
```js
W
wusongqing 已提交
530 531 532 533 534 535 536
process.exit(0);
```


## process.cwd

cwd(): string
Z
zengyawen 已提交
537

Z
zengyawen 已提交
538
Obtains the working directory of this process.
Z
zengyawen 已提交
539

W
wusongqing 已提交
540 541
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
542 543
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
544
**Example**
Z
zengyawen 已提交
545

546
```js
W
wusongqing 已提交
547 548
var path = process.cwd();
```
Z
zengyawen 已提交
549 550


W
wusongqing 已提交
551
## process.chdir
Z
zengyawen 已提交
552

W
wusongqing 已提交
553
chdir(dir: string): void
Z
zengyawen 已提交
554

Z
zengyawen 已提交
555
Changes the working directory of this process.
Z
zengyawen 已提交
556

W
wusongqing 已提交
557 558
This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
559 560
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
561 562 563 564 565 566 567 568
**Parameters**

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

**Example**

569
```js
W
wusongqing 已提交
570 571 572 573 574 575 576
process.chdir('/system');
```


## process.uptime

uptime(): number
Z
zengyawen 已提交
577

Z
zengyawen 已提交
578
Obtains the running time of this process.
Z
zengyawen 已提交
579

W
wusongqing 已提交
580 581
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
582
**Return value**
Z
zengyawen 已提交
583

W
wusongqing 已提交
584 585 586
| Type| Description|
| -------- | -------- |
| number | Running time of the process, in seconds.|
Z
zengyawen 已提交
587

W
wusongqing 已提交
588
**Example**
Z
zengyawen 已提交
589

590
```js
W
wusongqing 已提交
591 592
var time = process.uptime();
```
Z
zengyawen 已提交
593 594


W
wusongqing 已提交
595
## process.kill
Z
zengyawen 已提交
596

S
shikai-123 已提交
597
kill(signal: number, pid: number): boolean
Z
zengyawen 已提交
598 599 600

Sends a signal to the specified process to terminate it.

W
wusongqing 已提交
601 602
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
603
**Parameters**
Z
zengyawen 已提交
604

W
wusongqing 已提交
605 606 607 608
| 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 已提交
609

W
wusongqing 已提交
610 611 612 613 614 615 616
**Return value**

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

**Example**
W
wusongqing 已提交
617

618
```js
W
wusongqing 已提交
619
var pres = process.pid
S
shikai-123 已提交
620
var result = process.kill(28, pres)
W
wusongqing 已提交
621
```