I'm using whm/cpanel and the first time i'm doing a cron job.
I have a php file located in /home/my_website/public_html/wp-content/youtube_channels/cron.php
I want this to run every minute.
I have this as a cron command but i want to make sure it is correct before going forward.
***** /home/my_website/public_html/wp-content/youtube_channels/cron.php
Is this correct? I want this code to run every minute.
A crontab line
* * * * * php /path/to/phpfile.php >> /path/to/logfile.log 2>&1
* * * * * time settings
php to execute an php file, You can not just call the phpfile like an executeable file (only if you setup and add #!/usr/bin/php correctly)
>> pipes the output from the phpfile into logfile (or else)
/path/to/logfile.log the log file
2>&1 ensures that all what is comeing from STDOUT and STDERR is piped into the logfile.
More info:
https://corenominal.org/2016/05/12/howto-setup-a-crontab-file/
Run a PHP file in a cron job using CPanel (not only for CPanel user)
http://php.net/manual/en/wrappers.php.php (STDOUT/STDERR)
Have a nice cron
For a crontab entry, you need spaces between the stars -
* * * * * /home/my_website/public_html/wp-content/youtube_channels/cron.php
Related
I wrote a PHP script that push a CSV file into a database. I want to do this automatically every minute. I know there is a way via cron on Linux but I don't know anything about bash and think cron can't give my PHP file a callback, so I can show a progress bar for the user to see the timer interval. What do I do?
You can enter your jobs using crontab -e. If your default editor is vi, I recommend to change it nano using export EDITOR=nano because it is easy to use for starters.
Every line of the crontab file represents a job. The first 5 tokens are : minute, hour, day of month, month, day of week respectively, the last one is command so in your case first 5 tokens will be * * * * * that means run this job every minutes when the second is '00'.
You can call your php files directly using this command : php /var/www/cron.php & or using a browser wget -O /dev/null http://example.com/cron.php If you use first one you cannot use some $_SERVER variables but if you use second one, it is like a real browser.
In your case you can use like this :
* * * * * wget -O /dev/null http://example.com/cron.php
to add a cron and make it run every minute, type crontab -e and add the following line
* * * * * command you need executing
example:
* * * * * ls -l /home/ > /usr/local/users.txt
* * * * * df -h > /tmp/filesystem_usage.txt
* * * * * service httpd restart
Look at this for a starter: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
Also remember that cronjobs don't support all $_SERVER vars like 'DOCUMENT_ROOT' and 'HTTP_HOST', so try to avoid them, or use a workaround.
Some 'callback' possibilities:
- Let your script trigger another script
- Redirect the output of your cron to an another bash script
- ...
I try to create a cron on a server (I should have right on it).
This is my script cron.php :
<?php
echo "CRON OK \n";
?>
I connect with SSH and create the following cron with crontab -e :
* * * * * php /home/myname/www/cron.php
But I don't know how to save it.. Anyway I save the crontab at the default location /tmp/crontab.XAblsdZ/crontab, the server tell me "crontab: installing new crontab" and when I execute crontab -l I can see * * * * * php /home/myname/www/cron.php.
I also try my script : php /home/myname/www/cron.php
The server display "CRON OK" so I guess it works.
But without this command I can't see any "CRON OK" displayed... Am I missing something ? How to enable this cron and execute my script every minute?
either add #!/usr/bin/php as suggested or you could add it to your crontab, make sure the /usr/bin/php is actually the right path to your PHP.
* * * * * /usr/bin/php /home/myname/www/cron.php
The cron line looks good to execute every minute though.
Try adding this to the beginning of your cron.php:
#!/usr/bin/php
I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.
I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.
I have verified that my php script runs from the command line, but it has not been executed from the crontab (yet, I guess). Does it take a while for it to start working?
this is the crontab line:
00,15,30,45 * * * * php /var/www/download.php
I want it to execute everyday, every fifteen minutes starting at the top of the hour.
Most likely it is environment variable issue. First thing to check is whether cron user has php in its path?
Cron jobs don't have access to all the env variables set in user's profile. Better to redirect stdout and stderr to a file in your cron command like this:
*/15 * * * * php /var/www/download.php > $HOME/cron.out 2>&1
And then after 15 minutes examine $HOME/cron.out why it failed.
*/15 * * * * php /var/www/download.php
if that fails, then your script is probably failing. permissions and what not. check the cron log.