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.
Related
I am trying to send email using PHP mailer at regular interval. I am using windows 10 and have set up a cron . Here is .bat file code .
#echo off
schtasks /Create /TN XAMPP /TR "C:/xampp/php/php-win.exe C:/xampp/htdocs/aurr/emailcron.php" /SC MINUTE /MO 1
pause
Problem is even though I am able to send emails ,there is no attachment in received emails .But when i manually execute emailcron.php ,I get attachment in received email.My guess is this might be because of permission issue .How do I fix this error?
When the script is run by the cron task it's run by whichever user the cron script is run as (a default if you don't override it), and when you run it yourself it's obviously run as you. If you have permission to read the file but the cron user does not, it won't be able to send the file.
Several things to look at here:
Run the cron task as a user that does have permission to read the
files, e.g. you.
Give the cron user permission to read the files.
Make your script check for a successful return value when it adds
attachments.
For the last of these, do it like this:
if (!$mail->addAttachment('path\to\file')) {
echo "Could not access path\to\file";
exit;
}
That way it will generate an error and not send the message if it can't access the file, rather than failing silently as it's doing at present.
I have one PHP script that I want to run every 20 minutes. I search in google and I see I must use CRON for that, I have to use FileZilla and putty to access my server. I found this:
https://crontab.guru/every-20-minutes
SO the code should looks like this:
*/20 * * * * /usr/bin/php /path.php
I need to write in the putty?
Use Putty to connect to your server via SSH. It will give you a command line interface (CLI).
Once in the CLI, you need to type the following command:
crontab -e
This will open the CRON config file. Then you need to add a line at the bottom of the file:
*/20 * * * * /usr/bin/php /path.php
Finally, you save and quit using CTRL+X
And that's it. The PHP script will be executed every 20 minutes. Of course, I'm assuming you're SSH access have the right permissions.
Now I don't know if your script path.php is already on your server, but this is where you need FileZilla, to upload the file on the server, or to update it every time you change it's content.
This is my code. File name is test_cron.php(inside the crtest folder).
Cron command is: /usr/local/bin/php -q /home/portroot/public_html/crtest/test_cron.php
It should be run on the server every minute. It should generate the text files every minute. But nothing is happen. I gave an email address also. But I didnt get any email. Please help me to correct this.
<?php
//Cron command: /usr/local/bin/php -q /home/portroot/public_html/crtest/test_cron.php
$filename = "./public_html/crtest".time().".txt";
$handle = fopen($filename,'w') or die("Cannot open file");
for($i=0;$i<10;$i++)
{
$con = "Hello world \n";
fwrite($handle,$con);
}
fclose($handle);
?>
u have to do 2 different test :
check ur script without using Cron by running it in browser and check if u get the result u want.
check ur Cron with a very sample script like an insert query to enter some data to ur database.
Then u can find the problem.
Please follow below step to check your code and setup cron on server:
1. First check your code is successfully running on your local system or not.
2. If you need to call a php script using URL; you can simply use lynx, curl or wget. Make sure you've placed your php script within the www or public_html directory and call the path properly on the cronjob.
*/2 * * * * wget -q http://localhost/test_cron.php
3. I've used this command to activate cron job for this.
/usr/bin/php -q /home/username/public_html/yourfilename.php
on mostly server and it works fine.
/usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)
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
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