I have some executable PHP code that I'm trying to implement using cron jobs. The page sends a couple emails. It works just fine when I type in the address into my URL bar and load the page (the url is akin to www.mysite.com/mypage.php) but for some reason it doesn't work when I do it as part of a cron job. I have double checked the permissions and the file is executable, so that's not the issue. I am getting an email confirming that the cron job was completed, but the emails that are supposed to be sent by the program do not come through. Here is my code in crontabs:
SHELL= /bin/bash/
HOME = /
MAILTO = "mymail#gmail.com"
* * * * * /usr/bin/php /usr/share/nginx/html/mypage.php
Any idea why this might not be working?
EDIT ABOUT PERMISSIONS: Possibly relevant: I am editing cron jobs by typing 'sudo crontab -e'. I was able to successfully set up another cron job that just emails me text. However, I tried setting up another text-only-email cron job NOT in sudo (ie typing 'crontab -e') and that did not work. I didn't receive any emails. I also got an error when I typed 'crontab -e' about it not being able to read .nano_history and permission being denied but I was able to bypass that by pressing Enter
Check to see that wget is installed on your server. it should be. Then use wget to call your program from the cron tab. This way you do not have to concern yourself with calling php from the command line.
wget http://999.999.99.9/hr/stats/send_stats.php?task=first
Related
I dont know alot about cron job but i have a php file which sending data with attachment when i run it using the browser it's working fine and attachment is sent ,but if i use cron job to run it ,it never goes with the attachment.
here is the cron job command-line :
/opt/php54/bin/php /home/username/public_html/path/to/myscript.php
and here is the place of error in the php file ,as the cron job sending me email with "Some how file :hhshd.jpg is not exist!" , however the data with attachment is sent successfully when i use the browser.
if (!file_exists("uploads/" . $fileName))
{
die("Some how file :$fileName is not exist!");
}
Note: i am using the cronjob of Hostgator.
Problem solved i have used this cron job command:
curl http://yoursite/path/to/publish.php
One possible reason cold be when you run the script through browser, the Apache user is able to create the attachment file and send it.
But when cronjob is not able to create attachment file due to permission issue.
You can try using 'sudo' in your cronjob or try changing the permission of the directory where you are creating the attachment.
It is most likely to be an ownership & permissions problem. What you have to put in your cron config depends on where you've put it and what user your web server runs as. If you want it to run at 5am each day, it's in /etc/crontab and www-data is your web server user, you would need an entry like this:
0 5 * * * www-data /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
If it's in the web server user's own crontab file, it would skip the user name field. You would edit it using crontab -u www-data -e, and add a line like this:
0 5 * * * /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
One other issue may be that the current working directory may be different, so you may need to set the command to include a cd to the directory your script is in first, like this:
cd /home/username/public_html/path/to && /opt/php54/bin/php myscript.php
If you want to receive any error output from a cron job, set a MAILTO environment variable by adding a line like this to the cron file before you run the command:
MAILTO=user#example.com
Example:
* * * * * curl http://localhost/Android/SMS/p1.php
type above line to execute every second p1.php email sending file with attachment.
p1.php file is in my htdocs/Android/SMS folder.
have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.
I am working on an automatic monitoring system. I have made a .NET application which have MySQL database. For this I developed a normal ADMIN Panel where admin can log in and get necessary reports coming from various queries fired on the database. There is also a "summary Report" in the panel which is just the rough weekly summary. Now What I want is, I want this report (all text) to get sent automatically to some email "xxxxx#xxx.com" with a seven day period. I have used some PHP scripts previously to send email on submit button click. Like the one below.
<?php
if(isset($_POST['isPost']))
{
$header="From:".$_POST['customer_mail']."\r\nName:".$_POST['name']."\r\nCity:".$_PO ST['city'];
$subject = $_POST['title'];
$message="From:$_POST[customer_mail]\r\nName:$_POST[name]\r\nPhone:$_POST[city]\r\n \r\n\r\n\r\n".$_POST['details'];
$to = 'xxxxxxxxx#xxx.com';
$send_contact=mail($to,$subject,$message,$header);
if($send_contact)
{
echo "<h6 style='text-align:center;color:green'>Sent Successfully</h6>";
}
else
{
echo "<h6 style='color:red'>Error sending e-mail'</h6>";
}
}
?>
this is normal mail sending script which works fine. But for the above purpose I want, Can anyone help me to set this action periodically and automatically or point me in the right direction on how to do this. I have googled for such php script but no satisfied results.
~ Regards
You can do this with cronjobs.
A long running process which executes commands at given times / dates / intervals.
Depending on what system you are, there are different methods.
If you have the script on a webserver someone is running for you / Webhost service
Ask the system administrator to run the script with a cronjob. Or search for any help documentation if you can setup this yourself in any admin-panel. Ask your webhoster /system admin for more information.
If you have the script on your own local machine:
UNIX
Try reading about crontab on how to run the php script, or any script for that matter.
For example type crontab -e and add the line below in your crontab, the cronjob will run your script every hour:
00 * * * * /usr/local/bin/php /home/Low-pointer/myscript.php
Here is some more information if you want to play with the intervals
Windows
If you use Windows you can use scheduled tasks to run the php command.
For example add the following command to the scheduler: C:\wamp\bin\php\php.exe -f C:\wamp\www\my_script.php
You can also use web cron services (Google it) these are online services which run a page (for example on your webserver) on designated times. For free or paid.
You can use Cpanel to send schedule emails through the cronjob(if you are using it).
Once you open cpanel theere would be crontab system.
Add your current code to a file(xx.php) and add this command to crontab in cpanel ,
/usr/bin/php -q /home/public_html/xx.php
like everyone have already said you must use cronjob to make your task.
I assume you use a Linux OS as your production environment.
So you need:
1) A php endpoint ( eg. www.mywebsite.com/emailsend.php ) OR a CLI php script that when called send the email.
2) The correct crontab rule
Here below an example of a simple shell script ( mailsend.sh ) that call an endpoint using CURL and save an html file with an eventual response given by the webserver
#!/bin/bash
curl http://www.mywebsite.com/emailsend.php -o /path/to/my/mailsendreport/"$(date '+%Y-%m-%d-%H-%M-%S')".html
To add a scheduled task to cron
crontab -e
then add a rule like below
50 23 * * 6 sh /path/to/mailsend.sh
What "50 23 * * 6" means? It means that every sixth day of the week, in the 23th hour, in the minute 50 your sh script will be called, and then your web app and the email is sent.
Once I wrote a small doc about this you can see it here
Cheers
You're looking for "cron job".
Edit: Or since it sounds like you might be on Windows, "Scheduled Tasks"
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
I am running a cron job every five minutes which calls a php script to check to see if users have imported any files for processing.
When I run the php script by going to the address in my web browser it runs the script and then sends the user a notification by email. When I run the script using the cron job, the script works fine, but it doesn't send the user an email. Any thoughts about why no email is sent?
I'm running Ubuntu Hardy LTS. The cron job is:
*/5 * * * * /usr/bin/wget -–delete-after http://www.mywebsite.com/import_processing.php >/dev/null 2>&1
I'm using delete-after so that I don't get copies of the script piling up in my server directory. I'm suppressing output and errors also as I don't need email confirmation myself.
The script uses the basic mail function, and as I said, works just fine when run from my browser.
Update: It looks like the issue is my php script is looking for a browser cookie to send the email. I imagine I'll have to find another way to get the user's identity.
run it like this
*/5 * * * * /usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
when you use wget you are downloading the file,
with php you are running the file,
test your script running
/usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
using the local path, just in case run
$ which php
to figure out where it is installed
The script was running properly using wget or php; the problem was that the php script looked for a browser cookie which didn't exist when the page was run outside of a web browser.
I changed the php script to do a database lookup instead and it worked just fine. Thanks for your suggestions.