Добрый день!
Как отследить, что несколько runShellCommand выполнилось?
Требуется вызвать три команды и по завершении всех трех выполнить одно действие.
Это возможно?
Добрый день!
Как отследить, что несколько runShellCommand выполнилось?
Требуется вызвать три команды и по завершении всех трех выполнить одно действие.
Это возможно?
Добрый день. Код возврата анализировать надо, так, например (обработка exitCode):
Ну и три команды можно объединить через &&
Не совсем удобно парсить код ответа от нескольких комманд через &&.
По поводу обработки exitCode не понял. Как это поможет дождаться внутри функции завершения runShellCommand?
В колбэк-функции есть параметр exitCode, в котором должент быть результат выполнения всех команд:
exitCallback: function (exitCode, capturedOutput, capturedErrorOutput) //Функция, в которую попадает вывод
Можно в асинхронном по своей природе JavaScript сделать синхронные вызовы через последовательность callback’ов.
Примерно так:
/*
* JavaScript sync run shell commands
*
* Just call next runShellCommand in current function's callback
*
*/
var shellCommand1 = "sleep 2",
shellCommand2 = "ping -c 4 8.8.8.8",
shellCommand3 = "echo WFT",
WBT = "@Wiren Board Team, " +
"send proper Priority/LogLevel from wb-rules to journald";
function runMainBlock () {
log.info("All shell command have run");
log.info("==========================");
log.info("= =");
log.info("= Have a nice day! =");
log.info("= =");
log.info("==========================");
}
function runThirdShellCommand () {
log.info("Run third command");
runShellCommand(
shellCommand3,
{
captureOutput: true,
captureError: true,
exitCallback: function (exitCode, capturedOutput, capturedErrorOutput) {
if (exitCode) {
log.error("Third command failed");
log.warning(WBT);
log.info(capturedErrorOutput);
} else {
log.info("Third command success");
log.info(capturedOutput);
runMainBlock();
}
}
}
);
}
function runSecondShellCommand () {
log.info("Run second command");
runShellCommand(
shellCommand2,
{
captureOutput: true,
captureError: true,
exitCallback: function (exitCode, capturedOutput, capturedErrorOutput) {
if (exitCode) {
log.error("Second command failed");
log.warning(WBT);
log.info(capturedErrorOutput);
} else {
log.info("Second command success");
log.info(capturedOutput);
runThirdShellCommand();
}
}
}
);
}
function runFirstShellCommand () {
log.info("Run first command");
runShellCommand(
shellCommand1,
{
captureOutput: true,
captureError: true,
exitCallback: function (exitCode, capturedOutput, capturedErrorOutput) {
if (exitCode) {
log.error("First command failed");
log.warning(WBT);
log.info(capturedErrorOutput);
} else {
log.info("First command success");
log.info(capturedOutput);
runSecondShellCommand();
}
}
}
);
}
runFirstShellCommand();