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);
Related
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.
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
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.
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.
I write curl php script which work is download csv file from one website after succesfully loged in. It works fine when i start it in my browser but it fails when i put it on cron jobs list. I seen memory exhausted error in my log once, so i guess my server give me less memory for cron.
How can i go around this problem?
You can see part of the code, which is doing download work, it's just usual :
<?php
...
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
$fp = fopen("data.csv", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
Just want to say again, this is part of the code and everything works fine in browser and fails in cron jobs.
Assuming that you're using standard configuration of CRON, your cwd (current working directory) is ~/ or / or something like that, however when you're running script from the web your cwd is set to dir( __FILE__) (current script directory).
Say you're using script /var/www/web1/scripts/script1.php, than:
webs cwd = /var/www/web1/scripts/
CRONs cwd = /var/www
When you use relative file name such as data.csv (without / at the beginning) full path is created as CWD . $filename, therefore:
webs full path /var/www/web1/scripts/data.csv
CRONs full path /var/www/data.csv
You can easily avoid this by using:
define( 'PATH_ROOT', dirname( __FILE__) . '/');
// ...
curl_setopt ($ch, CURLOPT_COOKIEJAR, PATH_ROOT . 'cookie.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, PATH_ROOT . 'cookie.txt');
// ...
$fp = fopen( PATH_ROOT . "data.csv", "w");
Also make sure your system user is connect and script will run at all (check CRON long or send yourself an email).
Usually this means that you're configuration of PHP (php.ini) is not the same when calling your server and when calling by command line. I'm not sure it's a best practice but I usually make my cronjob calling a wget on my scripts to be sure they are running in the same environment I tested them i.e. through a webserver.
# m h dom mon dow command
* * * * * wget -O - http://myapp.example.com/cron/run > /dev/null 2>&1
The * indicate that the command will be run at any :
minute
hour
day of the month
month
day of the week
The -O option tells wget to doesn't write the output of the script to a file
The "> /dev/null 2>&1" just redirect the ouput of the script to avoid the default behavior of cron (which is to send emails with command output to the users).
Just as a workaround, you could execute this in browser without having to modify the PHP script. For example:
30 * * * * /usr/bin/lynx -source http://mypage/status/cron > /dev/null
It'll behave just like in browser, and will avoid you having to mess around with code and environment issues.
Alternatively, you could use wget instead of that PHP script.
what about "php /home7/philbike/public_html/atlanticauto/assets/components/cronmanager/cron.php" command? Does it work in your terminal?
Finally get everything works with my first settings: cd /home7/philbike/public_html/atlanticauto/assets/components/cronmanager/ && php cron.php .
I should write path to csv file as it is without using system variables:
/home7/philbike/public_html/atlanticauto/data.csv