js-apis-process.md 12.3 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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

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

W
wusongqing 已提交
39 40 41 42 43 44 45 46 47 48 49
| 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 已提交
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
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
55
**Return value**
Z
zengyawen 已提交
56

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

W
wusongqing 已提交
61
**Example**
Z
zengyawen 已提交
62

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


W
wusongqing 已提交
72
### getOutput
Z
zengyawen 已提交
73

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

Obtains the standard output of the child process.

W
wusongqing 已提交
78 79
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
80
**Return value**
Z
zengyawen 已提交
81

W
wusongqing 已提交
82 83 84
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.|
Z
zengyawen 已提交
85

W
wusongqing 已提交
86
**Example**
Z
zengyawen 已提交
87

88
```js
W
wusongqing 已提交
89 90 91 92 93 94
var child = process.runCmd('ls');
var result = child.wait();
child.getOutput.then(val=>{
    console.log("child.getOutput = " + val);
})
```
Z
zengyawen 已提交
95 96


W
wusongqing 已提交
97
### getErrorOutput
Z
zengyawen 已提交
98

W
wusongqing 已提交
99
getErrorOutput(): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
100 101 102

Obtains the standard error output of the child process.

W
wusongqing 已提交
103 104
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
105
**Return value**
Z
zengyawen 已提交
106

W
wusongqing 已提交
107 108 109
| Type| Description|
| -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.|
Z
zengyawen 已提交
110

W
wusongqing 已提交
111
**Example**
Z
zengyawen 已提交
112

113
```js
W
wusongqing 已提交
114 115 116 117 118 119
var child = process.runCmd('madir test.text');
var result = child.wait();
child.getErrorOutput.then(val=>{
    console.log("child.getErrorOutput= " + val);
})
```
Z
zengyawen 已提交
120 121


W
wusongqing 已提交
122
### close
Z
zengyawen 已提交
123

X
xdmal 已提交
124
close(): void
Z
zengyawen 已提交
125 126 127

Closes the child process in running.

W
wusongqing 已提交
128 129
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
130
**Example**
Z
zengyawen 已提交
131

132
```js
W
wusongqing 已提交
133 134 135
var child = process.runCmd('sleep 5; ls');
child.close();
```
Z
zengyawen 已提交
136 137


W
wusongqing 已提交
138
### kill
Z
zengyawen 已提交
139

W
wusongqing 已提交
140
kill(signal: number | string): void
Z
zengyawen 已提交
141 142 143

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

W
wusongqing 已提交
144 145
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
146
**Parameters**
Z
zengyawen 已提交
147

W
wusongqing 已提交
148 149 150
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.|
Z
zengyawen 已提交
151

W
wusongqing 已提交
152
**Example**
Z
zengyawen 已提交
153

154
```js
W
wusongqing 已提交
155 156 157
var child = process.runCmd('sleep 5; ls');
child.kill(9);
```
Z
zengyawen 已提交
158

Z
zengyawen 已提交
159

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

W
wusongqing 已提交
162
isIsolatedProcess(): boolean
Z
zengyawen 已提交
163

W
wusongqing 已提交
164
Checks whether this process is isolated.
Z
zengyawen 已提交
165

W
wusongqing 已提交
166 167
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
168
**Return value**
Z
zengyawen 已提交
169

W
wusongqing 已提交
170 171 172
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
Z
zengyawen 已提交
173

W
wusongqing 已提交
174
**Example**
Z
zengyawen 已提交
175

176
```js
W
wusongqing 已提交
177 178
var result = process.isIsolatedProcess();
```
Z
zengyawen 已提交
179 180


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

X
xdmal 已提交
183
isAppUid(v: number): boolean
Z
zengyawen 已提交
184 185 186

Checks whether a UID belongs to this app.

W
wusongqing 已提交
187 188
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202
**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**

203
```js
W
wusongqing 已提交
204 205 206 207 208 209 210 211 212 213
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 已提交
214 215
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
216 217 218 219 220 221 222 223
**Return value**

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

**Example**

224
```js
W
wusongqing 已提交
225 226 227 228 229 230
var ressult = process.is64Bit();
```


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

X
xdmal 已提交
231
getUidForName(v: string): number
Z
zengyawen 已提交
232 233 234

Obtains the process UID based on the process name.

W
wusongqing 已提交
235 236
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250
**Parameters**

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

**Return value**

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

**Example**

251
```js
W
wusongqing 已提交
252 253 254 255 256 257
var pres = process.getUidForName("tool")
```


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

X
xdmal 已提交
258
getThreadPriority(v: number): number
Z
zengyawen 已提交
259 260 261

Obtains the thread priority based on the specified TID.

W
wusongqing 已提交
262 263
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
264
**Parameters**
Z
zengyawen 已提交
265

W
wusongqing 已提交
266 267 268
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
Z
zengyawen 已提交
269

W
wusongqing 已提交
270
**Return value**
Z
zengyawen 已提交
271

W
wusongqing 已提交
272 273 274
| Type| Description|
| -------- | -------- |
| number | Priority of the thread.|
Z
zengyawen 已提交
275

W
wusongqing 已提交
276
**Example**
Z
zengyawen 已提交
277

278
```js
W
wusongqing 已提交
279 280 281
var tid = process.getTid();
var pres = process.getThreadPriority(tid);
```
Z
zengyawen 已提交
282 283


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

X
xdmal 已提交
286
getStartRealtime(): number
W
wusongqing 已提交
287 288

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

W
wusongqing 已提交
290 291
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
292 293 294 295 296 297 298 299
**Return value**

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

**Example**

300
```js
W
wusongqing 已提交
301 302
var realtime = process.getStartRealtime();
```
Z
zengyawen 已提交
303

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

X
xdmal 已提交
306
getPastCpuTime(): number
Z
zengyawen 已提交
307

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

W
wusongqing 已提交
310 311
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
312
**Return value**
Z
zengyawen 已提交
313

W
wusongqing 已提交
314 315 316
| Type| Description|
| -------- | -------- |
| number | CPU time obtained.|
Z
zengyawen 已提交
317

W
wusongqing 已提交
318
**Example**
Z
zengyawen 已提交
319

320
```js
X
xdmal 已提交
321
var result = process.getPastCpuTime() ;
W
wusongqing 已提交
322
```
Z
zengyawen 已提交
323 324


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

X
xdmal 已提交
327
getSystemConfig(name: number): number
Z
zengyawen 已提交
328 329 330

Obtains the system configuration.

W
wusongqing 已提交
331 332
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346
**Parameters**

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

**Return value**

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

**Example**

347
```js
W
wusongqing 已提交
348 349 350 351 352 353 354
var _SC_ARG_MAX = 0
var pres = process.getSystemConfig(_SC_ARG_MAX)
```


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

X
xdmal 已提交
355
getEnvironmentVar(name: string): string
Z
zengyawen 已提交
356 357 358

Obtains the value of an environment variable.

W
wusongqing 已提交
359 360
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374
**Parameters**

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

**Return value**

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

**Example**

375
```js
W
wusongqing 已提交
376 377 378 379 380 381
var pres = process.getEnvironmentVar("PATH")
```


## process.runCmd

S
shikai-123 已提交
382
runCmd(command: string, options?: { timeout : number, killSignal : number | string, maxBuffer : number }): ChildProcess
W
wusongqing 已提交
383 384 385

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

W
wusongqing 已提交
386 387
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
**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**

411
```js
W
wusongqing 已提交
412 413 414 415 416 417 418 419 420 421 422
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 已提交
423 424 425

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

