I am attempting to use cron to run a PHP file that executes a series of actions. I've done this before with simple procedural PHP, the trick then was doing the cron like this:
/usr/local/php5/bin/php /home/me/fullpath/turnrun.php
and having this at the very top of the PHP file I wanted executed.
#!/usr/local/bin/php -q
I'm using CodeIgniter 2 and Doctrine 2, so I have controllers and classes and all kinds of fancy stuff now, and I can't figure out how to make this work.
When I attempt to run a 'backend_test_turn.php' file I get an error
Parse error: syntax error, unexpected T_STRING in
/home/me/fullpath/application/controllers/backend_test_turn.php
on line 59
This file includes
class Backend_test_turn extends CI_Controller {
// code
$logmsg = new ORM\Dynasties2\Systemlog; // <--- this is line 59
// code
}
Note that when I run this normally (in a browser), the code works fine and does not throw an error.
What am I doing wrong?
You can run the controller code directly from command line like this:
php index.php Backend_test_turn/action/data
OR
php index.php Backend_test_turn action data
You can find more help here.
you can try
$logmsg = new ORM\Dynasties2\Systemlog;
$logmsg = new ORM\Dynasties2\Systemlog(); // <- see difference
Did you try to use CURL instead?
I have an application CI & Doctrine with CRON jobs and I've got many errors like yours while trying to launch controllers via CLI.
Personnaly now I use CURL instead of CI CLI and it works like a charm.
Related
I have an App\Jobs\BanUser job which accepts a parameter id to block a bad user by passing their id. That works perfectly if called from a controller like this:
dispatch(new BanUser($id));
But when I try to do that from the tinker command line with the full namespace like this:
dispatch(new App\Jobs\BanUser('1'));
I get this error:
PHP Fatal error: Class 'App/Jobs/BanUser' not found in Psy Shell code
on line 1
Any idea how to accomplish this job with the passed id parameter from the command line?
NOTE: If the solution requires starting up a queue from command line just for this job, maybe it's best to set it as an event? I don't want it to queue, just want to be able to run it in real time by passing parameter and executing the BanUser respective code from command line for either job or event.
Make sure you have correct namespace App\Jobs in your BanUser class.
Also check BanUser class file is placed in right directory.
try
app('Illuminate\Bus\Dispatcher')->dispatch(new App\Jobs\BanUser('1'));
I am not sure if it will work but, you can give it a try.
I want to run custom php code in laravel directly without using any routes or http requests..
I hope I can make it clear, I mean, like those online tools that runs php code by writing php code in browser, and then run it, and view result..
I found this handy project (Run-PHP-Code) to run PHP in browser directly, but I can't use models of my laravel project in PHP code..
How can I include laravel 's environment, so that I can for example:
$tag= new Tag;
where Tag is a model in laravel project, that would result into:
Fatal error: Class 'Tag' not found in D:\xampp\htdocs\widgetsRepository\app\controllers\Run-PHP-Code-master\index.php(49) : eval()'d code on line 3
Any idea? this would be very useful!
EDIT
I tried Brian suggestion at his answer, but I got this error now:
Call to a member function connection() on null
at vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
public static function resolveConnection($connection = null)
{
return static::$resolver->connection($connection);
}
so, I think I only need to get database sorted, then I can do experiments easily..
I've never tried to run code from a laravel project directly, I just copy and paste parts of the code into Run PHP Code.
That being said, it should be possible to run the code using the method taken from this StackOverflow question:
The Composer autoload script, to autoload all of your classes:
require __DIR__.'/../bootstrap/autoload.php';
And if you need things from the IoC container, you'll:
$app = require_once __DIR__.'/../bootstrap/start.php';
Then you will be able to do things like:
$post = Post::find(1);
Hi I am facing some problems in writing a cron job using CI CLI way. My application has a controller name called manager.php in that there is method called check_status where I am gonna get all the order_ids using one model function. Ever order_id row had a status filed in database which either success or failure.
I have an api if i pass order_id to that it will tell whether order is successfully delivered or not. But here comes the problem I have below line in controller in the top.
<?php if(! defined('BASEPATH') ) exit("NO Direct Script Access Allowed"); ?>
So when i try to run method check_status from CLI in CI it gives me an error stating NO Direct Script Access Allowed.
This is the way i called above method php application/controllers/manager.php check_status
So i decided like this i created an another class file called cron_job.php in that i didn't keep the above error line "No Direct Script Access Allowed". I thought it will give access now when i try to run but it doesn't give an error and even output also.
This is the class which i created and method in that.
<?php
class Cron_job extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
I run this controller form CLI like this php application/controller/cron_job.php message
Note: I am in ROOT directory.
No Output at all. So i tried in another way like this php index.php application/controller/cron_job.php message
Now it gives me error stating that Error 404 page not found.
What i tried in another way now i created a file in views folder and in that i am calling old controller/method url like below.
$result = file_get_contents("http://application_path/controller/method");
echo $result;
Now i am getting output which i defined in the method check_status in manager.php controller.
But here comes another problem now after the above line i will get an array output which had all the order_ids.
I am gonna send this each id to a api to check status. If it is failure it will check whether it is delivered or not. If it's done i need to update that status in the database against that order_id. But now i am in view file, is it possible to call a model file from the view file or is there any way to do this.
Any help?
Note: There is no syntax errors in any controller or any method , which are fully verified and working normally when i am accessing using urls.
You need to read the CodeIgniter help section on Running via the Command Line. It's very easy. Your original approach was correct. But you do not call your controller method directly by its path, instead CD to your project root and then the call the index.php file with the controller and method as parameters.
// This is how you call CI via the command line.
// Use spaces between index.php and your arguments.
$ php index.php <controller> <method> [params]
// And in your instance
$ php index.php manager check_status [param1 param2 param3]
Depending on your host you may need to call the PHP version compiled for CLI.
the ci helper will help you in this.
1 ) Create a helper & create a function in it, that calls your model function
function getUserDetails($userId = '') {
$CI = & get_instance();
$getUserDetailsByUserId = $CI->user_model->getUserDetailsByUserId($userId);
return $getUserDetailsByUserId;
}
2) Now you can call getUserDetails($userId); in your view.
Here's the situation:
I have a catch-all on my domain email (so *#domain.com) redirecting to a piping script located at /home/domain/scripts/piper.php. This piper script is not within the Kohana ORM, but all of my other files are. I want to try to use Kohana inside this piper.php file.
I have tried (unsuccessfully) all of the following:
Including Kohana
I couldn't figure out what needed to be included, and more importantly how to override the url variable that Kohana uses to determine the right controller. Also, this is a catch-all piper, so it isn't using HTTP (to my knowledge), so much as executing a command.
Piping
I tried piping to the following:
/home/domain/public_html/index.php --uri="piper"
But cPanel makes this impossible, as you can only specify the destination script, and not the proper flags and such (unless I am missing something).
PHP exec()
I tried using the following line:
exec("php /home/domain/public_html/index.php --uri=\"/piper\"")
I was hoping that the stdin data would be maintained across the exec() command, but I could never get it to recognize the uri command, though I can run this on my localhost and it works just fine.
I was using http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/ as a reference, but can't get anything to work.
I'm happy with either one of these solutions such that I can see an incoming email, parse it, then send emails based on the parameters.
Let me know if you need more information! I'm le stumped.
/home/domain/public_html/index.php --uri="piper" would be a valid way to do it. If your host sucks and doesn't let you specify that, put it into a bash script instead and reference that.
If you are on any recent version of kohana (3.2 or 3.3), a better way to do this would be to use Minion to run the command line task. This is what Minion was designed for.
All you need to do is to:
modify your piper.php script to be a valid PHP class;
place it in /application/classes/ folder;
Kohana will automatically load your class file (like include) during initialization.
Then you can use your piper class as usual class by $piper = new Piper; ....
UPD
You have to serve your emails trough Kohana.
Create controller, for example pipe (route it with /pipe URL):
public function action_pipe() {
$pipe = new Pipe; // This creates new Pipe object (your emails serving class)
$pipe->serve(); // Sserve emails within `serve()` method of Pipe class
}
Although admittedly, I'm not sure if these other answers are correct because I can't figure out how to reproduce the results.
What ended up working for my situation was to create a Controller_Piper class that is called in the /home/domain/scripts/piper.php. What I did was to copy the code from /home/domain/public_html/index.php and changed the following:
echo Request::factory("/piper")
->execute()
->send_headers(TRUE)
->body();
This loads the piper controller and executes everything very nicely. Not sure if it's the cleanest, but it does work.
I'm setting up my cron job controller that will only run within the CLI, I've not started with anything built, just in the testing phase with CI's examples. However, when running it I get no output or anything else, just a new line, this is the command I ran:
root#serv$ php /var/www/ci/index.php tools message
root#serv
As you can see in the second line, I get no output, just a new line to run an command but I don't understand why and I cannot debug it. The code contains this:
<?php
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
In my configuration file, the $config['uri_protocol'] is set to AUTO so this does not seem to be the problem.
How can I debug this? What are the options that I may need to deal with?
I also have display_errors on and error_reporting on to E_ALL.
I found the problem, it was a problem with a redirect('domain.com'); exit; I had on an autoloaded library, because it was matching against the domain in a database, that way CLI doesn't serve a domain when it detects, hence I included a redirect('domain.com') with an exit which is why I'm not seeing any output.
I also encountered this while i was playing w/ cli for codeigniter. It took me a while to troubleshoot the issue and found out that I forgot to except my controller on my login model which runs a redirect function.
This may be a silly suggestion, but might as well give it a shot... #lolwut, what if instead of using "echo", maybe you have to "return" the output?
I have an application following this guide: http://codeigniter.com/wiki/Category:Advanced::CronScript running (and producing output). CI was only 1.7.2 when I did so, but it might still hold
(CodeIgniter 2.2.0)
Add a route in /application/config/routes.php
for...
<?php
class Cron extends CI_Controller
{
public function process_dumps()
{
echo "Processing dumps..." . PHP_EOL;
}
}
?>
add...
$route['cron/process_dumps'] = 'cron/process_dumps'
Without this line there was no output on the CLI!
I think you can find the answer in the Codeigniter Wiki.
By calling to the cron.php with parameters controller and function, and then define CRON_CI_INDEX to the refer to the file path of your main index.php.
For example,
php /var/www/ci/cron.php --run=/tools/message