简体   繁体   中英

Childprocess.exec function giving an error when service is inactive

I am using CentOS 7 server, node version 10.23.0, child-process version 6.14.9 and I should watch the status of the given services.

For that, I'm using Childprocess.exec function in format systemctl status servicename . This command is working properly when service is active, but giving an error when service is inactive. In command line, despite status of the service, all is working good.

I've tried to use systemctl is-active servicename but there is also error. I don't know what is the reason. Error message is

{Error: Command failed: systemctl status crond
   at ChildProcess.exithandler (child_process.js:294:12)
   at ChildProcess.emit (events.js:198:13)
   at maybeClose (internal/child_process.js:982:16)
   at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
 killed: false,
 code: 3,
 signal: null,
 cmd: 'systemctl status crond' }

NOTE: I should use child-process.

Systemctl's exit codes are documented in man systemctl as:

EXIT STATUS
       On success, 0 is returned, a non-zero failure code otherwise.

       systemctl uses the return codes defined by LSB, as defined in LSB 3.0.0[2].

       Table 3. LSB return codes
       ┌──────┬───────────────────────────┬──────────────────────────┐
       │Value │ Description in LSB        │ Use in systemd           │
       ├──────┼───────────────────────────┼──────────────────────────┤
       │0     │ "program is running or    │ unit is active           │
       │      │ service is OK"            │                          │
       ├──────┼───────────────────────────┼──────────────────────────┤
       │1     │ "program is dead and      │ unit not failed (used by │
       │      │ /var/run pid file exists" │ is-failed)               │
       ├──────┼───────────────────────────┼──────────────────────────┤
       │2     │ "program is dead and      │ unused                   │
       │      │ /var/lock lock file       │                          │
       │      │ exists"                   │                          │
       ├──────┼───────────────────────────┼──────────────────────────┤
       │3     │ "program is not running"  │ unit is not active       │
       ├──────┼───────────────────────────┼──────────────────────────┤
       │4     │ "program or service       │ no such unit             │
       │      │ status is unknown"        │                          │

In your output you have code: 3 , so it's telling you what you already know, that the service is not active, but since it is exiting with a non-zero code exec() thinks it is an error.

When you say it runs fine on the command line, it's actually operating the exact same way, but you wouldn't notice the exit code was 3 unless you checked the variable $? afterwards.

You can parse the error in your callback against systemctl's documented exit codes to determine if it was an actual error or not given your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM