Execute a shell script with php

0 votes

I need to execute a shell script through php. I am using Apache web server to run my php pages.

Here is my php file:

<?php

$output = shell_exec('sh /usr/local/hadoop-3.0.2/copytoallhdfs.sh');

echo "<pre>" . $output . "</pre>";

?>

Here is my shell script  copytoallhdfs.sh:

#!/bin/bash

myarray=`bin/hdfs dfs -ls -C /`

echo $myarray;
for name in $myarray
do bin/hdfs dfs -copyFromLocal myData/* $name;
done

My  copytoallhdfs.sh script is located on the path /usr/local/hadoop-3.0.2​ .  So normally php should be executing my shell script and displaying the output of echo $myarray; on the web server, but nothing is displaying on my browser.

Maybe it is some permissions problems or something missing in my php.ini but i just can't solve this problem. 
Can anyone please give me a work around for this. 

Thank you.

Feb 23 in Others by Bhavish
• 370 points
26 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote

One of the possible reason for this is exec is disabled in the php.ini. In the php.ini file search for ​

disable_functions

And if it has exec, then remove exec from it and try again. See if it works

answered Feb 23 by Omkar
• 66,910 points

Hello @omkar , thank you for the suggestion. Actually the problem was something very simple. 
I edited the copytoallhdfs.sh script and added pwd to verify the location that php is giving me.
Then i realized that it was giving me the location of my apache directory even though the file was located in my hadoop directory.

So i updated the 
copytoallhdfs.sh script and inserted the full path to execute the command bin/hdfs and it worked. 

Here is my updated script:

#!/bin/bash

myarray=`/usr/local/hadoop-3.0.2/bin/hdfs dfs -ls -C /`
echo $myarray;

for name in $myarray
do /usr/local/hadoop-3.0.2/bin/hdfs dfs -copyFromLocal /usr/local/hadoop-3.0.2/myData/* $name;
done



That was kind of a silly mistake, but i guess it can happen!
Thanks for your help.

You're welcome :)

Related Questions In Others

0 votes
1 answer

Parsing a string with GetOpt::Long::GetOptions

Hey, have a look at the section ...READ MORE

answered Nov 15, 2018 in Others by nirvana
• 3,060 points
77 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,100 points
171 views
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 9,490 points
1,845 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 9,490 points
160 views
0 votes
10 answers

hadoop fs -put command?

copy command can be used to copy files ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Sujay
9,158 views
0 votes
1 answer

Hadoop dfs -ls command?

In your case there is no difference ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by kurt_cobain
• 9,260 points
657 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.