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, 2019 in Others by Bhavish
• 370 points
2,452 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, 2019 by Omkar
• 69,030 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,120 points
350 views
0 votes
1 answer
+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
68 views
0 votes
0 answers

How to run commands within fabric pods through a batch script?

I am trying to write a batch ...READ MORE

May 30 in Others by anonymous
• 120 points
96 views
+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
• 10,950 points
6,124 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,950 points
934 views
+1 vote
11 answers

hadoop fs -put command?

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

answered Dec 7, 2018 in Big Data Hadoop by Aditya
39,649 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,320 points
2,301 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,030 points
120 views