LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-05-2005, 09:22 AM   #1
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Rep: Reputation: 15
run shell command with php


I want to run few shell commands with PHP. I need to run lsof -i :<port_no> and kill all the process running on that port number..
I tried"
<?php
$output = shell_exec('lsof -i :8080');
print "<pre>".$output."</pre>";
?>

but it did not return any results. however the command "ps -x" return results successfully..
What could be the issue..
please help
 
Old 12-05-2005, 09:47 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Can it find the command. I don't think lsof is in the normal path and so you may need to give the whole path to the command.

graeme.
 
Old 12-05-2005, 09:53 AM   #3
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Original Poster
Rep: Reputation: 15
this is a very common command to find the processes running on particular port..
 
Old 12-05-2005, 12:44 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Certainly when I check on my distribution (Fedora) it is not available a normal user. Yes the command is there but not unless I give the whole path. Since php is being executed as the user of the web server that user (apache) may not be able to see the command.

graeme
 
Old 12-05-2005, 09:22 PM   #5
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Original Poster
Rep: Reputation: 15
this also did not give any ouput

I tried giving full fath .. but still not able to get output..

<?php
$output = shell_exec('/usr/sbin/lsof -i :8080');
print "<pre>".$output."</pre>";
?>
 
Old 12-06-2005, 12:25 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Well, this page is quite informative: http://php.planetmirror.com/manual/e...shell-exec.php .
If you redirect stdout + stderr to a temp file, you may well find the lsof exe is not avail from the shell you are creating. The relevant msg is prob written to stderr, not stdout. Hence the above suggestion about saving it to a file.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP can't execute shell command ! rome Programming 3 11-15-2005 12:46 PM
PHP can't execute shell command ! rome Programming 1 11-15-2005 09:12 AM
run shell command inside of c code? khucinx Programming 2 05-17-2004 10:04 AM
how to run shell command in java? u4u Linux - General 3 03-02-2004 09:08 AM
PHP to run Shell commands Gear_freak2000 Linux - Software 4 06-04-2003 05:39 AM


All times are GMT -5. The time now is 04:39 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration