crontab not working in kohana freamwork - php

I'm using crontab to call my function.
but it is not working.
My function is working browser as well as in postman too.
====> MY FUNCTION
public function action_dateNotification() {
$sql = "INSERT INTO testcrown(name) VALUES('nitin')";
DB::query(Database::INSERT, $sql)->execute();
echo "inserted";
}
below url is working in browser and postman. When we invoke this url entry get save into database.
http://www.nkg.com/index.php/api/firebasenotification/dateNotification
I have set above url in crontab -e file
1 * * * * php index.php --uri=controller/api/firebasenotification/dateNotification
My project kohana version 3.1.3.1.

Use Kohana Task or:
1 * * * * wget http://example.com/controller/api/firebasenotification/dateNotification

Related

Laravel Artisan: Returns 0 for Python output

I have written one python utility program which returns an array of the result. This utility takes an image path as an input, process that image and in return gives an json array of the result.
Now I want to show this output on my website which is using Laravel 5.7
So I created one artisan command which executes python script and gives me the result.
This command works well in terminal but when I execute this same command from my controller using Artisan::call method, it shows me "0" as the output in the web browser.
I tried shell_exec() too. But the result is the same.
Here is my Artisan Command File
$process = new Process('python3 my_python_script.py -i ' . $this->argument('path'));
$process->run();
$result = $process->getOutput();
return $result;
Artisan Command
php artisan image:upload image_path
The call method of Artisan returns integer. It doesn't read from output or it is not your return value from handle method.
See:
/**
* Run an Artisan console command by name.
*
* #param string $command
* #param array $parameters
* #param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer
* #return int
*/
public function call($command, array $parameters = [], $outputBuffer = null);
Maybe you should try Artisan::output() but I don't think that it is thread safe or something like that.

Symfony 4 .{_format} not working

so I've got this route
/**
* #Route("/user/{id}/diary.{_format}",
* defaults={"_format": "json"},
* requirements={
* "id": "\d+",
* "_format": "csv|json"
* }
* )
*/
public function showRaw(User $user, Request $request)
{
}
So when I acccess /user/1/diary it works with whatever I write in the function but when I try to access /user/1/diary.json or /user/1/diary.csv I get an 404 error, so I guess the route parameters are not matching properly.
I would like to, depending on what the format is, return a diferent response but until now I can't get the format.
Thanks in advance for your help.
I assume you are using PHP's built-in development server without an explicit routing script:
[saizferri:public]$ php -S localhost:5000
or:
[saizferri:symfony_project]$ php -S localhost:5000 -t public/
When run like this, PHP processes each request by looking in the current directory (or root given with -t) for the requested resource. If it isn't found and PHP thinks it is a file—as it did in the case of /user/1/diary.csv—PHP will return HTTP 404; otherwise, PHP will search for index.php or index.html, and, if either is found, it is served with $_SERVER["PATH_INFO"] set appropriately—this is why you were able to view /user/1/diary. (See the description in the manual.)
Instead, tell PHP to route all requests through public/index.php:
php -S localhost:<port> public/index.php

PHP webhook script (bitbucket)

