LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-25-2019, 11:46 AM   #1
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Rep: Reputation: Disabled
Shell script fails when executed via php


Hi Everyone,

I have written a script using shell and uemcli(Unity/EMC), which executes fine from backend. But, when I execute the same script using php i.e frontend, it fails.

Below is the code:

shell script code-

uemcli -d 11.11.22.33 -u test -p test@123 /env/sp show
uemcli -d 11.11.22.33 -u test -p test@123 /env/disk show

These are the commands that works fine in black screen. But it doesn't execute when called using php. There is no error being shown in apache error logs or in /var/log/messages.

Below is the code of php file

{ shell_exec("/usr/bin/sh /test/test.sh"); }
 
Old 03-25-2019, 11:48 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by yash1990 View Post
{ shell_exec("/usr/bin/sh /test/test.sh"); }
Two points, shouldn't that be /bin/sh instead? Check the path.

Also, even that should not be needed if /test/test.sh is both executable (+x bit set) and has #!/bin/sh as its first line.
 
1 members found this post helpful.
Old 03-25-2019, 11:54 AM   #3
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Original Poster
Rep: Reputation: Disabled
Hi Turbo,

These have been applied to the file. It is just that I didn't post it.

shouldn't that be /bin/sh instead?
Actually, I have other codes, where I'm already using /usr/bin/sh successfully

Thank You

Last edited by yash1990; 03-25-2019 at 12:08 PM.
 
Old 03-25-2019, 12:11 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,710

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by yash1990 View Post
Hi Everyone,
I have written a script using shell and uemcli(Unity/EMC), which executes fine from backend. But, when I execute the same script using php i.e frontend, it fails. Below is the code:
Code:
uemcli -d 11.11.22.33 -u test -p test@123 /env/sp show
uemcli -d 11.11.22.33 -u test -p test@123 /env/disk show
These are the commands that works fine in black screen. But it doesn't execute when called using php. There is no error being shown in apache error logs or in /var/log/messages. Below is the code of php file
Code:
{ shell_exec("/usr/bin/sh /test/test.sh"); }
Please use CODE tags.

Aside from the INCREDIBLY BAD security for such a thing, you should be able to just put "#!/bin/sh" as the first line of your script, as said by Turbocapitalist. However, why /bin/sh, as opposed to the MUCH more common /bin/bash? And you are just putting 'uemcli' in...you probably need to use a fully-qualified path, such as /usr/bin/uemcli (or wherever that program lives; run 'which uemcli' to find out). Is that script executable??? What are the permissions, and who owns it? Since you're running a shell script through PHP, your web server needs permissions to run that file and/or the subsequent commands IN that script. So if you need sudo rights to run uemcli...it'll die right there.

And you won't get PHP errors, since your PHP code is working just fine...it's the shell script that's not. Run it from the command line to see what you get, with something like "php /path/to/file.php".
 
2 members found this post helpful.
Old 03-25-2019, 12:28 PM   #5
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Original Poster
Rep: Reputation: Disabled
Hi TB0ne,

As per your suggestion, I have added the full path for uemcli i.e. /usr/bin/uemcli (got this o/p by running cmd whereis uemcli). Changed the first line to #!/bin/bash from #!/bin/sh

Permission of apache service is wwwrun, so this ownership is already provided to the php and shell script files. 755 permission is set for shell script.

Still the code doesn't work. No error logs. Shell script runs fine from command line.

FYI, php page has a refresh button. Once that is hit, shell script is been invoked. Here it gets failed.


Thank You
 
Old 03-25-2019, 01:26 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,710

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by yash1990 View Post
Hi TB0ne,
As per your suggestion, I have added the full path for uemcli i.e. /usr/bin/uemcli (got this o/p by running cmd whereis uemcli). Changed the first line to #!/bin/bash from #!/bin/sh

Permission of apache service is wwwrun, so this ownership is already provided to the php and shell script files. 755 permission is set for shell script. Still the code doesn't work. No error logs. Shell script runs fine from command line. FYI, php page has a refresh button. Once that is hit, shell script is been invoked. Here it gets failed.
Again, does that command need sudo rights to run from the command line?? And can you run that PHP script (*NOT* the shell script), from the command line by invoking it with "php /some/path/to/script.php"??? There are many guides on how to debug a PHP script: https://www.php.net/manual/en/debugger.php

Without any error messages to go by, there is absolutely nothing we can tell you. And have you verified that the uemcli command syntax is correct? Because it's different in the EMC documentation. And if the ENTIRETY of your PHP 'program' is a one-line call to a shell script...what do you think is going to happen?? It's spawning that system call, and returning. You're not capturing any data/variables, or putting them to the screen. It's doing exactly what you told it.
 
1 members found this post helpful.
Old 06-13-2019, 08:30 AM   #7
olidev
LQ Newbie
 
Registered: Jul 2018
Location: Illinois, US
Posts: 11

Rep: Reputation: Disabled
To debug PHP code, you need to enable the error logging. Have you done that? Add this to the top of your PHP script

Quote:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
add this to php.ini file:
Quote:
display_errors = on
You can also use Xdebug for debugging in PHP. Since you are using Linux, you can install it using this command;

Quote:
sudo apt install php-xdebug;
Now add this to xdebug.ini file

Quote:
xdebug.profiler_enable_trigger = 1
xdebug.profiler_enable = 0
xdebug.remote_enable = 1
xdebug.profiler_output_dir = "/tmp"
Source: https://www.cloudways.com/blog/php-debug-with-xdebug/
https://www.php.net/manual/en/debugger.php
 
  


Reply

Tags
emc, html, php, shell scripting, unity



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
After upgrading php 5.3.8 to php 5.4.30 php files are not being executed kingkashif Linux - Server 5 06-26-2014 10:15 AM
ShellScript executed from command prmpt but not executed from crontab or at command BMMadhav Linux - Newbie 1 11-16-2012 07:20 PM
[SOLVED] Configured Cron job executed every hour is instead executed every minute for 10m markings Linux - Software 4 05-13-2012 05:43 PM
[SOLVED] Xwindow's program will not run when executed on boot or when executed remotely richman1234 Programming 2 10-08-2010 01:32 PM
shell script acting different via shell/php and http jfeniello Linux - General 4 03-03-2010 10:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration