Cron jobs with laravel using hostinger - php

I had problem setting up cron jobs to send automated emails under hostinger server.
I tested out the automated email function through smtp under local environment using php artisan schedule:work to run the scheduled task.
The email is sent out without a problem.
Then i created a script in my public folder following this tutorial tutorial
The cron job config is
/bin/sh /home/u765133174/domains/eurofinsmy.net/public_html/eurotracks/public/script.sh
and the script.sh is
#!/bin/sh
cd /home/u765133174/public_html/eurotracks/public && php artisan schedule:run >> /dev/null 2>&1
What did i did wrong? My guess is there's something wrong with the script but I couldn't quite figured how. The automated email just wouldnt send out after hosting

I had same problem on hostinger. From that I have resolved with one trick inside project.
I have created get route for cron execution and created a php file on root path of project.
Ex : in web.php create one route of type get form call cron like
Route::get('/cronsStartToWorkEmailSend', function () {
\Artisan::call('emailsending:cron');
return true;
});
In root path of project create one file called as cronsStartToWorkEmailSend.php and add a code for curl call for cron job run.
<?php
$url = 'https://your_project_domain/cronsStartToWorkEmailSend';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
?>
And Now you can register cron job with type php selection from hostinger to call your cronsStartToWorkEmailSend.php file
So basically what happens from this code structure.
When php file called via daemons on server on regular interval of time cronsStartToWorkEmailSend.php will call.
When cronsStartToWorkEmailSend.php call at that time our route cronsStartToWorkEmailSend will call.
When route cronsStartToWorkEmailSend will call at that time with help of this command \Artisan::call('emailsending:cron') our cron will be triggered.

Related

Sending HTTP request through cron job

I have a PHP file that is deployed in a server:
/var/www/html/cron/leave_mail.php
The code inside this PHP file is just sending a request via CURL (The server of the url is the same server of the cron job):
<?php
$url = "http://my-site.build.com/sendmail/sendmail_leave";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
curl_close($ch);
?>
Now I created a cron job on the same server where I deployed the PHP file:
* * * * * php /var/www/html/cron/leave_mail.php
After 1 minute, it didn't execute the command inside the PHP file, however, I can see on the system logs that the cron job runs.
I run my local virtual machine with ubuntu os and created the same cron job, executing the same file. After 1 min, I can see that it executes the command inside the PHP file because the data on the database was updated.
I'm really puzzled on why the cron job on the server didn't executes the PHP command. I'm stuck on this issue.
Start with finding where PHP is:
which php
Then update the crontab by running the following command:
crontab -e
Then update the line with the correct PHP path:
* * * * * root /usr/bin/php /var/www/html/cron/leave_mail.php
Also note that you can specify the user before the path.
Hopes this helps a little.

How to do curl in Php to trigger and send a file to jenkins job (which accepts file as parameter)

