Home > Back-end >  error in Run PHP code with command line linux
error in Run PHP code with command line linux

Time:02-02

My system is : Ubuntu 20.04 Php 7.4 Nginx

I have a php file with following content in /var/www/hamrah.com/create.php path :

<?php
$output = shell_exec('bash flutter create test');
echo "<pre>$output</pre>";

i run this command in terminal and And this command executes correctly (creates a flutter project for me)

 cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php

BUT !

But when I put this command in Crontab, the command is not executed properly and gives an error

In Crontab :

* * * * * cd /var/www/hamrah.com && php /var/www/hamrah.com/create.php

Note: If I put shell_exec(' bash ls') instead of shell_exec('bash flutter creat test') in the php file, it will run well and some commands like the flutter create command will not be executed.

error : bash: flutter: No such file or directory

CodePudding user response:

You can test

<?php
$output = shell_exec('bash /opt/flutter/bin/flutter create test');
echo "<pre>$output</pre>";

CodePudding user response:

Cron doesn't read user's environment variables, the default value of PATH is /usr/bin:/bin, usually your flutter SDK won't be located in these 2 folders, so you need explictly specify the location of the flutter SDK. Add this line to the crontab.

PATH=/usr/bin:/bin:/path-to-flutter-sdk-bin

Use the following command if you don't know where flutter is.

which flutter
  • Related