I am using xampp with my local window system.
now a day I am working with codeigniter, I am trying to run a mycontroller function as a background job. Like
class Admin extends MX_Controller{
function __construct(){
parent::__construct();
}
function index(){
echo "working";
$command = "D:\xampp\php\php D:\xampp\htdocs\client\newslatter\index.php admin preget";
echo $out = exec( $command);
echo "here";
}
function preget(){
echo "<br/>Done!!!!!!";
}
}
I am not able to run a function using exec command can any one help out my problem?.
CodeIgniter has a page in it´s manual that is exactly about this:
http://ellislab.com/codeigniter/user-guide/general/cli.html
Note that, in your command, you just put the path to the file, you didn´t add "php"
$command = "php D:\xampp\php\php D:\xampp\htdocs\client\newslatter\index.php admin preget";
Maybe this solve your problem.
Is php.exe in the "Path" enviroment variable of your windows?
The process should be pretty easy: you run > "cmd" in Windows and navigate to your CodeIgniter project.
$ cd /path/to/project;
$ php index.php YourController ControllerMethod
I see you're using windows. To do this in Windows, I believe 'exec()' works by just calling like how you do on your command prompt using the "php" command, assuming that you have "php" installed as your environment variable.
So your $command would be
$command = "php D:\xampp\php\php D:\xampp\htdocs\client\newslatter\index.php admin preget";
However, FYI, this is a synchronous call.
If you want to do an async one (which a lot of times you would want), it doesn't work this way.
I did some research previously as I had this problem. This is probably what you're looking for when you wanna run exec on Windows with async.
$WshShell = new COM('WScript.Shell');
$oExec = $WshShell->Run('php D:\xampp\php\php D:\xampp\htdocs\client\newslatter\index.php admin preget', 0, false);
Hopefully that helps?
Thomas
Related
I must update my currency rate and I have this in my Plugin.php:
public function registerSchedule($schedule) {
$schedule->call(function () {
$url = "https://cbu.uz/ru/services/open_data/rates/json/";
$json = json_decode(file_get_contents($url), true);
file_put_contents("currency.json", json_encode($json[0]['G4']));
})->everyMinute();
}
I ran my cron job in cPanel
/usr/local/bin/ea-php72 /var/www/u1041398/public_html/agroparts.uz/artisan schedule:run >> /dev/null 2>&1
The path is correct I checked the PHP version it is correct also. My cron codes in plugin.php are correct also but it is not updating the currency rate. Is this command above correct to run cron job? I checked my code without cron and it worked successfully.
Cron job was not showing mistake. So, I created console command to update currency rate and run it and it showed
file_get_contents(): https:// wrapper is disabled in the server configurati
on by allow_url_fopen=0
So I used curl to get currency rate and worked
I'm creating a custom command which will truncate a table every thirty minutes in the console kernel (for development purposes). I want to run another right after the previous command.
P.S: I have an if statement which prevents running these commands on the production server.
$schedule->command('db:seed')->after(function () use ($schedule) : void {
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->environments(['demo', 'local']);
});
I expect to run the seeder right after "my-command" runs successfully every thirty minutes. However, in this way, only db:seed runs.
If you want to run B after A you need to schedule A and AFTER that run B:
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->after(function() {
$this->artisan->call('db:seed');
});
I have checked the source code for Illuminate\Console\Scheduling\Schedule class.
I think when we say:
$schedule->command(...);
The artisan command will be scheduled, not run straightaway.
So when you write like this:
$schedule->command('first-command')->after(function () use ($schedule) {
$schedule->command('second-command');
});
The second command will be registered, not run right after the first command.
So the best approach that I can think of is run the second command inside the first command according to this link
You might try something like this:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RemoveUsersFromTable extends Command
{
public function handle()
{
// Do something to remove users from table.
$this->call('db:seed');
}
}
An alternative that may be relevant in some cases (but I can imagine would also be a bad idea in others) is to run shell_exec: https://www.php.net/manual/en/function.shell-exec.php
If you want to chain two artisan commands together as you would on the cli using && you can simply shell_exec('php artisan command:first && php artisan command:second').
shell_exec returns the console output so if your commands print anything (ie, via $this->info) then you can print that to console like so: $this->info(shell_exec('php artisan command:first && php artisan command:second'))
I'm probably going to get a lot of hate for this answer...
I have a problem deploying my project with envoyer which executes an artisan-command I created.
The command gets all my users, performs another artisan command ($this->call('command')) and performs it actions by iterating through all the users.
The problems lies here:
foreach($usernames as $username) {
shell_exec('php ' . base_path('artisan') . ' command ' . $username . ' > /dev/null 2>/dev/null &');
}
This command starts a script in the background.
Its getting executed without any problems manually and doesn't end in a timeout (takes about 1s~ to execute) but in envoyer it wont stop running in the deploying step and fails in a timeout altough it executes flawless.
Additional informations:
For the reason why i'm running the script in the background:
The script im starting opens a socket which he will listen 24/7 until
the user cancel it manually.
I've just created a smaller example to make sure it's all working fine:
File forever.php will keep an infinite loop printing something every 5 seconds:
<?php
while (true) {
echo "I am still in the loop $argv[1]\n";
sleep(5);
}
File script.php will call multiple instances of forever.php and detach from the parent process (same as what you did):
<?php
for ($i = 0; $i < 5; $i++) {
shell_exec("php forever.php $i > /dev/null 2>/dev/null &");
}
When executing php script.php, obviously there's 5 instances of forever.php running. So your code seems fine (the part you've shown).
The only things I can think of in your case are:
1 second is too long for a script to run on Envoyer?
Your loop is a foreach on all your users. Could it be you're generating too many processes in that loop? How many users do you have?
Could you print your command before executing it, and try it directly on the terminal, to see whether it's all working fine?
Hope it helps, if you need more help please provide some more information.
How can i run these artisan commands in my application that is hosted in the net? Is there like a cmd in my cpanel where i can do these commands? Thanks in advance.
php artisan clear:cache
php artisan view:clear
Now in Laravel 5.8, you cannot pass object to call() func. You must pass an array [] as second argument to call() func.
Route::get('/clear-cache', function() {
$output = [];
\Artisan::call('cache:clear', $output);
dd($output);
});
Try this. You can clear all of laravel application cache hosted in shared hosting server that can not access ssh shell by the following code:
Route::get('/cleareverything', function () {
$clearcache = Artisan::call('cache:clear');
echo "Cache cleared<br>";
$clearview = Artisan::call('view:clear');
echo "View cleared<br>";
$clearconfig = Artisan::call('config:cache');
echo "Config cleared<br>";
$cleardebugbar = Artisan::call('debugbar:clear');
echo "Debug Bar cleared<br>";
});
Now run yourdoamin.com/cleareverything
This code does not throw any error. I already used this code.
Ref : https://laravel.com/docs/5.2/artisan#calling-commands-via-code
You can make a personalized route, and call it when you need it:
Route::get('/clear-cache', function() {
$output = new \Symfony\Component\Console\Output\BufferedOutput;
\Artisan::call('cache:clear', $output);
dd($output->fetch());
});
Another solution is to access ssh to your server and to run the commands.
You could create a simple bash script called clear-cache.sh like this:
#!/bin/sh
PHP=/path/to/your/php-binary
PATH=/path/to/your-artisan-install
cd $PATH
$PHP artisan clear:cache
$PHP artisan view:clear
Save the script and make it executable (chmod +x clear-cache.sh). Run it through a cronjob at specific intervals and configure the cron job to email you the output of those 2 commands. This way you'll get an email, every time the cron runs the script (basically the cron will automatically issue your two commands) and the ouput will be emailed to you.
Of course there are other methods as well like creating a php script and invoke it via web
I have setup CasperJS without a problem and have a script correctly configured & working when I execute directly via the command line - I would now like to have my PHP Codeigniter application 'talk-to' this script and be able to execute the same script via my web application. I am running XAMPP - although the final product will be deployed on a LAMP server.
To run the script (successfuly via the cmd I use the following:)
casperjs test.js
I have created a basic controller within Codeigniter with the following line but nothing seems to happen? Can anyone suggest what I am doing wrong?
public function run()
{
shell_exec('casperjs test.js');
}
The way I only allow a controller to be run by the command line is to check if it is a command line request. I validate this in the controller file.
if (!$this->input->is_cli_request()){
//redirect them to the homepage
redirect('', 'refresh');
}else{ //Request is coming from the command line }
To run a controller from a command line on a linux server use a command like this:
/usr/bin/php /var/www/website/index.php controller_name function_name
See this link for more information: http://ellislab.com/codeigniter/user-guide/general/cli.html