'nodejs execSync failing on command that runs in terminal
I'm trying to issue commands to stop and start a service from a nodejs app and while I can run some commands like "ls" or "pidof java" and get the expected response, when I try to run the following:
const minecraft = execSync('systemctl status minecraft').toString('utf8');
I get the following error:
Error: Command failed: systemctl status minecraft
at checkExecSyncError (node:child_process:826:11)
at execSync (node:child_process:900:15)
at Client.<anonymous> (/var/www/dekobot/bot.js:135:23)
at Client.emit (node:events:390:28)
at MessageCreateAction.handle (/var/www/dekobot/node_modules/discord.js/src/client/actions/MessageCreate.js:25:14)
at Object.module.exports [as MESSAGE_CREATE] (/var/www/dekobot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/var/www/dekobot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:350:31)
at WebSocketShard.onPacket (/var/www/dekobot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:443:22)
at WebSocketShard.onMessage (/var/www/dekobot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:300:10)
at WebSocket.onMessage (/var/www/dekobot/node_modules/discord.js/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:390:28)
at Receiver.receiverOnMessage (/var/www/dekobot/node_modules/discord.js/node_modules/ws/lib/websocket.js:1093:20)
at Receiver.emit (node:events:390:28)
at Receiver.dataMessage (/var/www/dekobot/node_modules/discord.js/node_modules/ws/lib/receiver.js:522:14)
at Receiver.getData (/var/www/dekobot/node_modules/discord.js/node_modules/ws/lib/receiver.js:440:17)
at Receiver.startLoop (/var/www/dekobot/node_modules/discord.js/node_modules/ws/lib/receiver.js:148:22) {
status: 3,
signal: null,
output: [
null,
<Buffer 2a 20 6d 69 6e 65 63 72 61 66 74 2e 73 65 72 76 69 63 65 20 2d 20 44 65 6b 6f 43 72 61 66 74 20 53 4d 50 20 28 31 2e 31 38 2e 31 29 0a 20 20 20 20 20 ... 2133 more bytes>,
<Buffer >
],
pid: 6713,
stdout: <Buffer 2a 20 6d 69 6e 65 63 72 61 66 74 2e 73 65 72 76 69 63 65 20 2d 20 44 65 6b 6f 43 72 61 66 74 20 53 4d 50 20 28 31 2e 31 38 2e 31 29 0a 20 20 20 20 20 ... 2133 more bytes>,
stderr: <Buffer >
}
I'm a bit lost at this point as I'm not sure why I can run some commands but not others.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