W
wusongqing 已提交
428
**Example**
Z
zengyawen 已提交
429

430
```js
W
wusongqing 已提交
431 432
process.abort();
```
Z
zengyawen 已提交
433 434


W
wusongqing 已提交
435
## process.on
Z
zengyawen 已提交
436

W
wusongqing 已提交
437
on(type: string, listener: EventListener): void
Z
zengyawen 已提交
438 439 440

Stores the events triggered by the user.

W
wusongqing 已提交
441 442
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
443 444 445 446 447 448 449 450 451 452 453
**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 已提交
454
| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|
W
wusongqing 已提交
455 456 457

**Example**

458
```js
W
wusongqing 已提交
459 460 461 462 463 464 465 466 467
process.on("data", (e)=>{
    console.log("data callback");
})
```


## process.off

off(type: string): boolean
Z
zengyawen 已提交
468 469 470

Deletes the event stored by the user.

W
wusongqing 已提交
471 472
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486
**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**

487
```js
W
wusongqing 已提交
488 489 490 491 492 493 494 495 496 497
process.on("data", (e)=>{
    console.log("data callback");
})
var result = process.off("data");
```


## process.exit

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

Z
zengyawen 已提交
499
Terminates this process.
Z
zengyawen 已提交
500

W
wusongqing 已提交
501 502 503 504
Exercise caution when using this API.

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

W
wusongqing 已提交
505 506 507 508 509 510 511 512
**Parameters**

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

**Example**

513
```js
W
wusongqing 已提交
514 515 516 517 518 519 520
process.exit(0);
```


## process.cwd

cwd(): string
Z
zengyawen 已提交
521

Z
zengyawen 已提交
522
Obtains the working directory of this process.
Z
zengyawen 已提交
523

W
wusongqing 已提交
524 525
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
526
**Example**
Z
zengyawen 已提交
527

528
```js
W
wusongqing 已提交
529 530
var path = process.cwd();
```
Z
zengyawen 已提交
531 532


W
wusongqing 已提交
533
## process.chdir
Z
zengyawen 已提交
534

W
wusongqing 已提交
535
chdir(dir: string): void
Z
zengyawen 已提交
536

Z
zengyawen 已提交
537
Changes the working directory of this process.
Z
zengyawen 已提交
538

W
wusongqing 已提交
539 540
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
541 542 543 544 545 546 547 548
**Parameters**

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

**Example**

549
```js
W
wusongqing 已提交
550 551 552 553 554 555 556
process.chdir('/system');
```


## process.uptime

uptime(): number
Z
zengyawen 已提交
557

Z
zengyawen 已提交
558
Obtains the running time of this process.
Z
zengyawen 已提交
559

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

W
wusongqing 已提交
562
**Return value**
Z
zengyawen 已提交
563

W
wusongqing 已提交
564 565 566
| Type| Description|
| -------- | -------- |
| number | Running time of the process, in seconds.|
Z
zengyawen 已提交
567

W
wusongqing 已提交
568
**Example**
Z
zengyawen 已提交
569

570
```js
W
wusongqing 已提交
571 572
var time = process.uptime();
```
Z
zengyawen 已提交
573 574


W
wusongqing 已提交
575
## process.kill
Z
zengyawen 已提交
576

S
shikai-123 已提交
577
kill(signal: number, pid: number): boolean
Z
zengyawen 已提交
578 579 580

Sends a signal to the specified process to terminate it.

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

W
wusongqing 已提交
583
**Parameters**
Z
zengyawen 已提交
584

W
wusongqing 已提交
585 586 587 588
| 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 已提交
589

W
wusongqing 已提交
590 591 592 593 594 595 596
**Return value**

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

**Example**
597
```js
W
wusongqing 已提交
598
var pres = process.pid
599
var result = that.kill(28, pres)
W
wusongqing 已提交
600
```