Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizKindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
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.
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.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I want to run a PHP script from cron. I setup cron with:
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:
The testing php script (
cron_job.php) is: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 runningphp cron_job.php.As requested in the comments, if I append
2> /tmp/php.logtophp /var/www/html/cron/cron_job.phpuse 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.