Creating a cron job in openshift - php

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.

Related

Cron jobs with laravel using hostinger

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.

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.

Silently call PHP-CLI script from PHP page

I have a number of PHP scripts that I run under PHP-CLI via cron jobs. All OK.
What I would like to do is, via a web page that I have already created, allow the user to manually run their own individual file by calling the .php file that contains their script (which is called via cron).
I already have the control panel and have a button where I want to be able to call the PHP script from, but I want it to be done 'silently' as the page contains data that I do not want them to see. Ideally I want to be able to call the PHP file and it run in the background and PHP-CLI.
So I guess my question is, can I execute a PHP-CLI script from a PHP page and have it execute without displaying any information to the user?
I have since found this works.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://URL/page.php");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);

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

sftp/scp files with bash

I have the need to upload a set of files to an sftp account.
My first thought was to use php's ssh2_connect() function and I was able to get this working locally no problem. However, once I moved to the dev environment I quickly realized that this wasn't a good solution because there are too many dependencies that wont exist and installing them would require too many approvals from too many people.
So, my next thought was to use bash, and this is where I need help. I will be running this bash script every hour through cron, so it needs to be unattended. However, when I run sftp/scp it requires a password.
The sftp account does not allow ssh connections so I cannot create authorized keys. I don't want to rely on the .ssh folder on the remote machine as well.
Any suggestions would be appreciated.
Keep in mind I cannot install anything, so no keychain, sshpass or expect. All other answers found in How to run the sftp command with a password from Bash script? are not feasible as I cannot install anything on the server.
Initially I was trying to use php's ssh2_connect() because php creates the file that I need to upload. It's better to have this sftp transaction in my php script for that reason but since it wasn't working, I moved on to bash.
My solution is actually using php and curl:
$ch = curl_init();
$fp = fopen($file, "r");
curl_setopt($ch, CURLOPT_URL, "sftp://USER:PASS#HOST/" . basename($file));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_exec($ch);
curl_close($ch);
check lftp, it's very powerful tool in file transfer. for example:
lftp -u "$username","$password" -e "cd /desc/path; put $FILE; bye" sftp://remote.example.com
check also mput, mirror in lftp manual.
BTW. I don't think you need to install anything on the remote server if you use expect. anyway, I am using lftp instead of expect in most similar situations at work.

Categories