Trying to pass arguments to wp-cli in a bash script

298 Views Asked by At

I'm using a wp-cli tool in order to optimize images:

$ wp image-optimize batch --limit=20

I've installed wpcli using composer so it's in an unusual location, but is in my $PATH: /home/user/.config/composer/vendor/wp-cli/wp-cli/bin/wp

This works great. I'd like to run this command nightly. I've tried two different approaches to this. First, I tried running the command as a cronjob (set every minute for testing):

$ crontab -e

* * * * * cd /path/to/example.com && wp image-optimize batch --limit=20

I got no response. I wondered if the problem had something to do with passing arguments in a cronjob. So, I created a bash script nightly-image-optimize (also in path) hoping that this might get around it:

#!/bin/bash

echo "begin" >> /home/user/cronlog.log

cd /path/to/example.com

sh /home/user/.config/composer/vendor/wp-cli/wp-cli/bin/wp image-optimize batch --limit=2

echo "end" >> /home/user/cronlog.log

I then modified the cronjob to execute this file every minute as my username since cron runs as root: * * * * * username /usr/local/bin/nightly-image-optimize

I know the cronjob is running because my cronlog.log file is created and is populated every minute with the echo begin and end statements above.

While in context this is a wp-cli problem, I don't believe that the issue has anything to do with wp-cli. I think I'm misunderstanding how to essentially 'tell' bash to run a process as if I had manually entered it in (maybe something to do with the interactivity of wp-cli?).

Any ideas?

Note: I'm on AWS running Ubuntu 18.04.3 as a non-root user with sudo privileges

0

There are 0 best solutions below