K
K
kovalr2020-09-08 20:00:13
bash
kovalr, 2020-09-08 20:00:13

Why doesn't bash show a variable inside echo when run from Cron?

There is a script

#!/bin/bash 

RESULT=$(/usr/local/bin/wp core verify-checksums --allow-root --path=/var/www/web/)
IS_OK=${RESULT:0:7}

if [ "$IS_OK" == "Success" ]; then
    echo "No problem found. Have a nice day!"
else
        echo "Problem is: >>> $RESULT <<<"

     fi


When launched manually, $RESULT is substituted with the answer from
/usr/local/bin/wp core verify-checksums --allow-root --path=/var/www/web/


When run via Cron, $RESULT is always empty. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-09-08
@kovalr

When run via Cron, $RESULT is always empty.
Why?

Most likely because it is not possible to start /usr/local/bin/wp. For variety, you can do this:
RESULT=$(/usr/local/bin/wp core verify-checksums --allow-root --path=/var/www/web/ 2>&1 )

to see the full output of the command.
In addition, commands usually set the exit status based on success/failure. It can be obtained from $?. And if /usr/local/bin/wp does everything right, then the IS_OK part can be rewritten like this:
IS_OK=$?
if [ $IS_OK == 0 ]; then

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question