I have a bash script which is retrieving the information from my database, based on the information it gets creates some folders containing the WP files on each one and then makes the setting. The script works perfect when I call it manually however the WP CLI dosent work when is called from my ubuntu Cronjob
Here is the bash script:
#!/bin/bash
set -f # disable globbing
IFS=$'\n' # set field separator to NL (only)
arr=($(sudo mysql -u root -h localhost -e "USE mysite;SELECT * FROM sites WHERE status = 'pending'" | awk 'NR>1'))
for i in "${arr[@]}"
do
siteid=$(echo $i | awk '{print $1}')
instance=$(echo $i | awk '{print $2}')
owner=$(echo $i | awk '{print $3}')
status=$(echo $i | awk '{print $4}')
temporaryurl=$(echo $i | awk '{print $5}')
url=$(echo $i | awk '{print $6}')
dbname=$(echo $i | awk '{print $7}')
dbuser=$(echo $i | awk '{print $8}')
dbpass=$(echo $i | awk '{print $9}')
dbprefix=$(echo $i | awk '{print $10}')
title=$(echo $i | awk '{print $11}')
admin_user=$(echo $i | awk '{print $12}')
admin_password=$(echo $i | awk '{print $13}')
admin_email=$(echo $i | awk '{print $14}')
sudo mysql -u root -h localhost -e "CREATE database $url;GRANT all privileges on $url.* to $dbuser@'localhost' identified by '$dbpass'"
sudo cp -r /var/www/mysite.com/wordpressfiles /var/www/mysite.com/$url
sudo chown iosef:iosef -R /var/www/mysite.com/$url
sudo find /var/www/mysite.com/$url -type d -exec chmod 777 {} ;
sudo find /var/www/mysite.com/$url -type f -exec chmod 777 {} ;
sudo find /var/www/mysite.com/$url/wp-content/ -type d -exec chmod 777 {} ;
sudo find /var/www/mysite.com/$url/wp-content/ -type f -exec chmod 777 {} ;
cd /var/www/mysite.com/$url && /usr/local/bin/wp core config --dbname=$url --dbuser=$dbuser --dbpass=$dbpass --dbprefix=$dbprefix;
cd /var/www/mysite.com/$url && /usr/local/bin/wp core install --url=www.mysite.com/$url --title=$title --admin_user=$admin_user --admin_password=$admin_password --admin_email=$admin_email;
done
And here is the cronjob (is runned as root)
* * * * * /bin/bash /home/iosef/createinstanceinfolder.sh >/dev/null 2>&1
Any idea?
Add them to your question.
– vautee Jan 15 '22 at 21:36If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
– Iosef Jan 15 '22 at 22:47If you'd like to run it as the user that this site is under, you can run the following to become the respective user:
– Iosef Jan 15 '22 at 22:48