luci: Fix func execAndRead

This commit is contained in:
remittor
2026-01-31 23:09:12 +03:00
parent 1f784b5773
commit b0fc623183
2 changed files with 28 additions and 16 deletions

View File

@@ -809,11 +809,23 @@ return baseclass.extend({
{ {
ticks += 1; ticks += 1;
try { try {
if (retCode < 0) {
let rc = await fs.exec('/bin/cat', [ rcFile ], null);
if (rc.code != 0) {
fixLogEnd();
resolve(callback.call(ctx, 542, 'ERROR: cannot read file "' + rcFile + '"'));
return;
}
if (rc.stdout) {
retCode = parseInt(rc.stdout.trim(), 10);
}
}
let res = await fs.exec('/bin/cat', [ logFile ], null); let res = await fs.exec('/bin/cat', [ logFile ], null);
if (res.code != 0) { if (res.code != 0) {
if (ticks > 1) { if (ticks > 1) {
console.log('ERROR: execAndRead: '+JSON.stringify(opt_list)); console.log('ERROR: execAndRead: '+JSON.stringify(opt_list));
resolve(callback.call(ctx, 541, 'ERROR: Failed on read process log: code = ' + res.code)); fixLogEnd();
resolve(callback.call(ctx, 546, 'ERROR: Failed on read process log: code = ' + res.code));
return; return;
} }
setTimeout(epoll, 500); setTimeout(epoll, 500);
@@ -831,17 +843,6 @@ return baseclass.extend({
appendLog(log, ''); appendLog(log, '');
lastLen = res.stdout.length; lastLen = res.stdout.length;
} }
if (retCode < 0) {
let rc = await fs.exec('/bin/cat', [ rcFile ], null);
if (rc.code != 0) {
fixLogEnd();
resolve(callback.call(ctx, 545, 'ERROR: cannot read file "' + rcFile + '"'));
return;
}
if (rc.stdout) {
retCode = parseInt(rc.stdout.trim(), 10);
}
}
if (retCode >= 0) { if (retCode >= 0) {
fixLogEnd(); fixLogEnd();
if (retCode == 0 && res.stdout) { if (retCode == 0 && res.stdout) {

View File

@@ -1,13 +1,18 @@
#!/bin/sh #!/bin/sh
# Copyright (c) 2024 remittor # Copyright (c) 2024 remittor
awk -V
PID_FILE=/tmp/zapret-script-exec.pid PID_FILE=/tmp/zapret-script-exec.pid
[ -f $PID_FILE ] && exit 70 if [ -f $PID_FILE ]; then
echo "ERROR: file $PID_FILE already exists!" | awk 'NR==1'
exit 70
fi
LOG_FILE=$1 LOG_FILE=$1
RC_FILE=$1.rc RC_FILE=$1.rc
SH_FILE=$2 SH_FILE=$2
shift 2 shift 2
[ ! -f $SH_FILE ] && exit 71 if [ ! -f $SH_FILE ]; then
echo "ERROR: script $SH_FILE not found!" | awk 'NR==1'
exit 71
fi
: > $LOG_FILE : > $LOG_FILE
: > $RC_FILE : > $RC_FILE
start-stop-daemon -S -b -p $PID_FILE -x /bin/sh -- -c ' start-stop-daemon -S -b -p $PID_FILE -x /bin/sh -- -c '
@@ -17,7 +22,13 @@ start-stop-daemon -S -b -p $PID_FILE -x /bin/sh -- -c '
shift 3 shift 3
sh $SH_FILE "$@" > $LOG_FILE 2>&1 sh $SH_FILE "$@" > $LOG_FILE 2>&1
RET_CODE=$? RET_CODE=$?
sleep 1 wc -l $LOG_FILE >/dev/null
echo $RET_CODE > $RC_FILE echo $RET_CODE > $RC_FILE
' sh $LOG_FILE $RC_FILE $SH_FILE "$@" ' sh $LOG_FILE $RC_FILE $SH_FILE "$@"
RET_CODE=$?
if [ $RET_CODE != 0 ]; then
echo "ERROR: script $SH_FILE not executed! ret_code = $RET_CODE" | awk 'NR==1'
exit $RET_CODE
fi
echo "Script $SH_FILE running..." | awk 'NR==1'
exit 0 exit 0