This question already has answers here:
Cannot get PHP cron script to run
(4 answers)
Closed 8 years ago.
I am running a php script using cron. The name of the file is : PriceChecker.php .
This is my cron script
# m h dom mon dow command
0 07,11,16,20 * * * php /var/www/mainsite/PriceChecker.php
* * * * * /var/www/mainsite/pricecheck.sh
The shell script is:
#!/bin/bash
php PriceChecker.php >logger.log
The first line is the original. The second is for testing
I have tried various variations including: /usr/bin/php /var/www/mainsite/PriceChecker.php
If I log the out in the cron php /var/www/mainsite/PriceChecker.php > logfile.log
The logfile is created, but is empty
Same as here, I think Cannot get PHP cron script to run
You forgot to specify the user name you want your script to be run as.
Specify the full path of the PHP file to be execute
Okay what worked was CDing into the directory of the script
https://serverfault.com/questions/97828/php-from-command-line-path-problems/97881#97881
Related
I have a php script, let's say fetch.php which will pull data from other database and insert those data to another database, How do I run the script automatically maybe via command line, let's say I want it to run Everyday at 3pm? Something like a cron.
Here is the cron rule you should add:
00 15 * * * php fetch.php
Read more here: Running a script every day using a cron job
If you are on Windows, you can make use of Windows Task Scheduler.
Here is the link: http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
Using php from command line is not that hard. Start the fetch.php file like this:
#!/usr/bin/env php
<?php
// You have the command line arguments available as
$scriptName = $argv[0];
$firstArg = $argv[1];
// Add PHP code here :)
// If an error occurs, exit the script with an error code:
if($someError) exit(1);
After saving, set execution rights:
$ chmod 755 fetch.php
Then add the script to the crontab. There are many ways, control panels like Plesk and cPanel have a web interface for example, but here's the command line version:
$ crontab -e
Add to the file:
0 15 * * * /path/to/fetch.php
Save, and you're done.
This question already has answers here:
Crontab - Run in directory
(2 answers)
Closed 8 years ago.
I have a php script that sends out a email report with an attached pdf document. I use fopen. If I call the php file manualy it works without any problems and the mails are beeing send as expected.
But:
If the same file should is call by cron job, it does not work at all. The error is:
Warning: fopen(filename.pdf): failed to open stream: No such file or directory in /home/public_html/util.php on line 29
Any ideas how to fix this?
Every cron runs in a minimal environment, which can be a different shell and a different profile than your user account. Also scripts are started not from the containing directory themselves.
In most cases it works, if you just change your directory to the one containing the script like you do when execute the script on the command line shell. Instead of
* * * * * /usr/bin/php /path/to/your/file.php
write this:
* * * * * cd /path/to/your/ && /usr/bin/php ./file.php
If this is not sufficient, you can load your default user shell and your PATH variable with writing
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
at the top of your crontab (with the right settings of course, which might be /bin/bash and the output of echo $PATH).
Cron jobs run without any environment, including a current directory. That means that you'll need to use the full path to any files that you want to work with.
If you add a valid email address at the top of your crontab:
MAILTO=your-email#example.com
you'll get any output the cron job does sent to your email. This includes error messages, which can be useful for debugging.
This question already has answers here:
Cron Job with Laravel 4
(4 answers)
Closed 7 years ago.
I am trying to develop a cron job for a command I have already created. I am completely new to cron jobs so I dont really know how it works.
Trying the command by myself in the console works perfectly. All I need is to be able to execute it every 24 hours. I am using Laravel 4, can anyone help?
Thanks!
To create a cron job as root, edit your cron file:
[sudo] crontab -e
Add a new line at the end, every line is a cron job:
25 10 * * * php /var/www/<siteName>/artisan <command:name> <parameters>
This will execute the same command at 10:25AM everyday.
Just make sure you keep a blank line after the last one. And you also might need to use the full path of your php client:
25 10 * * * /usr/local/bin/php /var/www/<siteName>/artisan <command:name> <parameters>
You could register your cron job like this:
php /path/to/your/laravel/project/artisan your-custom-command
See my answer on this question, my example is for L3 but should work for Laravel 4 accordingly.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
schedule an email in php
How can I keep loading a PHP file on my server after every hour?
It will mail me something. Actually it mails me something when I visit it. But I want it to mail me after every hour. So someone should either visit it each hour or I have to use cron job like something.
Any help will be appreciated.
You should use a cronjob.
Start by opening you terminal and run
crontab -e
You may need to configure your crontab settings (default editor) if this is the first time you are using crontab. Now, in your editor, you have to call your php script like this: (it is set to be called each hour)
0 * * * * /path/to/php /path/to/your/script.php
You can also use an alias of hourly.
#hourly /path/to/php /path/to/your/script.php
This question already has answers here:
Executing php with crontab
(6 answers)
Closed 9 years ago.
My situation is that I have a php file at /root/test.php.
And I set my root's crontab like this
* * * * * /usr/local/bin/php /root/test.php
The content of test.php worked for generate the aliase database for postfix just like the following:
<?php echo shell_exec("postaliases hash:/etc/aliases"); >
I know that the test.php is successfully run by crontab.
But the shell_exec inside doesn't seem to work(I can't find my target aliases.db file under /etc/).
And I have tested use commend php -f test.php directly is work.
I know the situation is a little complex. But if someone can solve my problem, I will be very appreciate your for help.
Use the full path to postaliases. Most likely it is not in the path of the user executing the script
Why do use a php script to call a shell? Just use a shellscript