php /var/www/html/*.php is not working in terminal which is open from Menu php *.php is working in terminal which is open inside php holding folder

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I want to run a PHP script from cron. I setup cron with:

*/1 * * * * php  /var/www/html/cron/cron_job.php

And I can see it in crontab -u root -l, but the job is not executed. It also doesn't work when I run the command directly from the command line like this:

I tried many answers related to cron but nothing worked. For example:

/usr/local/bin /var/www/html/cron/cron_job.php
/usr/local/bin/php /var/www/html/cron/cron_job.php
sudo php -f /var/www/html/cron/cron_job.php

The testing php script (cron_job.php) is:

php file_put_contents  ('test.txt',"test content\n",FILE_APPEND);  

File permissions are all OK and it is working when run in the browser or when opening a terminal in /var/www/html/cron/ and running php cron_job.php.

As requested in the comments, if I append 2> /tmp/php.log to php /var/www/html/cron/cron_job.php use a script with a syntax error and try to run (php /var/www/html/cron/cron_job.php 2> /tmp/php.log) from menu terminal, then the error log gets the error message.

profilepic.png
manpreet 2 years ago

Your script is almost certainly running and creating your file. It's just not creating it where you expect it to be. You are not using any path in your script, you just use a file name. This means that the file will be created in the directory the script is running in.

That's why, when you move into a specific directory and run the script there, you think it works. Because the file is created in the directory you ran it in and so you can see it. By default, cron runs in the home directory of the user running it. Since this is root's crontab (which, by the way, is a bad idea), the file will be created in /root. So, go check:

sudo ls /root/test.txt

So, next time, give your script a full path instead of just a file name:


Now, the file will be created in /path/to.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community