- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to read piped commands with JAVA
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2009 12:56 PM
тАО08-14-2009 12:56 PM
java6
I'd like to perform the following unix command from Java:
lsof -P|grep TCP
I'm using:
Process process = Runtime.getRuntime().exec( "lsof -P|grep TCP" );
but I get no output, only if I remove grep.
TIA for your help.
Juan.
Solved! Go to Solution.
- Tags:
- Java
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2009 02:26 PM
тАО08-14-2009 02:26 PM
Re: How to read piped commands with JAVA
like this:?
[...]exec( "sh -c 'lsof -P|grep TCP'" );
That may need to look more like this:
[...]exec( { "sh", "-c", "'lsof -P|grep TCP'" } );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2009 02:42 PM
тАО08-14-2009 02:42 PM
Re: How to read piped commands with JAVA
I don't do Java (as we say). However, in other languages, the lvalue of the system() call would return the status of the call, not the results.
Perhaps this helps:
http://www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2009 03:21 PM
тАО08-14-2009 03:21 PM
Re: How to read piped commands with JAVA
Right. The results would go to stdout/stderr or to a redirected file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2009 04:00 PM
тАО08-14-2009 04:00 PM
Re: How to read piped commands with JAVA
A long time ago, I looked at some Java code
for several minutes, so I still know nothing.
My theory is that the pipeline ("|") symbol
means something to a shell, not to lsof.
Therefore, if Runtime.getRuntime().exec uses
something in the execl()/execv() category,
then I'd expect all this stuff to be passed
into lsof, which might become confused and/or
unhappy. Java syntax aside, my plan is to
run a shell, to whom the pipeline ("|") makes
some sense, and pass it the (shell) command
which works so well when used interactively
(that is, when you give that command to a
shell).
One advantage to all these new programming
languages is that they make it easy to write
code while having no idea what the computer
is actually doing. Starting on a Univac 422
is obviously a superior method, which I
recommend to one and all. (There's very
little magic in a system with 512 15-bit
words of core memory.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2009 06:47 AM
тАО08-18-2009 06:47 AM
Re: How to read piped commands with JAVA
gets no output.
Guess java VM just process one proc at shell...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2009 07:14 AM
тАО08-18-2009 07:14 AM
Solutionexec( "sh -c 'lsof -P >/tmp/some_temp_file'" )
exec( "sh -c 'grep TCP /tmp/some_temp_file'" )
exec( "sh -c 'rm /tmp/some_temp_file'")
I know it is not the most elegant way of doing it or most efficient, I can even guarantee it is a resource hog by launching series exec calls, but if you are not going to execute your java code continuously, you can absorb the performance penalty.
Just a suggestion, otherwise, I am not a java programmer neither. Quoting JRF "I don't do anything with word JAVA in it" other than drinking copious amounts of Java everyday :)
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2009 08:08 AM
тАО08-18-2009 08:08 AM
Re: How to read piped commands with JAVA
(This doesn't use the OS, but the shell.)
>exec("sh -c 'lsof -P >/tmp/some_temp_file'")
If you invoke sh, you can use a pipe.