0

I am working on a project where i need to setup cron jobs dynamically. for your acknowledge i am using linux e2-user amazon instance. i have added cron job and its running fine if i run from terminal but not works when i run url from web browser or postman.

here is my code for dynamic cron job in demo2.php file.

$output = shell_exec('crontab -l');

file_put_contents('crontab.txt', $output.'20 14 * * * php /var/www/html/jackpot/demo.php'.PHP_EOL);
exec('crontab crontab.txt');

when i run in terminal php demo.php it works fine and run two cron jobs. If i run same file in browser like this http://ec2-52-23-188-162.compute-1.amazonaws.com/jackpot/demo2.php and after run this file i go to my terminal and type crontab -l it shows nothing. i dont know whats going wrong with this.all linux command are also working perfectly.

Please tell me any perfect solution for this.

Thank you

Shakti Sisodiya
  • 218
  • 2
  • 5
  • 14
  • 2
    Are you sure you're using the same user id as the web server's using? – Bjorn A. Feb 07 '18 at 15:17
  • 1
    Use absolute paths wherever plausible to avoid environment discrepancies. – IncredibleHat Feb 07 '18 at 15:22
  • 1
    Possible duplicate of [Why Doesn't My Cron Job Work Properly?](https://stackoverflow.com/questions/13204/why-doesnt-my-cron-job-work-properly) – shellter Feb 07 '18 at 16:29
  • Hi, Your crontab is running in a restricted environment. This means that `PATH` is very limited and probably `php` cannot be found. Use the full path of `php` in your crontab file. (Also have a look at ServerFaults [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)) – kvantour Feb 07 '18 at 16:33

1 Answers1

0

Kindly check the privileges of the file which user currently being used to access the file.

Ram
  • 111
  • 1
  • 3