How Execute PHP File Using a Cron Job - php

I am using a WordPress site hosted on a DigitalOcean WordPress Litespeed droplet. I am attempting to run a php file in my theme every five minutes using a Cron Job. I don't want to use the default WordPress cron job system.
So far I have connected to the sever through ssh. Then entered the crontab using "crontab -e" then entered the following code then saved / exit
*/5 * * * * php /var/www/html/wp-content/themes/my_theme/cron.php
And inside the cron.php I put a simple mailing function for testing purposes:
<?php
/*template name: Cron*/
$content = date("h:i:sa");
wp_mail( 'reece.r.barrett#gmail.com', 'Cron Test', $content, array());
?>
But I never receive an email so I assume its not working. Is there something wrong with the way I am declaring my cron job? Also I haven't disabled the WordPress Cron system like I have seen a lot of tutorials doing. Is this required to get my own cron to work?
This is how I've seen some tutorials disabling the WordPress cron in wp-config
define('DISABLE_WP_CRON', true);

Your problem is that even if your file physically live inside your theme folder it is actually run "outside" wp core ENV since you are calling it directly. If you ask me, the best, simpliest and cleanest way to use external cron system inside WP ecosystem is to do something like this:
add_action( 'wp_ajax_nopriv_my_cron_action', array( 'myCronAction' ) );
function myCronAction(){
//...your code
}
In that way you make use of WP ajax endpoint to enter the WP ecosystem. Be careful with security since in that way you are exposing a cron url to do your stuff. Securing that is up to you, but that's the easiest way to achieve that.
To use that from your cron you just need to make something that fetches an URL built like this: https://yourdomain.com/wp-admin/admin-ajax.php?action=my_cron_action
If you don't want to make it like that, then you have to bootstrap WP core yourself inside your .php file see https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files

First check if your script run from cli/webhook. Then please check for cli execution, since there could be the case that php is not in server's PATH and you have to call full path to php, e.g.:
/usr/local/bin/php /home/your_username/public_html/path/to/cron/script.php

Related

Run php script for each call in FreePBX

I want to run a php script before each call in freepbx. also i need destination gateway ip and did. this is the first time that I try to do this function and I don't have any idea for this.
First of all run php script is bad idea, it slow, will slow down your pbx.
But if you insist you can add it via asterisk System call into predial-hook.
You can write hook in extensions_ovveride_freepbx.conf
List of hooks in current freepbx version is:
[macro-dialout-trunk-predial-hook]
[macro-dialout-one-predial-hook]
[macro-dial-hunt-predial-hook]
[macro-dial-ringall-predial-hook]
[macro-dialout-dundi-predial-hook]
you can do it without any configuration in asterisk configs.
first you have to install DialPlanInjection module on your freepbx and then route the calls to the php file in this path : /var/lib/asterisk/agi-bin

Global Library in Crontab PHP

Is it possible to include global $_lib, $_SETUP; in crontab?
I have a cronjob writting in php file in internet directory (/internet/mycrontab.php), but seem like the crontab throws error when I using $_Lib like in $_lib['db']->db_fetch_object($query).
The $_Lib working fine if I enter the url directly in browser www.myweb.dom/internet/mycrontab.php, and the crontab also working fine if I remove the $_Lib like in $_lib['db']->db_fetch_object($query) by using hardcode sysntax (primary).
If it is possible to include global $_lib, $_SETUP;, how do I correctly do that?
Many thanks for the help.
The problem is that the crontab environment and your web app environment are different things.
The cronjob is run by php-cli while the app is run by apache (or NGINX, whatever) php module.
You should probably evaluate to include your library into the crontab file.
include "/path/to/your/library.php";
$_lib = "whatever";
$_SETUP = "whatever";
Without having a proper look at the code that's the best I can suggest.

getting Could not open input file when trying to call php script from shell_exec