I have to trigger a job on jenkins and upload the file at same time (Jenkins has file parameter set) from PhP page.
I know we can easily do this with curl unix command specified in jenkins Remote Access API. but I want to use php curl to trigger the job at same time upload the file.
I have written following code but it doesn't work .
<?php
$baseUrl="197.10.2.1:8080";
$jobName="exampledatacopy";
$json='{"parameter": [{"name":" /var/lib/jenkins/file/workspace/1.txt", "file":"#/home/kark/Desktop/1.txt"}]}';
sprintf('%s/job/%s/buildWithParameters', $baseUrl, $jobName);
$curl = curl_init($baseUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$response=curl_exec($curl);
curl_close($curl);
?>
above code successfully triggers the job on Jenkins. Output of the job is as follows.
Started by user anonymous
Building in workspace /var/lib/jenkins/jobs/file/workspace
Finished: SUCCESS
but file is not uploaded. $response prints a response showing status of all jobs

PHP Curl script works from browser, doesn't work as Cron Job

I have a very simple PHP script that is supposed to make a POST request. The code is the following:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
mail('myemail#gmail.com','Script run with success','Script run with success',$headers);
When I execute it from the browser, it works fine. However, when I try to execute it as a cron job the Curl part won't work. The rest of the script works even as a cron job, as I receive the confirmation email at the end.
Here's the cron entry:
*/5 * * * * /usr/bin/php /home/username/scripts/test.php
Any clue regarding why Curl is not executing as a cron job?
Update: I tried to run the script via shell and the Curl part failed to run too. So:
Browser: curl works
Shell: curl doesn't work
Cron: curl doesn't work
Update 2: Adding -dsafe_mode=Off while running via shell make the script run fine. However adding the same flag to the cron entry didn't do anything. So I still need to figure out how to make it work from cron.
It turned out to be an authentication problem. I solved it by adding the following two lines to the CURL configuration:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Hope it helps someone.

Creating a cron job in openshift

I have created an application in openshift. I have a cron which should run every minute since it is placed in minutely folder inside cron. But it never runs. Its a php script which hits a url using curl. Any idea
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
I created this script and placed it inside minutely folder in .openshift/crons folder. Then I restarted my application. But it doesn't work. Any idea?
You will need two files.
1.: THE CRON FILE
It is a script, that will execute your PHP script. You need to place it in the minutely folder. Let's name it "crontest.sh", so the full path will be this, where the 000000000000000000000000 is your own OPENSHIFT_APP_UUID:
/var/lib/openshift/000000000000000000000000/app-root/runtime/repo/.openshift/cron/minutely/crontest.sh
The file contains only this line:
php $OPENSHIFT_REPO_DIR/php/crontest.php
2.: THE PHP FILE
It is your PHP script, that will be executed every minute by your Cron script. You need to place in the same folder, that you have specified in your Cron file. Let's name it "crontest.php", so the full path will be this, where the 000000000000000000000000 is your own OPENSHIFT_APP_UUID:
/var/lib/openshift/000000000000000000000000/app-root/runtime/repo/php/crontest.php
The file contains your PHP script, e.g. this will make a file named "crontest.txt" showing up next to your PHP script, containing as many "1" as the number of the passed minutes is:
<?php
file_put_contents(getenv('OPENSHIFT_REPO_DIR').'php/crontest.txt', '1', FILE_APPEND);
?>
To answer SanksR's specific question, the PHP file will contain the code below in the "app-root/runtime/repo/php/crontest.php" file, while the "app-root/runtime/repo/.openshift/cron/minutely/crontest.sh" will contain this: "php $OPENSHIFT_REPO_DIR/php/crontest.php".
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
You have to write a shell/bash script and place it in the minutely folder.
This script has to run your php file. It could look like:
myscript.sh:
#!/bin/bash
export PHP=/usr/local/zend/bin/php ;
$PHP my-curl-cron.php
(don't forget to make it executable: chmod +x myscript.sh)
I recommend to read this article along with this tutorial.

shell cron job to run a php file

I need to create a cron job where I only have shell access. I know how to do it with cPanel, but this is on 1and1 server so I must use shell.
I got to the part where I add a new cron job in the crontab as per 1and1 FAQ, but I'm not sure what I'm doing wrong because the cron job isn't running at all.
This is what I'd normally put in cPanel:
0 8 * * 1 wget 'http://my_www_domain/googlefroogle.php?feed=fy_uy_tp&key=XXXXX3f7d1'
This is what I have in my crontab now:
0 8 * * 1 /usr/local/bin/php /homepages/XX/XXXXXXX/htdocs/googlefroogle.php?feed=fy_uy_tp&key=XXXXX3f7d1
What am I missing or doing wrong? BTW, if I manually enter the URL in my browser, everything works fine so I'm sure the script is OK.
As far as I know, you can use wget on a cronjob on any server, so you there should be no problem if you keep using it that way.
Now, the reason it doesn't work it's because you can send GET params to a PHP file that way. PHP handles command line in a different way. See Using PHP from the command line.
So you must check if the php file is called from a url or from command line (it's easy, look at php_sapi_name) and, if that's the case, get the parameters using the command line interface or define them in your script.
if (php_sapi_name() == 'cli') {
if ($argc != 3) {
die(sprintf('Usage: %s <feed> <key>', $argv[0]));
}
$feed = $argv[1];
$key = $argv[2];
}else{
$feed = isset($_GET['feed']) ? $_GET['feed'] : null ;
$key = isset($_GET['key']) ? $_GET['key'] : null ;
}
Now you have to call the script like this on your cronjob:
php -f file.php fy_uy_tp XXXXX3f7d1
What it does, is that if the script is running from command line, it gets the arguments as variables, if not, then it check for the GET parameters to exists and use them.
You can still use your cPanel "command" in crontab manaed via shell.
use
crontab -l
to list current cron jobs.
crontab -e
to edit crontab.You will need to install crontab manually on some minimal installations.
try
*/0 */8 * * */1 /usr/local/bin/php -f /homepages/XX...
You cannot run a file with parameters like that, because the shell is looking for a file named "file.php?param=value", not "file.php".
if the parameters are always the same, you can set them by force in the script:
<?php
$_GET['feed'] = 'fy_uy_tp';
$_GET['key'] = ...
$_REQUEST = $_GET;
?>
and then check that the script doesn't need some other variable, such as $_SERVER['REMOTE_ADDR'] or things like that.
None of these answers helped me do what I need so I ended up using the following php file which was called by the crontab:
$data = array('feed'=>'fy_uy_tp','key'=>'MY_KEY_HERE');
$url = http_build_query($data, '', '&');
$fullurl = 'http://www.MYSITE.com/googlefroogle.php?'.$url;
$ch = curl_init($fullurl);
$fp = fopen("MY_LOG_FILE.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
In case anyone else needs something similar... Also, note that this requires php5, the http_build_query doesn't work on php4

Categories