Executing Shell Script from PHP Webpage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing Shell Script from PHP Webpage
# 1  
Old 10-19-2015
Executing Shell Script from PHP Webpage

Hello ,

I am trying to execute a small shell script from a PHP Webpage.

Below is the shell script : 1.sh

$ cat /home/oracle/rahul/1.sh

Code:
#!/bin/ksh
echo "`date` : Report generation in progress" > /home/oracle/rahul/report.txt
echo "Run successfully script";

Below is the PHP Script : index1.php and I copied this file in /var/www/html directory

Code:
<?php
        $old_path = getcwd();
        chdir('/home/oracle/rahul/');
        $output = shell_exec("sh 1.sh");
        echo "<pre>$output</pre>";
        chdir($old_path);
?>

When I am trying to run the PHP Script from the command line using : php index1.php then it is executing fine and generating the Output file : /home/oracle/rahul/report.txt however when I am trying to run the url i.e : IPADDRESS/index1.php then it is having problem and I an not able to see anything on my browser.

Last edited by rahul2662; 10-19-2015 at 07:44 AM..
# 2  
Old 10-19-2015
When run by PHP, it will run as the user and group of the web server, probably apache, which no doubt doesn't have permission to write there and might not even have permissions to execute the script or chdir to that folder.

What are the permissions on the folder and script?

Also, instead of getting the output and pasting it into the script, you could do it more simply as system("./1.sh"); and the output of the script should simply appear in the document directly. system("./1.sh 2>&1"); if you wish to see shell errors too.

Note that the output of your script appears to be something like Run successfully script and absolutely no more.

Last edited by Corona688; 10-19-2015 at 04:03 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running php index.php as shell in webpage

so i have a bit of a unique situation. i have an encrypted index.php file that that can't be run the normal way that a web browser would run it. if it is run the normal way, the php script will show only gibberish on the web browser, instead of the actual php code. when run from the command... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

Launch shell script with parameters from a webpage button

I want to create a simple html page that should contain 2 fields in which the user can write the input. Then I want to have a button that should launch a shell script with the parameters inserted by user in the fields from the webpage. This is the code behind my webpage: <html> <form... (2 Replies)
Discussion started by: black_fender
2 Replies

3. Shell Programming and Scripting

Executing from shell , not from php

Hi, When I run command from php as www-data: cordova platform add android .. I get error without any solution in google. But when I run it from shell (also as www-data) - it works! So.. how to do to run this command from php but in shell (tty) not from php? Regards! (5 Replies)
Discussion started by: wrkilu
5 Replies

4. Shell Programming and Scripting

Why we use -f while executing any shell script?

Hi All, I wanted to know why we use the '-f' option while executing script. in my case... abcd.sh -f any_evts 02 2014 abcd = Scriptname -f = dont know any_evts = Some file or string 02= month 2014 = year So in above pleas ehelp to understand here -f and other arguement like... (1 Reply)
Discussion started by: ajju
1 Replies

5. Shell Programming and Scripting

Executing a shell script

LD_LIBRARY_PATH=~/tme-0.8/bus/multibus:~/tme-0.8/bus/sbus:~/tme-0.8/dist/softfloat/softfloat/bits32:~/tme-0.8/dist/softfloat/softfloat/bits64:~/tme-0.8/generic:~/tme-0.8/host/bsd:~/tme-0.8/host/gtk:~/tme-0.8/host/posix:~/tme-0.8/ic:~/tme-0.8/ic/ieee754:~/tme-0.8/ic/m68k:~/tme-0.8/ic/sparc:~/tme-0.8/... (1 Reply)
Discussion started by: lucky7456969
1 Replies

6. Shell Programming and Scripting

Read webpage from shell script

Hey experts, I am trying to read a webpage and want to search for a text patter (case insensitive). Please help me acquire this. If the text is found i will receive a mail which can be hardcoded in the script. I am not a big fan of using PERL script. Please help. Machine: AIX... (15 Replies)
Discussion started by: bankimmehta
15 Replies

7. Web Development

php script + not displaying webpage

Hi guys Im in the process of designing a website for my company which does the following 1) Daily execution a perl script. The perl script's output is diverted to a .html file. 2) The .html file generated from 1) sets up a centralised webpage showing options as radio buttons (the radio... (1 Reply)
Discussion started by: JamesGoh
1 Replies

8. Shell Programming and Scripting

Calling an Expect script from a Webpage using PHP

I have a webpage that is in HTML and PHP. In PHP I have tried using exec, system, shell_exec and passthru functions to call an Expect Script file (temp.exp). This Expect file spawns a telnet session that uses "expect/send" commands to retrieve information from an environmental unit (not a normal... (0 Replies)
Discussion started by: CCUSmith
0 Replies

9. UNIX for Dummies Questions & Answers

Executing a Shell Script

I am trying to run a shell script using the ./<ScriptName> command, but the server returns an error bash: ./Script1.sh: Permission denied What variable do I need to set to avoid this? (4 Replies)
Discussion started by: igandu
4 Replies

10. UNIX for Advanced & Expert Users

Shell script is not executing

Hi, I am trying to execute the below shell script: script name(ss1). ss1 was given permission - 744 before executing. name: ss1 #ss1 #usage:ss1 ls who pwd :wq I tried to execute $ss1 (Enter) Its not executing.... It says that ss1 is not found: echo $SHELL. The o/put i got is... (5 Replies)
Discussion started by: dreams5617
5 Replies
Login or Register to Ask a Question