Wordpress cli via cron - user/path issues

jk_gabba

Registered
Aug 18, 2008
2
0
51
I've got wp cli installed on my cpanel server successfully, and via terminal under the account or via putty it works as it should, however calling it via a cron job it doesn't seem to find the wordpress install, wp commands work but the path target doesn't so you just get the default info rather than executing the command.

I've tried cd to the root dir and running under the main crontab, as well as via the account cron and none seem to work, this is my test cron script at the moment, which should create a new post and log to cron the action:

/usr/local/bin/php /usr/local/bin/wp post create --post_type=post --post_title='test 123' --post_status=publish --path=/home/development/public_html/ > /home/development/cron.txt
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
15,294
2,438
363
cPanel Access Level
Root Administrator
Hey there! Just to confirm, if you run this on the command line directly, it works as expected?

Code:
/usr/local/bin/wp post create --post_type=post --post_title='test 123' --post_status=publish --path=/home/development/public_html/ > /home/development/cron.txt
I believe you'll need to specify the path of PHP, and possibly the WordPress install too:

Code:
/usr/bin/ea-php## /usr/local/bin/wp --path=/home/username/public_html/ post create --post_type=post --post_title='test 123' --post_status=publish --path=/home/development/public_html/ > /home/development/cron.txt
Can you try that and see if that gives you better results? You'll just need to adjust the "##" portion to use the version of PHP you're working with.
 

jk_gabba

Registered
Aug 18, 2008
2
0
51
yes, the command works correctly via terminal every time, and i've just tried via cron with the php version as you suggest and it gives the same result, it runs and obviously finds php but rather than executing the command it just spits out the default wp info text, which suggests that wp-cli works as it should but its just not being directed at the wordpress install or responding to the path value.

wondered if it was cron user related, or due to not exectuing in the correct directory, so i tried this:

cd /home/development/public_html; /usr/bin/ea-php81 /usr/local/bin/wp post create --post_type=post --post_title='test 444' --post_status=publish --path=/home/development/public_html/ > /home/development/cron.txt

and that does the same, just spits out the info.
 

rbairwell

Well-Known Member
May 28, 2022
110
47
28
Mansfield, Nottingham, UK
cPanel Access Level
Root Administrator
cd /home/development/public_html; /usr/bin/ea-php81 /usr/local/bin/wp post create --post_type=post --post_title='test 444' --post_status=publish --path=/home/development/public_html/ > /home/development/cron.txt

and that does the same, just spits out the info.
Can you try something like
Code:
cd /home/development/public_html && /usr/local/bin/php /home/development/bin/wp post create --post_type=post --post_title='test 444' --post_status=publish >> /home/development/cron.txt[  2>&1
The && (Bash's logical "and") can help chain commands - the semicolon indicates to Bash that they are separate commands and so the cd command may not affect effect the next call. Adding the 2>&1 to the end will also mean any error output (on stderr/pipe 2) will go into your cron.txt log.