0

I added the following command in crontab -e:

15 * * * * php /home/rizwan/PHP-workspace/mgstore/testcron.php

I need to run testcron.php every 15 minutes; for testing purposes I used the following PHP code in testcron.php:

<?php 
echo "test";

15 * * * * /usr/bin/php /home/rizwan/PHP-workspace/mgstore/testcron.php >> /home/rizwan/cron.out 

this was working .but i have one php script,for adding customers from magento to ERP,when i run script manually,it asks for authorization,after accepting it creates customers into ERP from magento,I need whenever i added customer in magento after 5 or 10 mins this script should run and add the customer to ERP.how this can be done ?,If any one have any idea,please help ?

andrew.46
  • 39,479
Rizwan
  • 29

2 Answers2

0

First you have to be sure to run the executable by complete path. you can get the path using which command

$ which php
/usr/bin/php

Now the entry in crontab must be:

15 * * * * /usr/bin/php /home/rizwan/PHP-workspace/mgstore/testcron.php

Now To check the output you can use redirection trick, so redirect the output to some file and then you can check the file to see the result. So the entry would be:

15 * * * * /usr/bin/php /home/rizwan/PHP-workspace/mgstore/testcron.php >> /home/rizwan/cron.out

Now you can check the file in your home named cron.out and see if your cron run or not.

Maythux
  • 87,451
  • @Rizwan you are welcome friend – Maythux Jul 21 '15 at 11:34
  • 15 * * * * /usr/bin/php /home/rizwan/PHP-workspace/mgstore/testcron.php >> /home/rizwan/cron.out
            this was working .but i have one php script,for adding customers from magento to ERP,when i run script manually,it asks for authorization,after accepting it creates customers into ERP from magento,I need whenever i added customer in magento after 5 or 10 mins this script should run and add the customer to ERP.how this can be done ?,If any one have any idea,please help ?
    – Rizwan Jul 23 '15 at 11:46
  • @Rizwan It's better to make it as new answer to get help more than a comment here – Maythux Jul 23 '15 at 11:49
0

In PHP you you can use either the backtick operator or the shell_exec() function to run a command in the system's default shell; you could run pgrep 'php /home/rizwan/PHP-workspace/mgstore/testcron.php' and evaluate its return code; either this:

<?php
    if(!($PID = `pgrep 'php /home/rizwan/PHP-workspace/mgstore/testcron.php'`))
        echo 'Process is running with PID ' . $PID . '.';
?>

Or this:

<?php
    if(!($PID = shell_exec("pgrep 'php /home/rizwan/PHP-workspace/mgstore/testcron.php'")))
        echo 'Process is running with PID ' . $PID . '.';
?>
kos
  • 41,378