Crontab process to link a php file - php

i have a php file named "name.php which i want to run in every 5 minutes. I want to use crontab for this function. I have found a lot of tutorial but those all are really confusing to me. Everyone seems to use a crontab file and write like so ...
*/5 * * * * wget http://www.domainname.com/cronfile.php
but i really did not get it. Suppose i have a domain name "www.mydomain.com", so if i create a php file name.php how it be directed by www.mydomain.com/name.php.
Can anyone describe shortly how i should run my php file using crontab or wget.
i am running my project in symfony.

(I'd comment if I had enough reputation to clarify if you are running the file locally - but I think that you are.)
Here's now I do it:
*/5 * * * * /usr/bin/php /home/user/cronfile.php

Related

Cronjob is not working on Ubuntu 16.04

I would like to put in place some cronjobs for my website.
My actual cronjob is:
4 * * * * /usr/bin/php /var/www/domain/backup.php
But it doesn't work.
Mannually the cron job is working.
With a service like https://www.easycron.com it's working too.
In Terminal, when I make crontab -e, I see my cronjob but it does not seem to run.
From where I can start my search to understand ?
Thanks.
In my server, when setting up a php file to run, I only have to put:
4 * * * * php /var/www/domain/backup.php. They run flawlessly. Works the same for my shell scripts as well with just bash /directory/thisScript.sh
I added this as an answer because my reputation score won't let me add comments yet.

Run a php script with cron in mac

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.

PHP Cronjob does not execute

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.

Unable to run a php script using cron

I have 2 php scripts which need to run every few minutes, the php itself is fine as I can trigger it manually by typing http://myfakesite.com/myphpfile.php into a browser. Both work as they should.
I don't have much experience with cron but I have found the crontab file (I am using Parallels Power Panel on a VPN), and it already had jobs to run-parts cron.hourly, daily, weekly and monthly. I added my email address to the MAILTO field, and get a message telling me that cron.hourly is not a directory - even though it is (although it is currently empty).
I also added my own 2 jobs to this file as follows:
*/1 * * * * wget http://myfakesite.com/script1.php
*/5 * * * * wget http://myfakesite.com/script2.php
Neither of these ever get called. In the php script I also have a line to email me once the script is called, so that I know it is working, and have never received an email via the cronjob. If I ssh into the server and use wget the php works and emails me to confirm.
I have also tried
*/1 * * * * php http://myfakesite.com/script1.php
and
*/1 * * * * wget -O /dev/null http://myfakesite.com/script1.php>/dev/null 2>&1
with no luck. The MAILTO in the crontab file never sends me a message to say there was a problem running the script, but clearly it is not doing it.
Can anyone help at all? I have no idea what to try next.
Edit: Found some info that said that the 'not a directory' error can be down to a corrupt crontab file. So I downloaded it copied the info to a new file and uploaded that. The 'not a directory' error disappeared...COMPLETELY. Even if I tell it to look for a folder which I know doesn't exist like cron.myfakefolder I don't get a Not a directory: /etc/cron.myfakefolder email. Replaced my new crontab file with the original, and I'm still not getting any feedback from the cronjob.
Edit 2: As dAm2K and prodigitalson suggested, I tried using the absolute paths to both wget and php. Neither have worked, and I've double checked the locations of usr/bin/wget and usr/bin/php just to make sure they were actually there.
I also checked var/log/cron and var/log/messages, both contained this:
2002Can't perform "download" operation: Requested file "/var/log/cron" is to big to be sent at once. Try to request file in pieces of 512KB
No idea what is happening with this. As I mentioned in edit 1, I'm also no longer receiving error emails even when I purposely add a false location. Is this related to the error in the log files?
Also, I have checked that crond is running and according to both 'System Services' and 'System Processes' it is running.
Try using absolute paths to executables with cron: eg
*/5 * * * * /usr/bin/wget 'http://myfakesite.com/script1.php'
instead of
*/5 * * * * wget http://myfakesite.com/script1.php
Double check /var/log/messages or /var/log/syslog log files for details on cron execution, and what happens to the system in general.
If your script is on the same server, Try execute it with php -q:
*/1 * * * * php -q pathToTheFile/script1.php

Trying to get php to run on cron

This is driving me a little insane, and I've gone through a hundered different things without touching on the solution; so I may miss out on some details on what I've done so far.
I'm trying to get a Cron job to run on my linux server ive got running in a datacentre. All I'm trying to get to run is a simple php script in the format:
* * * * * php -q /path/to/script/file.php
The php part runs fine if I type it in manually, but nothing happens when the cron runs; it also appears to run in the logs just fine, with no errors.
If i go back and edit with crontab -e, and put in the line
* * * * * echo "test" > /tmp/test.txt
That seems to work ok, it creates the text file.
Has anyone had any problems running a php script in this format?
(Btw I'm just testing with the run every minute, it doesnt work at any time.)
Any help is appreciated.
try invoking php with it's full path, for example /usr/bin/php
the cron will not have the same environment variables as your user profile have, so it might not find the executable.
Try to put full path to php binary (/usr/bin/php or similar)
I also don't have '-q' flag in my distribution. Check it.
Might just be some path craziness: I'd run which php and then copy the full path into cron. On one of my boxes it is /usr/bin/php and so you'd have:
* * * * * /usr/bin/php -q /path/to/script/file.php
Try that and see if it helps.
Perhaps your PHP binary is not in your PATH. Try using the full path:
* * * * * /path/to/php -q /path/to/script/file.php
It's possible that you may need to provide the full path to your php binary
* * * * * /usr/bin/php -q /path/to/script/file.php

Categories