Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Process handling with Runtime.exec()

Thread view: 
Natanael Mignon - 23 May 2004 21:05 GMT
Good evening to you,

what I am trying to do is start an external process, let it live and
destroy it later on (by call of a method, i.e. when it is not needed
anymore). Now this process is a ssh commandline opening a ssh-tunnel
over which I want to communicate. After this communication is done, I'd
like to end the ssh-process.

At the moment I try it this way:

Process proc = null;
...
proc = Runtime.getRuntime().exec(sshCommandLine);
...
(method invoked for closing)
proc.destroy();

Unfortunately, I keep getting a NullPointerException from proc.destroy()
- the process seems to have disappeared (but in fact it is still there).

I need to keep track of the process - but I cannot get a pid or
something like that back from Runtime.exec() nor could I find any other
useful Classes or Methods helping me there.
Furthermore there is no way telling, whether the exec() has been
successful at all, I think. Asking for the returnValue tells me the
process has not yet exited. Humm.

Any ideas and help is highly appreciated. :)

Kind regards
Signature

 - Nat

www.bleeding.de | www.metal-germany.de

 -= Bleeding for Metal | c/o Madhouse Of Cain =-
 -= P.O. Box 39 23 | 30039 Hannover | Germany =-

Gordon Beaton - 24 May 2004 07:49 GMT
> Now this process is a ssh commandline opening a ssh-tunnel over
> which I want to communicate. After this communication is done, I'd
[quoted text clipped - 8 lines]
> (method invoked for closing)
> proc.destroy();

> Unfortunately, I keep getting a NullPointerException from
> proc.destroy()
> - the process seems to have disappeared (but in fact it is still
>   there).

If proc.destroy() causes NullPointerException, then I am certain that
proc is null. This has nothing to do with whether the child process is
still running, it has only to do with your handling of the "proc"
variable itself.

> I need to keep track of the process - but I cannot get a pid or
> something like that back from Runtime.exec() nor could I find any
> other useful Classes or Methods helping me there. Furthermore there
> is no way telling, whether the exec() has been successful at all, I
> think. Asking for the returnValue tells me the process has not yet
> exited. Humm.

The child process itself can tell you its pid. One simple way is to
run ssh from a script like this one (call this script from
Runtime.exec()):

 #!/bin/sh

 echo $$
 exec ssh "$@"

The first line you read from proc.getInputStream() will contain the
pid of the child process.

However there are two simple ways to terminate the ssh process:

1. close its stdin stream (proc.getOutputStream().close())
or
2. send a newline followed by ~. (tilde dot).

(1) may not work in all cases, for example if ssh needs to wait for
forwarded X11 connections to terminate.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Natanael Mignon - 24 May 2004 09:10 GMT
Hi Gordon,

>>Unfortunately, I keep getting a NullPointerException from
>>proc.destroy()
[quoted text clipped - 4 lines]
> still running, it has only to do with your handling of the "proc"
> variable itself.
Mh, I can hardly imagine that, but that may just be a problem of my
imagination (as it obviously is true). ;)

[...]
> However there are two simple ways to terminate the ssh process:
Thanks a lot, very good ideas!

Kind regards
Signature

 - Nat

www.bleeding.de | www.metal-germany.de

 -= Bleeding for Metal | c/o Madhouse Of Cain =-
 -= P.O. Box 39 23 | 30039 Hannover | Germany =-



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.