Score:-1

WP-CLI doesnt run from cronjob

cn flag

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?

vautee avatar
kr flag
Is there any error output from your cron job in /var/log/syslog? Maybe remove the >/dev/null 2>&1 part so you'll see any errors that show up. Add them to your question.
user9517 avatar
cn flag
The duplicate will almost certainly help you to solve your problem, If it does not it will help you gather information that will help people to help you.
cn flag
Now Im getting the error, says this: Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under. If 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.
cn flag
If you'd like to continue as root, please run this again, adding this flag: --allow-root If you'd like to run it as the user that this site is under, you can run the following to become the respective user: sudo -u USER -i -- wp <command>
Score:0
cn flag

user9517 provided the answer: Does this answer your question? Why is my crontab not working, and how can I troubleshoot it? –

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.