Laravel task scheduler runs only when doing the command "php artisan schedule:run" - scheduled-tasks

I'm trying to create a Scheduler in Laravel 5.8 on my localhost, but it only runs when I use the following command.
php /opt/lampp/htdocs/my_project/artisan schedule:run >> /dev/null 2>&1
php artisan schedule:run
and not runs every minute as i defined
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$good = new Grocery();
$good->name = 'good';
$good->type = 'type';
$good->price = '222';
$good->save();
})->everyMinute();
}
Data is stored in the database, but I should run the command every time.
Nothing is unique in the database.

Your schedule:run needs to be added to your cronjob.
This can be done by typing crontab -e. Once done you should enter the command to be executed every minute:
* * * * * php /opt/lampp/htdocs/my_project/artisan schedule:run >> /dev/null 2>&1
This will set the command to run every minute. Your application is then responsible for seeing if your scheduled command should run.

Related

How to optimise Percona MongoDB LDAP for more concurrent connections?

Percona MongoDB with LDAP is not working for more than 2 concurrent threads even if the connection pool configured for more than 2.
MongoDB Configuration,
setParameter:
saslauthdPath: /app/mongo/mongoldap/var/run/saslauthd/mux
authenticationMechanisms: PLAIN,SCRAM-SHA-1
ldapConnectionPoolSizePerHost: 10
ldapUseConnectionPool: true
ldapDebug: true
SASL Configuration,
ldap_servers: ldap://ldap.forumsys.com
ldap_mech: PLAIN
ldap_search_base: dc=example,dc=com
ldap_filter: (cn=%u)
ldap_bind_dn: cn=read-only-admin,dc=example,dc=com
ldap_password: password
Test Script (PHP),
<?php
use MongoDB\Driver\Manager as MongoDB;
use MongoDB\Driver\Query as Query;
use MongoDB\Driver\BulkWrite as BulkWrite;
try{
for($i=0;$i<3;$i++){
$handlerName = "handle".$i;
$$handlerName = new MongoDB("mongodb://xx.xx.xx.xx",array("authSource"=>"$external","authMechanism"=>"PLAIN","username"=>"cn=read-only-admin,dc=example,dc=com","password"=>"password","tls"=>true,"tlsCertificateKeyFile"=>"/xyzabc/dbs/mongoclient.pem","tlsCAFile"=>"/xyzabc/dbs/mongoca.pem","tlsAllowInvalidCertificates"=>true));
$filters=array();
$options=array();
$command = new Query($filters,$options);
$query="xyzabc.customerdetails";
$result = $$handlerName->executeQuery($query,$command);
$resultAsJson = $result->toArray();
$resultAsArray = json_decode(json_encode($resultAsJson), True);
print_r(count($resultAsArray));
echo "\n";
sleep(5);
}
for($i=0;$i<3;$i++){
$handlerName = "handle".$i;
$query="xyzabc.client";
$result = $$handlerName->executeQuery($query,$command);
$resultAsJson = $result->toArray();
$resultAsArray = json_decode(json_encode($resultAsJson), True);
print_r(count($resultAsArray));
echo "\n";
}
echo "Success";
}catch(Exception $e){
print_r($e);
echo "Failed";
}
?>
Test Script (Shell script for nohup),
nohup php test.php > output1.log 2>&1 &
nohup php test.php > output2.log 2>&1 &
nohup php test.php > output3.log 2>&1 &
nohup php test.php > output4.log 2>&1 &
nohup php test.php > output5.log 2>&1 &
Test Results,
Script executed in a single thread (same process ID) - there is no error it works for any number of connections
If the same script is executed on nohup (multi-thread or multiple process IDs) - only works for the first two threads, fails for 3rd and above
Error Message (MongoDB log),
LDAPLibraryError: Failed to authenticate 'cn=read-only-admin,dc=example,dc=com' using simple bind; LDAP error: Can't contact LDAP server
Percona MongoDB Version: 4.4.2-4
When the test PHP script is executed synchronously there is no error with the number of connections. I assume this is because the process ID is the same for all the DB connections so it uses the same connection pool.
On the other hand, if it is executed concurrently (with nohup), only the first 2 connections are working, by this, I assume only the first 2 connection pools are working and from 3rd connection pool, the requests are failing.
Since I have ldapConnectionPoolSizePerHost is set to 10, I don't understand why this is not working as expected.
Thanks in advance!

How can i add a cron job run every minute to my elearning site on moodle?

