Laravel Crontab + Shell Exec - php

I have a Laravel App with Short Schedule package installed and a crontab to execute it.
Inside the executed method I have a SHELL_EXEC function with some code inside it.
The problem is that when the code runs automatically using the cron, the SHELL EXEC doesn't work. The output is null.
When I run the method directly using php artisan or by simply running the schedule using php artisan manually, it works.
To have a clear vision of what's going on:
Inside the crontab -e I have the following
* * * * * php /var/www/html/project/artisan short-schedule:run --lifetime=60
The cron executes this method which has the following code:
$shell_command = '/home/paul/elrondsdk/erdpy --verbose tx new --receiver xxxx --send --pem asdfa.pem --gas-limit 300 --nonce 12';
$output = shell_exec($shell_command);
Log::info('output', [$output]);
If it runs automatically using the cron, the $output is NULL.
If I run the command manually, I get a proper output.
Initially I thought I need to specify the exact path in the shell exec because cron doesn't not PATH. Unfortunately did not solve my problem.
Thanks!
I tried to run the command manually or to run php artisan schedule:run and wait and it worked!
It doesn't work ONLY when its runs by the cron.

Related

Run a Cron Job In Cpanel with CURL

I have a code in laravel, I want to setup a cron job in cpanel But I don't know how to. I saw several examples that has file name to cron, but I am using Laravel so how can I point this to URL route?
/usr/bin/curl https://example.com/cron_route
I am not sure is this correct method or I can do that in different way, well I canot use laravel artisan scheduler, I have all custom code and custom Controller.
can anyone help me out with this?
Try this command
1 1 * * 0 /usr/bin/curl -m 120 -s https://example.com/cron_route &>/dev/null
To add cronjob using command line, use this command
0 2 * * * wget -q -O /dev/null http://example.com/cron_script
See for reference : https://crontab.guru/

Why Shell_exec() and exec() not working in php? $return_val is 0

file_put_contents('/opt/lampp/htdocs/imslivedec/crontab.txt','0 11 15 1 * /opt/lampp/htdocs/imslivedec/sendmail.php'.PHP_EOL,FILE_APPEND);
shell_exec('crontab /opt/lampp/htdocs/imslivedec/crontab.txt');
When I run this exact same command in the terminal, the new cron job is getting
created but when I do from the php script using shell_exec() the command is not executing in the terminal.May I know why, am I doing anything wrong here?The shell_ exec works fine when I tried to create a new folder.

Task helpers not running Laravel, but cron is running every minute

I have a task I'm trying to run every day. In my Kernel.php I have the following command:
$schedule->command('emailProgram')->daily()->timezone('US/Central');
I'm my crontab I have:
* * * * * php /var/www/html/appname/artisan schedule:run >> /dev/null 2>&1
So, When I run php artisan schedule:run, or run it directly with php artisan emailProgram it runs as expected. But, its not running on its own using daily()/dailyAt() or otherwise. Lastly, if I remove daily() from the command in the kernel.php file:
$schedule->command('emailProgram')->timezone('US/Central'); it's running every minute, so its like there is some disconnect with the Laravel task helpers. This is my first time setting up Cron, and task management with Laravel so maybe I'm overlooking something simple. Any help would be really appreciated, thanks.

Cannot execute external PHP with Laravel artisan scheduler

I have an external PHP script, that is processing an XML array to insert, update or delete rows in a database. This script lies in the root of the project in a folder called scripts and I can run and execute it via terminal with no problems whatsoever and it updates the database accordingly:
php index.php
I have also set up a schedule in Laravel (using October CMS syntax)
public function registerSchedule($schedule)
{
$schedule->exec(public_path() . '/script/index.php')->everyMinute();
}
This however is doing nothing. I tried manually running the schedule with artisan in command line by:
php artisan schedule:run
And the output is
Running scheduled command: /Users/x/x/x/x/scripts/index.php > '/dev/null' 2>&1 &
Nothing happens in the database tho.
Did you try to generate a new key?
php artisan generate:key

How to plan job in php script via exec and 'at'

I try to plan one-time job with 'at' command. There is next code in script:
$cmd = 'echo "/usr/bin/php '.$script_dir.$script_name.' '.$args.'"|/usr/bin/at "'.$time.'" 2>&1';
exec($cmd, $output , $exit_code);
When I run this command from script it adds the job to the schelude. This I see by the line in logs job 103 at Thu Sep 3 15:08:00 2015 (same text contains $output). But then nothing happens in specified time like at ignores the job. And there are no error messages in logs.
When I run same command with same args from command line on server it scheludes the job and than runs it at specified time.
I found out that when I try to plan a job via php script it runs under apache user. I tried to run next in command line on server:
sudo -u apache echo "/usr/bin/php /var/www/pant/data/www/pant.com/scripts/Run.php firstarg secondarg "|/usr/bin/at "16:00 03.09.2015"
It works correct too. I checked sudoers and have added apache user with NOPASSWD privileges. Script Run.php has execute rights.
at.deny is empty. at.allow does not exist.
So question is: why 'at' does not run command given via php script (exec) but runs same command in command line? How to run it?
Thanks to all.
I found by chance answer at stackexchange.com:
The "problem" is typically PHP is intended to run as module in a webserver. You may need to install the commandline version of php before you can run php scripts from the commandline

Categories