-1

I am using core PHP and written a simple script that needs to be called automatically. For testing purpose I have just written a print statement as below,

$host = 'localhost';
$user = 'admin';
$pass = 'password';
$db = 'TestDB';
print ($user);

When I execute the script from the command line as below, it works fine

php /Applications/MAMP/htdocs/project/services/test.php > ~/cronOutput/test.txt 

But when I call this from Cron, its not at all getting called. Below is the crontab command that I have,

* * * * * /usr/bin/php /Applications/MAMP/htdocs/project/services/test.php > ~/cronOutput/test.txt 
* * * * * php /Applications/MAMP/htdocs/project/services/test.php > ~/cronOutput/test.txt

Both the commands above does not work.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
  • Does this answer your question? [Why is my crontab not working, and how can I troubleshoot it?](https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it) – Gerald Schneider Oct 09 '20 at 14:13
  • I checked that already before posting here. It didn't help... – user595016 Oct 09 '20 at 14:20
  • 1
    Doesn't look like you did, because you didn't heed the advice there. `~` depends on the `$HOME` variable, which is most probably either not set or cron is using a different shell than you. Provide proper absolute paths in your cron entry. – Gerald Schneider Oct 09 '20 at 15:16
  • I am using MAC. Should I do this configuration? Because earlier I was using a framework called Drupal... Currently I am using core PHP. There the cron was working fine. So I am bit confused now.. – user595016 Oct 12 '20 at 04:55
  • I have checked all the system variables now... I executed the command -- ps -ef | grep cron | grep -v grep to check if CRON is running. The cron is working fine. Its getting called every minute. Now coming to the $HOME variable. $HOME is set to HOME=/Users/my-name . What should I do now? is this fine? Please help – user595016 Oct 12 '20 at 05:35
  • * * * * * root /usr/bin/php -f /Users/shweta.saxena/Applications/MAMP/htdocs/project/services/test.php > /Users/shweta.saxena/cronOutput/test.txt. This is the command I have updated to. But still this is not working – user595016 Oct 12 '20 at 05:48

1 Answers1

0

I found the issue here. If you are no the root user, you must create a new crontab file with the specific user name. I was logged in as a different user and was trying to run it from the root user and hence it was not working.

Below is the command to create a user specific crontab crontab -u your-user-name -e

Hope this helps someone!