I try to add a cron job on an elearning.mysite.gr(Moodle). Although my host gives this message every hour.
Oct 2 1:10:01 linux CROND[123456]: (admin) CMD (touch /tmp/test.txt > /dev/null)
On my site administration -> notifications get this message:
The admin/cli/cron.php script has not been run for 3 days 2 hours and should run every 1 min.
At moodle documentation
/path/to/moodle/admin/cli/cron.php, can i use it ? in which way ?
I tried this :
/usr/bin/php /path/to/moodle/admin/cli/cron.php
but gives me that the process completed with error at one minute
only this touch /tmp/test.txt > /dev/null
completed with success.
Moodle Documentation:
The CLI (command line interpreter) script. This will be at the path /path/to/moodle/admin/cli/cron.php
If in doubt, this is the correct script to use. This needs to be run by a 'PHP CLI' program on your computer. So the final command may look something like /usr/bin/php /path/to/moodle/admin/cli/cron.php You can (and should) try this on your command line to see if it works. WARNING: Check your command-line PHP version is compatible with your chosen version of Moodle.-How to check it?
The command-line PHP program is different to the one running your web site and is not always the same version.
I don't know what to do...I will appreciate any help!!!
I tried by my host panel interface :
Type of progress:
Command line
URL
PHP
i should choose one from 3.
Command*:
placeholder to add my command to be excecuted
Excecute:
Dropdown
1.Cron style
2.Daily at 00:00
I use command line.
touch /tmp/test.txt
When i add
cron style and 1 * * * * * , get message for syntax.
and for
daily at 00:00 success message but on my notification of my site has
The admin/cli/cron.php script has not been run for 17 days 22 hours and should run every 1 min.
That's the icon of my Plesk Login. To change the path/to/moodle/admin/cli/cron.php xould i check the file manager in order to find cron.php file ??
As you add the cron from a web interface (maybe Plesk ?) you only need this part:
/path/to/moodle/admin/cli/cron.php
replace /path/to/moodle/ with the real path on your server, usulay something like /var/www/moodle/ or /var/www/vhosts/domain.name/httpdocs/moodle
To run every 1 minute cron style is :
* * * * *
If you can choose PHP version, choose the same version as the one you are using for Moodle.
Finally, I give this touch /tmp/test.txt ->and at cron style the touch /tmp/test.txt, as you suggest to me.
Because the other commands cannot complete with success.
But in my notifications,(Site administration-Moodle) i still get the message that cron don't run.

Node - Run last bash command

What is happening:
user starts local react server via any variation of npm [run] start[:mod]
my prestart script runs and kills the local webserver if found
Once pkill node is run, that kills the npm start script as well so I want to run the starting command again.
My current solution is to do
history 1 | awk '/some-regex/
to get the name of the last command which I can run with
exec('bash -c 'sleep 1 ; pkill node && ${previousCommand}' &')
This is starting to get pretty hacky so I'm thinking there has to be a better way to do this.
My node script so far:
const execSync = require("child_process").execSync;
const exec = require("child_process").exec;
const netcat = execSync('netcat -z 127.0.0.1 3000; echo $?') == 1 ? true : false; // true when :3000 is available #jkr
if(netcat == false) {
exec(`bash -c 'sleep 1 ; pkill node' &`);
console.warn('\x1b[32m%s\x1b[0m', `\nKilling all local webservers, please run 'npm start' again.\n`);
}
There seems to be an npm package which does this: kill-port
const kill = require('kill-port')
kill(port, 'tcp').then(console.log).catch(console.log)
Source: https://www.npmjs.com/package/kill-port
I understand this might not answer the question of running last command but should solve OP's problem.

WP-CLI: STDOUT error when called in CRON (crontab)

Little help or guidance. Server is CentOS 7 - with WHM/CPanel installed.
Command:
$(which php) $(which wp) core update --require=/opt/wp-cli-pre.php --path=/home/USER/public_html/
The contents of /opt/wp-cli-pre.php
<?php
if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'r'));
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'w'));
if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'w'));
Works as expected from the command line, but if from cron job, I get:
PHP Warning: Use of undefined constant STDOUT - assumed 'STDOUT' (this will throw an Error in a future version of PHP) in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/utils.php on line 1057
output of "which php"
/usr/local/bin/php
output of "which wp"
/usr/local/bin/wp
I have installed the latest WP-CLI from https://wp-cli.org/
At long last, I have found the solution.
When the CRON runs a PHP script like this: */5 * * * * php /path/to/script.php
The SAPI name is: cgi-fcgi (on a WHM/CPanel install on CentOS)
It cannot find the CLI version setup by CPanel at /usr/local/bin/php as the $PATH var is just: /usr/bin:/bin
So, the solution is to not depend on the system environment to determine the PHP you want. But to set that directly.
Like this: */5 * * * * /usr/local/bin/php /path/to/script.php
It was always my assumption that if a CRON was setup in ROOT's crontab, it would inherit ROOT's environment. This apparently is not the case.
You should take a look at the following post.
https://forums.cpanel.net/threads/users-cannot-use-wp-cli-in-cron.643293/

run rake ts:index from within a rake task

I am running Rails 2.3.5.
In my project I have in lib/tasks the following rake task (test_task.rake):
desc 'test_synchro_task'
task :test_synchro_task => :environment do
# A bunch of things that are properly executed (basically I am inserting
# in the database)...
# ...
# and once the above is done, I want the following to be executed,
# the sphinx index to be rebuild, but it is NOT :
system("cd /sites/project/app")
system("RAILS_ENV='staging' rake ts:index")
end
I trigger the execution of the task via a crontab containing the following entry:
13 14 * * * cd /sites/project/app && /sites/ruby/bin/rake RAILS_ENV=staging test_task
which id correctly called and executed except for 2 system lines in the task.
Please note that when I place those 2 system lines in a ruby test.rb file in my project script directory, and run it manually using the ruby command:
ruby test.rb
those 2 system commands are properly executed and the index is rebuilt correctly.
In my rake task I tried replacing those 2 system lines by:
%x["cd /sites/project/app"]
%x["RAILS_ENV='staging' rake ts:index"]
or by
#cmd="cd /sites/project/app; RAILS_ENV='staging' rake ts:index"
`#{#cmd}`
but the rake ts:index is still not executed.
Any idea why?
Many thanks.
Yves
Problem resolved:
1- In the ruby script, I found that the $?.exitstatus was 127, which is "command not found".
2- This hinted me to a PATH problem occurring in the context of cron.
3- Found that post: http://dewful.com/?p=157 titled "Ruby - Cron Not Working For Ruby Script".
4- Added the PATH in the crontab and everything works fine.
Yves