Cron job is not working-cpanel VPS Optimized 3 - php

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)

Related

Setting up a Cron Job on CPanel to executes a PHP script

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

Php Cron job not working, using crontab -e command

i want to set a cron job on a server but its not working. I know there are hundreds of links on web that shows how to setup a cron job but i cant seem to make it work. What im doing now is:
1) Running crontab -e.
Then it shows bunch of lines in the command line.
2) I go to the bottom and add */5 * * * * path/to/myfile.php
and then i exit the editor in command line. Please tell me whats wrong here. Do i need to put my file in a specific folder? or do i need to go to the desired folder and then use crontab -e, or something else. Please forgive me, this is my first cronjob, hoping to be better next time.
Here are the pictures of what im doing.
Did you restart the cron service after you updated the file?
Have you tried executing the php script from the command line first to verify that it's executing as expected? It might be that the cron task is executing but the script is failing. If the script is fine, you might want to try using php as a command followed by the path and filename of the php file and then quitting the execution after it's done with -q.
*/5 * * * * php path/to/myfile.php -q
The problem could well be that you are trying to execute a PHP file and your system is unaware of what to do with it.
Is your PHP file executable?
You can make it executable by running
$ chmod +x file.php
and if you add a shebang to it
#!/usr/bin/php
<?php
// ...
the PHP script can be executed by running
$ ./file.php
Alternatively, you need to run the PHP interpreter and pass it the path to the file as an argument.
$ php file.php
For reference, see:
http://php.net/manual/en/features.commandline.usage.php

sending data with attachment doesn't work Cronjob

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.

How to run crontab-e?

I am currently reading this documentation here where I want to use CRON. Now it says in the first section that I need to enter in a command: crontab -e.
Do I only need to enter this in a simple text editor file and just upload the file into the server?
I am using helios.hud.ac.uk so would this be the correct command:
* * 25 10 * helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
This will execute this php script below (inactivatesession.php):
<?php
include('connect.php');
$createDate = mktime(0,0,0,10,25,date("Y"));
$selectedDate = date('d-m-Y', ($createDate));
$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);
$update->execute();
?>
The url for this php script is: helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
I havn't used CRON before so just need little help on it.
Thanks
If you are making a crontab that will access a remote webpage (which is what this is as it is not on your local server) you need to prepend the URL with wget
* * 25 10 * wget -O - http://helios.hud.ac.uk/u00000000/Mobile/inactivatesession.php
It will run the script on the server and output it to standard output (which in most servers will be emailed to you)
This assumes that you have a linux machine. crontab -e sets up a cron tab for your user account. So you can't really upload a crontab, but if you have cpanel or similar, most times you have access to cron from there.
You open a shell (probably through SSH) to your server
You run the command crontab -e
You edit the crontab according to your needs (if you want to run a php script over http you need to use wget)
You save and exit If you didn't make any mistakes, you will get a message that crontab was updated

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