• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Process Information
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
4> 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.
5
6
7## Modules to Import
8
9```
10import process from '@ohos.process';
11```
12
13
14## Attributes
15
16**System capability**: SystemCapability.Utils.Lang
17
18| Name| Type| Readable| Writable| Description|
19| -------- | -------- | -------- | -------- | -------- |
20| egid | number | Yes| No| Effective group identifier (EGID) of the process. This is a system API and cannot be called by third-party applications.|
21| euid | number | Yes| No| Effective user identifier (EUID) of the process. This is a system API and cannot be called by third-party applications.|
22| gid | number | Yes| No| Group identifier (GID) of the process. This is a system API and cannot be called by third-party applications.|
23| uid | number | Yes| No| User identifier (UID) of the process.|
24| groups | number[] | Yes| No| Array with supplementary group IDs. This is a system API and cannot be called by third-party applications.|
25| pid | number | Yes| No| Process ID (PID) of the process.|
26| ppid | number | Yes| No| Parent process ID (PPID) of the process. This is a system API and cannot be called by third-party applications.|
27| tid<sup>8+</sup> | number | Yes| No| Thread ID (TID) of the thread.|
28
29
30## ChildProcess
31
32Allows a process to obtain the standard input and output of its child processes, send signals, and close its child processes.
33
34
35### Attributes
36
37**System capability**: SystemCapability.Utils.Lang
38
39| Name| Type| Readable| Writable| Description|
40| -------- | -------- | -------- | -------- | -------- |
41| pid | number | Yes| No| PID of the child process. This is a system API and cannot be called by third-party applications.|
42| ppid | number | Yes| No| PPID of the child process. This is a system API and cannot be called by third-party applications.|
43| exitCode | number | Yes| No| Exit code of the child process. This is a system API and cannot be called by third-party applications.|
44| 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.|
45
46
47### wait
48
49wait(): Promise&lt;number&gt;
50
51Waits until the child process ends. This method uses a promise to return the exit code of the child process.
52
53This is a system API and cannot be called by third-party applications.
54
55**System capability**: SystemCapability.Utils.Lang
56
57**Return value**
58
59| Type| Description|
60| -------- | -------- |
61| Promise&lt;number&gt; | Promise used to return the exit code of the child process.|
62
63**Example**
64
65```js
66var child = process.runCmd('ls');
67var result = child.wait();
68result.then(val=>{
69    console.log("result = " + val);
70})
71```
72
73
74### getOutput
75
76getOutput(): Promise&lt;Uint8Array&gt;
77
78Obtains the standard output of the child process.
79
80This is a system API and cannot be called by third-party applications.
81
82**System capability**: SystemCapability.Utils.Lang
83
84**Return value**
85
86| Type| Description|
87| -------- | -------- |
88| Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.|
89
90**Example**
91
92```js
93var child = process.runCmd('ls');
94var result = child.wait();
95child.getOutput.then(val=>{
96    console.log("child.getOutput = " + val);
97})
98```
99
100
101### getErrorOutput
102
103getErrorOutput(): Promise&lt;Uint8Array&gt;
104
105Obtains the standard error output of the child process.
106
107This is a system API and cannot be called by third-party applications.
108
109**System capability**: SystemCapability.Utils.Lang
110
111**Return value**
112
113| Type| Description|
114| -------- | -------- |
115| Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.|
116
117**Example**
118
119```js
120var child = process.runCmd('madir test.text');
121var result = child.wait();
122child.getErrorOutput.then(val=>{
123    console.log("child.getErrorOutput= " + val);
124})
125```
126
127
128### close
129
130close(): void
131
132Closes the child process in running.
133
134This is a system API and cannot be called by third-party applications.
135
136**System capability**: SystemCapability.Utils.Lang
137
138**Example**
139
140```js
141var child = process.runCmd('sleep 5; ls');
142child.close();
143```
144
145
146### kill
147
148kill(signal: number | string): void
149
150Sends a signal to the specified child process to terminate it.
151
152This is a system API and cannot be called by third-party applications.
153
154**System capability**: SystemCapability.Utils.Lang
155
156**Parameters**
157
158| Name| Type| Mandatory| Description|
159| -------- | -------- | -------- | -------- |
160| signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.|
161
162**Example**
163
164```js
165var child = process.runCmd('sleep 5; ls');
166child.kill(9);
167```
168
169
170## process.isIsolatedProcess<sup>8+</sup>
171
172isIsolatedProcess(): boolean
173
174Checks whether this process is isolated.
175
176**System capability**: SystemCapability.Utils.Lang
177
178**Return value**
179
180| Type| Description|
181| -------- | -------- |
182| boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
183
184**Example**
185
186```js
187var result = process.isIsolatedProcess();
188```
189
190
191## process.isAppUid<sup>8+</sup>
192
193isAppUid(v: number): boolean
194
195Checks whether a UID belongs to this app.
196
197**System capability**: SystemCapability.Utils.Lang
198
199**Parameters**
200
201| Name| Type| Mandatory| Description|
202| -------- | -------- | -------- | -------- |
203| v | number | Yes| UID.|
204
205**Return value**
206
207| Type| Description|
208| -------- | -------- |
209| boolean | Returns **true** if the UID is the app's UID; returns **false** otherwise.|
210
211**Example**
212
213```js
214var result = process.isAppUid(688);
215```
216
217
218## process.is64Bit<sup>8+</sup>
219
220is64Bit(): boolean
221
222Checks whether this process is running in a 64-bit environment.
223
224**System capability**: SystemCapability.Utils.Lang
225
226**Return value**
227
228| Type| Description|
229| -------- | -------- |
230| boolean | Returns **true** if the process is running in a 64-bit environment; returns **false** otherwise.|
231
232**Example**
233
234```js
235var ressult = process.is64Bit();
236```
237
238
239## process.getUidForName<sup>8+</sup>
240
241getUidForName(v: string): number
242
243Obtains the process UID based on the process name.
244
245**System capability**: SystemCapability.Utils.Lang
246
247**Parameters**
248
249| Name| Type| Mandatory| Description|
250| -------- | -------- | -------- | -------- |
251| v | string | Yes| Name of a process.|
252
253**Return value**
254
255| Type| Description|
256| -------- | -------- |
257| number | Process UID.|
258
259**Example**
260
261```js
262var pres = process.getUidForName("tool")
263```
264
265
266## process.getThreadPriority<sup>8+</sup>
267
268getThreadPriority(v: number): number
269
270Obtains the thread priority based on the specified TID.
271
272**System capability**: SystemCapability.Utils.Lang
273
274**Parameters**
275
276| Name| Type| Mandatory| Description|
277| -------- | -------- | -------- | -------- |
278| v | number | Yes| TID.|
279
280**Return value**
281
282| Type| Description|
283| -------- | -------- |
284| number | Priority of the thread.|
285
286**Example**
287
288```js
289var tid = process.getTid();
290var pres = process.getThreadPriority(tid);
291```
292
293
294## process.getStartRealtime<sup>8+</sup>
295
296getStartRealtime(): number
297
298Obtains the duration, in milliseconds, from the time the system starts to the time the process starts.
299
300**System capability**: SystemCapability.Utils.Lang
301
302**Return value**
303
304| Type| Description|
305| -------- | -------- |
306| number | Duration obtained.|
307
308**Example**
309
310```js
311var realtime = process.getStartRealtime();
312```
313
314## process.getPastCpuTime<sup>8+</sup>
315
316getPastCpuTime(): number
317
318Obtains the CPU time (in milliseconds) from the time the process starts to the current time.
319
320**System capability**: SystemCapability.Utils.Lang
321
322**Return value**
323
324| Type| Description|
325| -------- | -------- |
326| number | CPU time obtained.|
327
328**Example**
329
330```js
331var result = process.getPastCpuTime() ;
332```
333
334
335## process.getSystemConfig<sup>8+</sup>
336
337getSystemConfig(name: number): number
338
339Obtains the system configuration.
340
341**System capability**: SystemCapability.Utils.Lang
342
343**Parameters**
344
345| Name| Type| Mandatory| Description|
346| -------- | -------- | -------- | -------- |
347| name | number | Yes| System configuration parameter name.|
348
349**Return value**
350
351| Type| Description|
352| -------- | -------- |
353| number | System configuration obtained.|
354
355**Example**
356
357```js
358var _SC_ARG_MAX = 0
359var pres = process.getSystemConfig(_SC_ARG_MAX)
360```
361
362
363## process.getEnvironmentVar<sup>8+</sup>
364
365getEnvironmentVar(name: string): string
366
367Obtains the value of an environment variable.
368
369**System capability**: SystemCapability.Utils.Lang
370
371**Parameters**
372
373| Name| Type| Mandatory| Description|
374| -------- | -------- | -------- | -------- |
375| name | string | Yes| Environment variable name.|
376
377**Return value**
378
379| Type| Description|
380| -------- | -------- |
381| string | Value of the environment variable.|
382
383**Example**
384
385```js
386var pres = process.getEnvironmentVar("PATH")
387```
388
389
390## process.runCmd
391
392runCmd(command: string, options?: { timeout : number, killSignal : number | string, maxBuffer : number }): ChildProcess
393
394Forks a new process to run a shell command and returns the **ChildProcess** object.
395
396This is a system API and cannot be called by third-party applications.
397
398**System capability**: SystemCapability.Utils.Lang
399
400**Parameters**
401
402| Name| Type| Mandatory| Description|
403| -------- | -------- | -------- | -------- |
404| command | string | Yes| Shell command to run.|
405| options | Object | No| Related parameters.|
406
407**Table 1** options
408
409| Name| Type| Mandatory| Description|
410| -------- | -------- | -------- | -------- |
411| 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**.|
412| 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**.|
413| 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**.|
414
415**Return value**
416
417| Type| Description|
418| -------- | -------- |
419| [ChildProcess](#childprocess) | **ChildProcess** object.|
420
421**Example**
422
423```js
424var child = process.runCmd('ls', { maxBuffer : 2 });
425var result = child.wait();
426child.getOutput.then(val=>{
427    console.log("child.getOutput = " + val);
428})
429```
430
431
432## process.abort
433
434abort(): void
435
436Aborts a process and generates a core file. This method will cause a process to exit immediately. Exercise caution when using this method.
437
438**System capability**: SystemCapability.Utils.Lang
439
440**Example**
441
442```js
443process.abort();
444```
445
446
447## process.on
448
449on(type: string, listener: EventListener): void
450
451Stores the events triggered by the user.
452
453This is a system API and cannot be called by third-party applications.
454
455**System capability**: SystemCapability.Utils.Lang
456
457**Parameters**
458
459| Name| Type| Mandatory| Description|
460| -------- | -------- | -------- | -------- |
461| type | string | Yes| Type of the events to store. |
462| listener | EventListener | Yes| Callback invoked to return the event.|
463
464**Table 2** EventListener
465
466| Name| Description|
467| -------- | -------- |
468| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|
469
470**Example**
471
472```js
473process.on("data", (e)=>{
474    console.log("data callback");
475})
476```
477
478
479## process.off
480
481off(type: string): boolean
482
483Deletes the event stored by the user.
484
485This is a system API and cannot be called by third-party applications.
486
487**System capability**: SystemCapability.Utils.Lang
488
489**Parameters**
490
491| Name| Type| Mandatory| Description|
492| -------- | -------- | -------- | -------- |
493| type | string | Yes| Type of the event to delete.|
494
495**Return value**
496
497| Type| Description|
498| -------- | -------- |
499| boolean | Returns **true** if the event is deleted; returns **false** otherwise.|
500
501**Example**
502
503```js
504process.on("data", (e)=>{
505    console.log("data callback");
506})
507var result = process.off("data");
508```
509
510
511## process.exit
512
513exit(code: number): void
514
515Terminates this process.
516
517Exercise caution when using this API.
518
519**System capability**: SystemCapability.Utils.Lang
520
521**Parameters**
522
523| Name| Type| Mandatory| Description|
524| -------- | -------- | -------- | -------- |
525| code | number | Yes| Exit code of the process.|
526
527**Example**
528
529```js
530process.exit(0);
531```
532
533
534## process.cwd
535
536cwd(): string
537
538Obtains the working directory of this process.
539
540This is a system API and cannot be called by third-party applications.
541
542**System capability**: SystemCapability.Utils.Lang
543
544**Example**
545
546```js
547var path = process.cwd();
548```
549
550
551## process.chdir
552
553chdir(dir: string): void
554
555Changes the working directory of this process.
556
557This is a system API and cannot be called by third-party applications.
558
559**System capability**: SystemCapability.Utils.Lang
560
561**Parameters**
562
563| Name| Type| Mandatory| Description|
564| -------- | -------- | -------- | -------- |
565| dir | string | Yes| Path|
566
567**Example**
568
569```js
570process.chdir('/system');
571```
572
573
574## process.uptime
575
576uptime(): number
577
578Obtains the running time of this process.
579
580**System capability**: SystemCapability.Utils.Lang
581
582**Return value**
583
584| Type| Description|
585| -------- | -------- |
586| number | Running time of the process, in seconds.|
587
588**Example**
589
590```js
591var time = process.uptime();
592```
593
594
595## process.kill
596
597kill(signal: number, pid: number): boolean
598
599Sends a signal to the specified process to terminate it.
600
601**System capability**: SystemCapability.Utils.Lang
602
603**Parameters**
604
605| Name| Type| Mandatory| Description|
606| -------- | -------- | -------- | -------- |
607| pid | number | Yes| PID of the process, to which the signal will be sent.|
608| signal | number | Yes| Signal to send.|
609
610**Return value**
611
612| Type| Description|
613| -------- | -------- |
614| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
615
616**Example**
617
618```js
619var pres = process.pid
620var result = that.kill(28, pres)
621```
622