Running the exec() function inside a cron job can't find command - php

I'm using cron to run a php script at regular intervals:
*/5 * * * * php /path/to/my/script.php
Inside the php script I have an exec command that runs node:
$command_str = 'node my_node_script.js';
exec($command_str, $output_arr, $error_res);
Everything works great on the command line, but not via cron. So I logged the output and I get the following error:
sh: node: command not found
Why is this happening, and how can I fix it?

I figured this out thanks to a comment by John C.
What I did was use which node from the command line to find out where node is installed (in my case it was usr/local/bin/node), and then update the exec() call to use this absolute path:
$command_str = '/usr/local/bin/node my_node_script.js';
exec($command_str, $output_arr, $error_res);
I hope it helps if you run into this challenge.

To avoid conflicts with other packages, Debian dev team has decided to use /usr/bin/nodejs as the path for node.js. If you're using Ubuntu or Debian, you should install node-legacy package to call nodejs using node command:
sudo apt-get install nodejs-legacy
For modern nodejs versions, you should call nodejs command instead or the full path of your nodejs installation:
/usr/bin/nodejs my_script.js
or
/home/user/.nvm/vx.xx.xx/bin/node my_script.js

Related

How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good,
but I can't run exec('gitbook xxxx').
gitbook is a npm package I installed by
npm install gitbook -g
I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code?
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && gitbook build);
after I added export PATH, everything works fine.
I tried once like this on UNIX-based OS:
You can run shell commands via the exec() function:
// make an php file to execute shell script
exec("node yourscript.js &", $output);
Well output here become array of each line of output along with process id. You can kill process by processid also.
exec("kill " . $processid);
This how I was did. Other then this you can use node supervisor. Hope it will help you. Try your also with node command.

How to execute Windows10's Bash command from PHP?

Windows 10 just released Anniversary Update today. Now you can use Ubuntu flavored bash command from Linux subsystem.
The question is: How to execute Windows10's Bash command from PHP?
I tried
<?php
exec('bash',$out1,$result1);
exec('ls -l',$out2,$result2);
var_dump($out1);
var_dump($result1);
var_dump($out2);
var_dump($result2);
It doesn't work. All $out are empty array, and both $results are 1.
Any idea?
Just found out that I can run web server directly from subsystem.
e.g.
$ sudo apt-get install apache2
$ sudo service apache2 start
Then put all web contents inside subsystem's directory located at %localappdata%\Lxss\rootfs
At this point I can execute bash script however I want.
You can not. Ubuntu on Windows is implemented as a different subsystem directly below the Windows Kernel. So it runs separate to normal Windows processes and can not interact with them. (At least that is how I understand it). But maybe you just have to use C:\Windows\System32\bash.exe as the command.
Bash is not meant to be used to run a server but you can run PHP-CLI on bash without an issue.
Instead of running the PHP script in Windows and then accessing bash it would be easier to create a command to run the PHP script directly in bash.
So you would run `bash.exe -c "php path/to/php-script.php"
https://blogs.msdn.microsoft.com/commandline/2016/10/19/interop-between-windows-and-bash/

Webmin cron job

I have been tried for few hours and nearly 1 day to try with this but I failed to make it
I want to run the cron file every 10 minutes and I searched for so many tutorials but I don't know why it is not working. Anyone here who experienced in Webmin scheduled cron job can give me any suggestions?
Have you got PHP-CLI installed on your webmin server? If not, you can install it using:
sudo apt-get install php7.0-cli
sudo apt-get install php5-cli
Either of the commands will work, however it depends on the PHP version you are currently using.
Once installed it will allow you to run PHP command through the command line. Your CRON Job should look simlar to this:
php /var/www/example.com/public_html/crons.php
I haven't had time to test the code, however it should work.
Another solution worked for me was adding complete URL of the file preceding by the 'GET' keyword instead of running it as a php script.
Example:
GET 'https://www.example.com/crons.php'

Run GruntJs task from php exec function

I would like to perform some grunt task by running command from php exec function.
I've already:
Install grunt globally: npm -g install grunt-cli
Setup apache and windows to allow running CLI from php (I use
wampserver)
I ran this script from php:
<?php
exec('grunt --gruntfile Gruntfile.js');`
And I found in apache log that grunt command is not found whereas I can run this command from CLI and grunt is in my env path.
So I've decided to create a batch file that contains that script and launch the batch instead of the grunt command directly.
<?php
exec ('gruntTask.bat'); // I know that it can execute script because I can
// create folder if I put mkdir command in gruntTask.bat`
gruntTask.bat contains:
grunt --gruntfile Gruntfile.js
But why it couldn't execute this script? Have I missed something?
Need helps guys,
Regards,

shell_exec command cannot find libraries

I am trying to execute a shell command via shell_exec (text to speech). The command works well from the shell and the paths are set correctly, but when executed from PHP it doesn't find certain libraries. This is the command
shell_exec('echo "nice voice" | text2wave -o /path/output.wav -eval "(voice_selected_voice)" 2>&1 ');
and this is the output that I get:
/usr/bin/festival: /opt/bitnami/common/lib/libstdc++.so.6: version 'GLIBCXX_3.4.11' not found (required by /usr/bin/festival)
/usr/bin/festival: /opt/bitnami/common/lib/libstdc++.so.6: version 'GLIBCXX_3.4.9' not found (required by /usr/bin/festival)
/usr/bin/festival: /opt/bitnami/common/lib/libstdc++.so.6: version 'GLIBCXX_3.4.11' not found (required by /usr/lib/libestools.so.2.1)
... and so on ...
It looks like it cannot find those libraries, but they are exactly there
Thanks
When you run these commands via shell_exec command, you are running them with the apache user permissions. You need to sudo it via root (modify the sudoers file a bit).

Categories