I am trying to build a small custom task scheduler. Basically the idea is I have cron run my process script, which looks in the database and finds any scheduled tasks that are ready to run, and runs them. So I think the best way to do this would be to try to launch the tasks "in the background" by way of shell_exec and using > /dev/null, which I understand makes it so the initial script (the process script) doesn't wait for the task scripts to complete.
So first, if there is a better way to achieve this, I'm open to suggestions. Though note I am on php 5.3 so there may be some options in 5.4 and up that I don't have access to :(
However here's the question at hand:
I am testing on WAMP on my windows machine and I am trying to make a call that looks like this:
shell_exec("php $path$base_url$querystring > output_test.txt 2>&1 &");
$path is the full windows path to the script
$base_url is the base url of the script I am calling
$querystring is of course the query string being passed to the task script
I am also outputting to output_test.txt which creates such file in same directory, where I get the following error:
Could not open input file:
C:\xampp\htdocs\email\batch_email_send_u2u.php?dealer=7
Yes I realize the path references an xampp installation, but that is not the issue - all the wamp files are executing from there and everything else has worked like this for years - it was just set up this way to support a legacy setup.
It seems to me shell_exec is locating and running php, it's just that it can't open the referenced script. Can't figure out why.
Also I need to eventually get this working on a real linux server so any advice on how to make that happen would be greatly appreciated!
Found a solution! Special thanks to dan08 for getting me set on the right path.
Ultimately I found the answer in this thread: Pass variable to php script running from command line
I ended up using the argv[] array as described in that post and with a little tweak to the script I'm calling it works like a champ now.

Creating Cron Jobs in CakePHP 2.x

I have attempted to create a cron job in my CakePHP 2.x application. But all of the resources I have read online seem to be either doing it completely different to one another with little consistency or explain it in very complex terminology.
Basically I have created the following file MyShell.php in /app/Console/Command
<?php
class MyShell extends Shell {
public function sendEmail() {
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->from('cameron#driz.co.uk');
$email->to('cameron#driz.co.uk');
$email->subject('Test Email from Cron');
$result = $email->send('Hello from Cron');
}
}
?>
And I want to say run this code at midnight every night.
What do I do next? As the next part really confuses me! I have read on the Book at: http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html that I should run some code in the terminal to make it do it at a certain time etc. And I can set these up using my hosting provider rather easily it seems.
But I'm rather confused about the Console directory. What should go in what folder in here: https://github.com/cakephp/cakephp/tree/master/app/Console
/Console/Command
/Console/Command/Tasks
/Console/Templates
Also noticed that many of the files are .php (e.g. my Shell file is also .php), but according to documentation I've read for Cron jobs, the executed files should be .sh?
Can anyone shed more light on this?
And what would the code be to call that command?
e.g. would presume this is incorrect: 0 0 * * * cd /domains/driz.co.uk/html/App && cake/Console MyShell sendEmail
Thanks
No. There is no way to do it just in PHP. But that doesn't matter, because crons are easy to set up.
In that article you linked to, you still have to set up a cron - the difference is just that you set up a single cron, that runs all your other crons - as opposed to setting up one cron per job. So, either way, you have to learn to create a cron.
The instructions depend on your server's operating system and also what host you're with. Some hosts will have a way to set up cron jobs through a GUI interface like cPanel or something, without you having to touch the terminal.
It's usually pretty easy to find instructions online for how to set up cron jobs with your host or server OS, but if you're having trouble, update your question with your host's name, and your server OS and version.
Also ---------------------------------
Often in cron jobs you'll be running a shell script (.sh). But don't worry about that for this case; your's will end in .php.
Re: the directory structure:
/Console/Command is where your new file should go.
If you're doing a lot of shell stuff, you may want to abstract common code out into the /Console/Command/Task folder. Read more about that here. This probably won't be needed in your case.
/Console/Command/Templates is where you can put custom templates for the Cake bake console - don't worry about that for now.
If I've only got a couple of cron jobs to run, then I create just one file called CronJobsShell.php, and put them all in there.
Really, you should read Cake's documentation on shells from start to end. It will give you a nice picture of how it all hangs together.
This can be done very easily by the following steps -:
1) Create a shell let's say HelloShell.php in Console/Command
<?php
class HelloShell extends AppShell
{
public function main()
{
//Your functionality here...
}
}
?>
This shell can be called by Console/cake hello
2) Write the command crontab-e .This will open up the default editor or the editor which you select Now as we want that our shell should run after at midnight write:-
0 0 * * * /PATH TO APP/Console/cake hello
For better understanding refer https://www.youtube.com/watch?v=ljgvo2jM234
Thanks!

Running Wordpress PHP from command line - specifically for Plesk scheduled task

I need to develop a script using Wordpress PHP that will run with the Plesk Scheduled task. I've seen a few topics with this on stackoverflow and the web, but I can't seem to get the wordpress or script to work. I'm fairly new to this, so I need a good step-by-step guide.
Basically, I want to run a PHP Script using the command line. (what's the command to do this?... I had "php -q /path/to/file.php" but not sure if that's it.)
Secondly, with this script, I need to be able to use Wordpress commands such as query_posts, add_post_meta, get_post_meta, etc.
I've seen elsewhere that I need the following at the top of the php file:
#!/usr/bin/php
<?php
$_SERVER = array(
"HTTP_HOST" => "http://example.com",
"SERVER_NAME" => "http://example.com",
"REQUEST_URI" => "/",
"REQUEST_METHOD" => "GET"
);
require_once('/wp-load.php');
require_once('/wp-blog-header.php');
Is that it, or do I need more? I assume I can place the PHP script in my root Wordpress directory and run it from there?
I've got the entire PHP part working, and can run it if I include it in a Wordpress page and simply load that page. So I know the Wordpress part works.
I just need that other portion to get it working with the scheduled tasks.
Thanks in advance for all your help.
Yes that's it. Actually, all you need to do is set HOST_NAME.

Categories