I'm trying to attempt using a webhook script so that I can just commit locally and have the script triggered server-side and pull in any change.
Now if I login to the server via SSH and run php webhook.php the cript is triggered successfully and the files are updated. So I know the file does work. But if I make edits to the files, commit and push to master, I'm not seeing the files updated.
The log file produced by the script suggests everything is fine, but clearly it's not.
My file structure is like this:
var
www
my-project
- webhook.php
repo-folder
So the file should be pulling the files into repo-folder and the webhook.php is set as the webhook via the bitbucket control panel. If I view the log within bitbucket, it's showing a successful request every time I push a commit.
The script:
<?php
date_default_timezone_set('Europe/London');
class Deploy {
/**
* A callback function to call after the deploy has finished.
*
* #var callback
*/
public $post_deploy;
/**
* The name of the file that will be used for logging deployments. Set to
* FALSE to disable logging.
*
* #var string
*/
private $_log = 'deployments.log';
/**
* The timestamp format used for logging.
*
* #link http://www.php.net/manual/en/function.date.php
* #var string
*/
private $_date_format = 'Y-m-d H:i:sP';
/**
* The name of the branch to pull from.
*
* #var string
*/
private $_branch = 'master';
/**
* The name of the remote to pull from.
*
* #var string
*/
private $_remote = 'origin';
/**
* The directory where your website and git repository are located, can be
* a relative or absolute path
*
* #var string
*/
private $_directory;
/**
* Sets up defaults.
*
* #param string $directory Directory where your website is located
* #param array $data Information about the deployment
*/
public function __construct($directory, $options = array())
{
// Determine the directory path
$this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
$available_options = array('log', 'date_format', 'branch', 'remote');
foreach ($options as $option => $value)
{
if (in_array($option, $available_options))
{
$this->{'_'.$option} = $value;
}
}
$this->log('Attempting deployment...');
}
/**
* Writes a message to the log file.
*
* #param string $message The message to write
* #param string $type The type of log message (e.g. INFO, DEBUG, ERROR, etc.)
*/
public function log($message, $type = 'INFO')
{
if ($this->_log)
{
// Set the name of the log file
$filename = $this->_log;
if ( ! file_exists($filename))
{
// Create the log file
file_put_contents($filename, '');
// Allow anyone to write to log files
chmod($filename, 0666);
}
// Write the message into the log file
// Format: time --- type: message
file_put_contents($filename, date($this->_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND);
}
}
/**
* Executes the necessary commands to deploy the website.
*/
public function execute()
{
try
{
// Make sure we're in the right directory
chdir($this->_directory);
$this->log('Changing working directory... ');
// Discard any changes to tracked files since our last deploy
exec('git reset --hard HEAD', $output);
$this->log('Reseting repository... '.implode(' ', $output));
// Update the local repository
exec('git pull '.$this->_remote.' '.$this->_branch, $output);
$this->log('Pulling in changes... '.implode(' ', $output));
// Secure the .git directory
exec('chmod -R og-rx .git');
$this->log('Securing .git directory... ');
if (is_callable($this->post_deploy))
{
call_user_func($this->post_deploy, $this->_data);
}
$this->log('Deployment successful.');
}
catch (Exception $e)
{
$this->log($e, 'ERROR');
}
}
}
// This is just an example
$deploy = new Deploy('/var/www/site-name/repo-name');
$deploy->execute();
?>
i was researching this more and the best way you can do is like i mention find out what is the user being used by apache server by building a php script and run the shell_exec('whoami'); and run on your browser to see that user it is.
Then on your document root for your website mine per example is /var/www you would need to create shh keys for this directory when you do that also add a config file with the host bitbucket and reference to the key you created.
Add the key to bitbucket
then you would need to add permission for apache to run the git command run the command visudo and add:
yourapacheuser ALL=(yourapacheuser) NOPASSWD: /usr/bin/ #the /usr/bin in my case is where i have installed git so you need to see what is the directory path where you install git.
After that you should be able to run your script without problems, in case it complains about a requiretty message then on visudo again add: Defaults:yourapacheuser !requiretty
hope it helps
did you configure SSH keys for the server you are using in bitbucket and add the webhook with the url of your script?
As user1361389 wrote you need to know what users are running the different processes. This is how I did on an Amazon Ubuntu instance.
I have a php file that calls a bash script:
shell_exec("sudo -u ubuntu /home/ubuntu/gitpull.sh");
Create SSH keys for user ubuntu and uploaded the public key to bitbucket.
Also, make sure that the php files on your server are owned by the correct user. In my case ubuntu.
You then need to impersonate ubuntu when calling the php file to deploy code. Add this line in the sudoer file (>sudo visudo )
www-data ALL=(ubuntu) NOPASSWD: /path/to/gitpull.sh
Then in bitbucket add the URL to your hook.

CRON command with Yii

I have a code that will automatic create a sitemap.xml . My Url look like this : http://example.com/sitemap
But now I want it to automatic run every minutes (just test) . I tried 2 way but none of them work :
first is call through URL ,ex:
*/1 * * * * wget http://example.com/sitemap
or
*/5 * * * * curl http://example.com/check
second is call throught php file , I'm using Yii so i'm not sure what the file .php is ? is this controller file or something else?
*/1 * * * * /usr/bin/php /home/domains/public_html/protected/modules/homepage/controller/HomepageController.php
my Sitemap action's code inside HomepageController.php
Use console command for running CRON calls. For example, create SitemapCommand.php (place it in protected/command/) with following code:
class SitemapCommand extends CConsoleCommand
{
public function actionGenerate($debug=0)
{
#generating of sitemap
}
}
Then add line like this, to your crontab file:
*/1 * * * * /usr/bin/php /home/domains/public_html/protected/yiic.php sitemap generate
You have to put something like this in your linux cron tab->
http://www.Yourwebstieaddress.com/index.php (use one spacebar) Controller_name (use one spacebar) function_name
it will definitely work

How to set cron job URL for CodeIgniter?

I am using CodeIgniter for my website. I have to use cron job to run one of controller function. I am using route in website. And also I am not using index.php in URL.
e.g. http://example.com/welcome/show, here welcome is my controller and show is function name of that controller.
I have used like this,
0 * * * * php /home/username/public_html/welcome/show
It is giving 'No such directory'
How can I set cron jon in cPanel for above URL.
Use:
php index.php welcome show
as command in your crontab. E.g.:
0 * * * * php /home/username/index.php welcome show
Source (ver. 2.2.0)
http://www.codeigniter.com/userguide2/general/cli.html
Source (ver. 3.*)
http://www.codeigniter.com/user_guide/general/cli.html
Source (ver. 4.*)
http://codeigniter.com/user_guide/cli/cli.html
I have used below cron
php /full-path-to-cron-file/cron.php /test/index
source: http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/
This works for me.
Thanks to all
You can try with this one:
wget api.example.com/index.php/controller/function
You can also try:
0 * * * * /usr/bin/curl --silent --compressed http://example.com/welcome/show
Or localhost
0 * * * * /usr/bin/curl --silent --compressed http://localhost/welcome/show
I hope that is helpful.
/usr/local/bin/php /home/username/public_html/index.php controllername methodname
This worked for me.
Here is the cron I use
/usr/bin/php /home/pia/www/jobs/index.php cron newsletter
Explanation:
a) $_SERVER['DOCUMENT_ROOT'] = /home/pia/www
b) codeigniter website root = /home/pia/www/jobs
c) 'cron' = controller name
d) 'newsletter' = method name
I have done it as
00 09-18 * * 1-5 /usr/bin/php /var/www/html/app/index.php crontest
crontest is the name of the controller which also uses a model to pull data from the database and send mail periodically (between 9 AM to 6 PM on Monday to Friday every week)
I just viewed this page which explains very detail with example. Hope this will be useful to others as well.
I am using codeigniter 3.0.3 and my server is hostgator. For me, the below format is working fine
*/15 * * * * /opt/php55/bin/php /home/username/public_html/myapp/index.php reminders index
above command runs every 15 minutes, reminders in command is controller name and index is method name.
watch -n60 curl [your application path]/check_banalce/user_balance
in my case im using codeigniter and the above command executes the user_balance function which is found in check_balance controller every 60 sec.
On a Linux EC2 intance, this worked:
*/5 * * * * /usr/bin/php /var/www/html/cifolder/index.php [module] [function]
If you are using the Hostgator(or any other Linux server) then try this one.
/opt/cpanel/ea-php72/root/usr/bin/php /YOUR_HOME_DIRECTORY/YOUR_USERNAME/public_html/marketing/index.php welcome emailcampaign 1
for example for me its
/opt/cpanel/ea-php72/root/usr/bin/php /home3/adnan/public_html/index.php welcome emailcampaign 101
where
welcome is the controller name
emailcampaign is the function name of welcome controller
101 = First argument of url.
Set up cron jobs through cPanel using this procedure:
Log on to your cPanel Interface.
Go to ''Advanced' section.
Click on "Cron Jobs".
Select the specific time from the lists provided.
You should enter the command to run in the "Command" field.
* * * * * php index.php controllername functionname
1st * - minute,
2nd * - hour,
3rd * - day of month,
4th * - month,
5th * - day of week.
For more info visit : https://crontab.guru/
I am using hostgator's cPanel.
I have created a user controller and run_cron_data function inside the user controller.
Command: wget www.example.com/index.php/user/run_cron_data
See the below screenshot
If you are using cPanel then Use the following command:
/usr/bin/curl -k http://example.com/welcome/show
This works perfectly for me.

Categories