0

Not sure if this belongs on Wordpress Stackexchange or here. I think here as the last line in my code is not Wordpress specific.

I have the following code in a .sh file which I run weekly using Cron.

The code iterates through all my sites, finds if a site is Wordpress, and if it is, run wp.cli to update Wordpress, plugins and themes.

It logs the output of the scripts to a file, then emails me the file.

If I run this from the command line manually, it works totally fine. I run the following command manually and via cron:

sh /home/update_all_wp_sites.sh > /dev/null 2>&1

If I run this via cron, the wp.cli commands are not run, neither is the sendmail line at the end.

I know cron is OK and the script runs via cron because my log file is still created each week, it still adds in my comments identifying what site is being run. It does not add in the output of the wp.cli commands like it does when manually run.

At first I thought maybe its an issue with tee, but the sendmail line at the end does not use tee.

#!/bin/bash

cd /home cat /dev/null > update_all_wp_sites.log echo "AC2" | tee -a update_all_wp_sites.log echo '----------------' | tee -a update_all_wp_sites.log for user in * ; do

for d in $user/public_html ; do
    count_file=`ls -1 $d/wp-config.php 2>/dev/null | wc -l`
    if [ "$count_file" != "0" ]
    then 
        echo '----------------'  | tee -a update_all_wp_sites.log
        echo $user  | tee -a update_all_wp_sites.log
        echo '----------------'  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp core update --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp plugin update --all --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp language core update --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp theme update --all --path=/home/$d  | tee -a update_all_wp_sites.log
    fi 
done

for d2 in $user/domains/*/public_html ; do
    count_file=`ls -1 $d2/wp-config.php 2>/dev/null | wc -l`
    if [ "$count_file" != "0" ]
    then 
        echo '----------------'  | tee -a update_all_wp_sites.log
        echo $user  | tee -a update_all_wp_sites.log
        echo '----------------'  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp core update --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp plugin update --all --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp language core update --path=/home/$d  | tee -a update_all_wp_sites.log
        sudo -u $user -i -- /opt/rh/rh-php70/root/usr/bin/php /usr/local/bin/wp theme update --all --path=/home/$d  | tee -a update_all_wp_sites.log
    fi 
done

done

echo -e "Subject: AC2 WP Updates Log" | sendmail -v myemail@mydomain.co.uk < update_all_wp_sites.log

Thanks

Note: I use rh-php70 as its an older server I cant get PHP 7.4 on it, in case anyone asks.

Update: Running the cron in the server admin console shows this:

sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
Mail Delivery Status Report will be mailed to <root>

1 Answers1

0

The root cause is the issue reported in the update:

sudo: sorry, you must have a tty to run sudo

As per this following post I have commented out Defaults requiretty in /etc/sudoers and now it works:

Why do I need a tty to run sudo if I can sudo without a password?