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 24, 2019 in Others by Bhavish
• 370 points
8,615 views

1 answer to this question.

+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 24, 2019 by Omkar
• 69,210 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
0 votes
1 answer

Newbie needs Excel suggestions with a simple script

Formula to Count the Number of Occurrences ...READ MORE

answered Feb 7, 2023 in Others by narikkadan
• 63,420 points
326 views
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,130 points
1,273 views
0 votes
1 answer
+1 vote
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
• 11,380 points
10,558 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
• 11,380 points
2,185 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
104,209 views
–1 vote
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,390 points
4,260 views
0 votes
1 answer

Where is the documentation to refer for coinbase api integration of Etherium coin currency in php?

Hey there! Please refer to the following ...READ MORE

answered Jan 25, 2019 in Others by Omkar
• 69,210 points
512 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP