LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2013, 06:48 AM   #1
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Rep: Reputation: Disabled
how to run shell script command from php web page


hey i want to fire a script from a html page with the help of php.the output should be displayed on the terminal.not on html page.Please any one help me i am new here
 
Old 09-02-2013, 06:58 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,161
Blog Entries: 7

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
PHP has shell_exec: http://php.net/manual/en/function.shell-exec.php

Now, getting that into a terminal instead of html, that may be a bit trickier. If no one else has input on this, at this moment what I can think of is pipe output to a file and use tail to follow it's output.
 
1 members found this post helpful.
Old 09-02-2013, 07:17 AM   #3
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thank you for your reply but please elobarate as i am new to linux.
i want script to be run on terminal when request is passed from php web page
 
Old 09-02-2013, 11:17 AM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
What you ask for is pretty tricky. Realize that Linux does not have one "terminal", rather each process optionally has a controlling tty (look at the output of "ps aux" at some point). There can be multiple different ttys and pseudo-terminals (/dev/pty/X and /dev/pts/X) active on a system at any time. To make matters even more complex, many processes do not have a controlling terminal. In fact, this is a requirement for most daemons like a web server daemon (e.g. Apache or Nginx). Your PHP script is run by this daemon, which has no controlling terminal. In principle, with some custom coding you could open up a pseudo-terminal, but this would not be the same pseudo terminal that you would be logged into the system on. As you can see, what you ask for is not trivial.

Are you simply asking for a script to be run every time a web page on your site is viewed? If so, you might want to set up something that monitors your web server's access log and runs the appropriate command each time a new line is added. This seems like a much better way to do things. If, however, this is not what you want then you need to spell out in more detail what exactly you're trying to accomplish here.
 
Old 09-02-2013, 01:23 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,896

Rep: Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261
Quote:
Originally Posted by Ankush Seth View Post
Thank you for your reply but please elobarate as i am new to linux.
i want script to be run on terminal when request is passed from php web page
Well, PHP runs on anything, so if you're an experienced PHP programmer, the platform makes little difference. Can you post what you've written/tried so far? And as btmiller pointed out, 'terminal' can mean several things.

Think about what you're asking for:
  • A web-browser program that somehow can 'know' RELIABLY what OS a user is running
  • What the installed shell programs are on that system
  • How to open one if it's even there
  • And put output onto it.
Not to mention the fact that this is pointless. If you have a PHP program, then code things in PHP. If you have a shell script, then run it in a terminal window. Even a PHP program can be run from the command line in a terminal window, if you want to...so running it from a web page to interface with a terminal window is REALLY taking the long way around. And why would you even want to do this, when you can output the results from that script to the web page you ran it from??
 
Old 09-03-2013, 12:11 AM   #6
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
Hello mr.TB0ne and mr.Btmiller good morning
i am elaborating what i want..:-
1) I have a task which takes some arguments from user.
2) Now a php page will pass these arguments in a script which is to be run on a terminal .
3) The terminal then fires the script received and produces the output.
Please suggest me ways to do this task i will be very thankful to you!
 
Old 09-03-2013, 08:38 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,896

Rep: Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261
Quote:
Originally Posted by Ankush Seth View Post
i am elaborating what i want..:-
1) I have a task which takes some arguments from user.
2) Now a php page will pass these arguments in a script which is to be run on a terminal .
3) The terminal then fires the script received and produces the output.
Please suggest me ways to do this task i will be very thankful to you!
Yes, we understand what you WANT....did you understand what we told you??

Again, what you're asking for is almost impossible to do, and even if you CAN do it, it's POINTLESS. If you have a PHP page already, you can get input from the user there, and pass it to a script, and put the output of that script onto the same web page. There is NO REASON to use a terminal for ANY of this. And AGAIN, you can run PHP from the command line, so if you REALLY want to use PHP for this, feel free...you can run it from a terminal window.

Also, you still haven't posted ANY of the code you already have written, you don't tell us anything about the 'task' that takes arguments, the script that you want run, or given any solid details.
 
Old 09-03-2013, 08:44 AM   #8
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
ok mr.TBOne i understood the need
here is my SSC code in php
"shell_exec('mkdir file1');" for demo

Now problem is that it is performing all the commands are executing in this function such as ifconfig, pwd,etc but it can't create files/directories.
when i pass whoami to this function it returns "daemon".

Kindly guide me ho to do away with this.Thank you!
 
Old 09-03-2013, 10:35 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,896

Rep: Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261Reputation: 4261
Quote:
Originally Posted by Ankush Seth View Post
ok mr.TBOne i understood the need
here is my SSC code in php
"shell_exec('mkdir file1');" for demo

Now problem is that it is performing all the commands are executing in this function such as ifconfig, pwd,etc but it can't create files/directories.
Not surprising, since you're running this from a WEB PAGE, and that the user that the engine is running under, doesn't typically have permissions to DO such things. You also don't bother saying where the directory you're trying to create is, or if you have permissions to create a directory there, under the user ID that the web engine is running under.

Either put the user in the sudoer's file and use sudo, or do an "su - " in your script to another user.
Quote:
when i pass whoami to this function it returns "daemon".
...which is who you are, since AGAIN, the PHP code is running on a web engine, and that engine is running as a daemon.
Quote:
Kindly guide me ho to do away with this.Thank you!
Read the PHP coding instructions on how to handle external commands, since it doesn't appear as if you have yet.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to run another command from within shell script, when run from home directory blackray1 Linux - Newbie 7 07-01-2013 09:42 AM
How to execute a shell script from a simple html web page Shreerang Patwardhan Linux - Software 7 03-11-2010 11:12 AM
shell script to refresh a web page vinaytp Linux - Newbie 6 06-12-2009 12:55 AM
Running a shell script from a web page anirbande Linux - Newbie 1 12-01-2006 01:36 PM
How do I display real shell script output to web page. farmerjoe Programming 1 04-07-2005 01:03 PM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration