1

I have the following command set used to update all my WordPress sites in my CentOs shared-hosting partition on my hosting provider's platform (via daily cron).

The wp commands inside the pushd-popd set, are of the WP-CLI program, which is a Bash extension used for various shell-level actions on WordPress websites.

for dir in public_html/*/; do
    if pushd "$dir"; then
        wp plugin update --all
        wp core update
        wp language core update
        wp theme update --all
        popd
    fi
done

The directory public_html is the directory in which all website directories are located (each website usually has a database and a main file directory).

Given that public_html has some directories which are not WordPress website directories, than, WP-CLI would return errors regarding them.

To prevent these errors, I assume I could do:

for dir in public_html/*/; do
    if pushd "$dir"; then
        wp plugin update --all 2>myErrors.txt
        wp core update 2>myErrors.txt
        wp language core update 2>myErrors.txt
        wp theme update --all 2>myErrors.txt
        popd
    fi
done

Instead writing 2>myErrors.txt four times (or more), is there a way to ensure all errors whatsoever, from every command, will go to the same file, in one line?

5

The > file operator opens the file for writing but truncates it initially. That means that each new > file causes the content of the file to be replaced.

If you'd want the myErrors.txt to contain the error of all the commands, you'd need either to open that file only once, or use > the first time and >> the other times (which opens the file in append mode).

Here, if you don't mind the pushd/popd errors to also go to the log file, you can redirect the whole for loop:

for dir in public_html/*/; do
    if pushd "$dir"; then
            wp plugin update --all
            wp core update
            wp language core update
            wp theme update --all
        popd
    fi
done  2>myErrors.txt

Or you could open the log file on a fd above 2, 3 for instance, and use 2>&3 (or 2>&3 3>&- so as not to pollute commands with fds they don't need) for each command or group of commands you want to redirect to the log file:

for dir in public_html/*/; do
    if pushd "$dir"; then
          {
            wp plugin update --all
            wp core update
            wp language core update
            wp theme update --all
          } 2>&3 3>&-
        popd
    fi
done  3>myErrors.txt
3

You can use curly braces to group a block and redirect all the output:

for dir in public_html/*/; do
    if pushd "$dir"; then
        {
            wp plugin update --all
            wp core update
            wp language core update
            wp theme update --all
        } 2>myErrors.txt
        popd
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.