0

I have searched hell and high water for a solution to a problem I'm having in CentOS. I'm trying to set up a cron job that executes a PHP script. I was able to get this working using wget, but now that we are going into production, I need to find a way to do this whilst being more secure, as the cron job itself works with sensitive data.

The error that I'm getting is: -bash: php: command not found.

Now I've looked around and I've seen people having the same problem, but nothing has been able to help me get this working.

For reference, here is what the working crontab looked like using the wget command.

* * * * * wget http://www.domain.com/cron_script.php

This is working fine, but I need to translate this into executing via PHP, rather than making an HTTP request to get the job done.

Let me know if I left anything out.

1 Answer 1

4

Cron doesn't have it's PATH set. The easiest thing is to change the php command to the full path of the php binary.

/usr/bin/php /path/to/yourscript.php

I'm fairly certain that's the path in CentOS but you can know for sure by doing which php on the command line and it will tell you.

Sign up to request clarification or add additional context in comments.

6 Comments

I don't have PHP in my /usr/bin folder, but in my /etc folder I have a folder called php52. Inside is a single file; php.ini. When trying to run the command /etc/php52 myscript.php, I get an error saying that php52 is a folder.
Definitely not be in /etc. If which php doesn't return anything then you need to install the command line tools. If you have root or sudo access you can install it with yum install php-cli
Awesome man, that was a step in the right direction. Now the PHP command is executing, but each time I try to test it out I get a message saying: PHP Startup: Unable to load dynamic library /usr/lib64/php/modules/mcrypt.so`. Any idea what that means? EDIT: It looks to be a just a warning, so this may be something that I can overlook for now. Needless to say, I'm going to mark your answer correct shortly.
Did the script run otherwise or did it fail? Are you using cryptography functions in your script? If so does the script work properly when accessed through Apache?
Based on your edit, if you're not using any crypto functions in your script you can just ignore it. I know mcrypt was finicky in some versions of RedHat and CentOS
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.