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
32 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 23 by Omkar
• 67,480 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
128 views
0 votes
1 answer
+1 vote
0 answers
0 votes
1 answer
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
• 10,670 points
2,729 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
• 10,670 points
287 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
13,514 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,240 points
989 views
0 votes
1 answer