cronjob executing a php file - php

So I have a php file that executes each time it you reload it in the web browser. It uses PHPMailer, to send mail based on criteria in my db. I was attempting to use a cronjob to execute the file which I thought would basically do the same as reloading the page. The php file that I need to run in the cronjob is test.php, and its path is /var/www/html/mailer/test.php.
My cronjob is:
1 * * * * root /var/www/html/mailer/test.php >> /var/www/html/mailer/cron_status.log
and it should be throwing errors into that cron_status.log file, but its empty. I realize that this is firing every minute, but I'm just doing it to test the cronjob, and I really need to set it to 24hrs. With no error output, and no emails landing where they should be, I don't think I've properly setup my cronjob. This is my first time ever trying this. This is on a centos 7 droplet, and I've followed the tutorial from digital ocean with no success.

I need to see the php file to be sure, but you probably need to change it to this:
1 * * * * php /var/www/html/mailer/test.php >> /var/www/html/mailer/cron_status.log

Related

Run Crontab in Linux when a new file created in a directory

I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.
*/5 * * * * php /var/www/html/cron/import-csv.php
Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).
I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/
There's a couple of ways I can think of off the top of my head:
Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)
And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)
SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php
One way is you can distribute your tasks is 3 different scripts and chain them in cron.
*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php
&& will make sure that, the next script runs only if the previous
ran sucessfully.

Laravel 4 CronJob Keeps Running

I have a weird situation here.
I'm using Laravel 4 and linux crontab to run my cronjob, which is to send an email.
For testing purpose, I set every minute to run the cronjob so that I could get my result instantly.
crontab commands as below:
* * * * * php /xx/xx/xx/artisan user:active
actual output: received emails every minute, which is very good.
then I changed to
0 0 * * 0 php /xx/xx/xx/artisan user:active
actual output: I still received emails every minute, which is not supposed to be.
then I removed the crontab, just wipe the command and I still receiving email, unless I remove the recipient email address.
May I ask where I should check what's wrong?
SOLUTION:
the correct cronjob was created by another user where all the while I logged in as root.
reboot the machine its likely still running in the background

set up Cron job to run PHP script

I know this question has been asked before, but none of the answers are working for me.
I'm trying to run a simple PHP script every night at midnight. I created a file called "autoDelete.php" that contains just this code:
<?php
include 'my-database-connection.php';
mysql_query("DELETE FROM meetings WHERE indexDate < NOW()");
?>
I know this script is working because if I navigate to it in a browser, it does what it should.
I then set up the Cron job (via GoDaddy cPanel) to run every minute, with a command to run the script using this:
* * * * /usr/bin/php -q /home/username/public_html/autoDelete.php
However, this is not working. I suspect this has something to do with whatever precedes the "/home" in the command.
Some things to check:
1) cron jobs' default "working directory" is the home directory of the user ID they're running under. That'd most likely be /home/username in this case. That means if you have any relative-pathed include/require commands, they're going to be relative to /home/username, NOT /home/username/public_html. Make SURE that all necessary files are accessible.
2) You're simply assuming the query call succeeded. That's exactly the wrong the thing to do. Calls to external resources (DBs in particular) have exactly ONE way to succeed, and a near infinite number of ways to fail. ALWAYS check for failure, and treat success as a pleasant surprise.
Combining these two (failing to include your connection script, and failing to check for failure), and you end up with what you've got: "nothing" happening. At least try something like
mysql_query(...) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
The error message will become your script's output, and get emailed to the controlling account's mailbox.
I've had issues in the past with running PHP scripts in a cron job when trying to invoke the PHP binary directly. My solution was to use wget in the cron job since the script was servable by Apache anyway (0 0 * * * wget url/of/script.php). Apache already has the right PHP environment set up so might as well just ask it to handle the job.

how to execute a php automatically on server

i am trying to run a PHP automatically on a server every 15 minutes. The PHP should send mails to some contacts from a database. I have created the PHP and tested it and it's ready.
Now i dont know how to set it to run every 15 minutes.
I read some article about it on this website and i saw something about cronjob. I know the server where i stored my php has the stuff installed to run cronjobs but to be honest i dont know where to start this. I have a ftp where i upload the phps and a froxlor admin panel.
I hope someone can enlighten me with this 'next level' thing :D
Thank you very much,
Daniel!
Type crontab -e, and add the below line:
*/15 * * * * /path/to/your/php/script
If you didn't add execute permission to your script, you need to add the php executive.
*/15 * * * * /path/to/php/executive /path/to/your/php/script
Search for "CronJobs" in the admin panel. There should be a section there for adding cron jobs using the interface.
When you create the cron job, you will see that there are various options to define the time interval when the task is repeated.
Just a small correction to what xdazz has to say:
*/15 * * * * php /path/to/your/php/script
You will need to prefix your script with "php".
add a cronjob
example cronjob code for php script(in directadmin):
php /home/username/domains/domainaddress/public_html/phpfile.php
example cronjob code for php script(in cpanel):
php /home/username/public_html/phpfile.php

Cron job is not giving required result, but accessing same file through browser does

I need to run a php script to generate snapshots using CutyCapt of some websites using crone job, i get websites' addressess from database and then delete this record after generating screenshots.
i used */5 * * * * /usr/bin/php -f /path/generate.php
it didn't worked for crone job but if i access the same file using browser it works fine, and if run this script using command php from command line it also works fine.
then i just created another file and accessed the url using file_get_contents; added this file to crone job it worked fine for some days but now this approach is also not working. i don't know what happened. i didn't change any of these files.
I also tried wget command to access that url but failed to get required out put.
my crontab is now looks like this
*/5 * * * * wget "http://www.mysite.com/generate.php" -O /dev/null
Strange thing is that crone job executes fine it fetches data from database and deletes record as well but does not update images.
Is there any problem with rights or something similar that prevents it to generate images using crone job but not when accessed using browser.
Please help i am stuck.
I don't know what your script is doing internally, but keep in mind that a user's cron jobs do not inherit the users environment. So, you may be running into a PATH issue if your php script is running any shell commands.
If you are running shell commands from the script, try setting the PATH environment variable from within your php script and see if that helps.
is there any user credintials on this page , such as Basic authentication ?
if so , you have to define the user name and password in wget request like
wget --http-user=user --http-password=password "http://url" ?
and try another solution by running yor script from php command line
so your crontab could look like
*/5 * * * * /usr/bin/php -f /path/to/generate.php
try this solution it will work and it is better than hitting the server to execute background operations on your data
and I hope this helps

Categories