Run php script for each call in FreePBX - php

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

Related

How Execute PHP File Using a Cron Job

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

Cron jobs and php scripts

So, i have a script that calls 2 other scripts that back up devices.
Problem is, If i call the main script manually that opens up the other two in a screen session, everything works fine.
The 2 scripts it calls basically sends data to an sql server via a php script.
IE
I have the line
$link = mysql_connect("server ip", "username", "password", "database");
Running manually (Which runs from my user directory) it works fine.
If i have the cron job run, it will run fine to a point when it gets to the php section of the script it gets the error
PHP Fatal error: Call to undefined function mysql_connect() in /export/home/myname/my_Scripts/php_script_its_running
So, being that it works correctly if i manually run it, and not when i run it via cron, I am assuming it is either running a different php instance due to it being run by cron or something oddball like that? or, idk, not 100% familiar with cron jobs yet.
So while i was debugging it more, it looks to be using a different version of php than what my user account is using, 5.1 comparred to 5.4
Not sure if there is an easy way to change it to use my user accounts php setup? Unfortunately i have no root access to the box.
Answer
So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash
PATH = /usr/kerberos/bin:/app/php-5.4.3/bin
Answer So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash PATH = /usr/kerberos/bin:/app/php-5.4.3/bin

manually call cron job action programmatically in shopware

I'm new in shopware.
I want to call cron job function programmatically is there any way ?
I have tried following for call function using command line but getting too many argument error.
php bin/console sw:cron:run ACTION-NAME
php bin/console sw:cron:run NAME -f
I need one more help is use shopware function out side of shopware my custom file. is it possible ?
Thanks in advance.
You can run the specific cronjob by following command.
For example:
php bin/console sw:cron:run Shopware_CronJob_ClearHttpCache
But it's only possible since Shopware 5.1.6, not as Shopware says since 5.1.5.
In case you need just run cron job for testing, you can run cron from browser:
http://www.myshop.com/backend/cron
There need to replace myshop.com to your domain. In this case all cron jobs will be handled by opening this link.
In case for some reason you need to run cron via console, you can do it this way:
php5 path_to_shopware/shopware.php /backend/cron
There need to replace path_to_shopware to your server path.

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.

How to set CRON using php and run php file with the help of it?

I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.
Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file.
And this file run every time which time is stored by user in database.
By using Cpanel I can do that but I don't want to use cpanel.
Please give me some proper and working solution, I really need it.
Thanks in advance.
Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.
From within the crontab you can use one of two strategies to run a php script_
- call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable.
- use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.
This might act as a starting point for you to experiment with:
#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');
// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab",
implode(' ',$time),
'/usr/bin/beep');
// execute command
$result=system($command);
// output result of execution
if ($result)
echo "Result: $result\n";
else
echo "FAILURE!\n";
?>
Works for me when called from CLI.

Categories