I need to start a java process and get a valid pid for it to be able to kill it later. So, I thought I could use startproc in this way 'startproc -f -p /var/run/myproc.pid /usr/bin/java/ jarfil.jar' But noway.. I don't get a pid file at all. So, how should I do to get a valid pidfile for the process? Anders.
Anders, On Saturday 09 April 2005 12:01, Anders Norrbring wrote:
I need to start a java process and get a valid pid for it to be able to kill it later.
So, I thought I could use startproc in this way 'startproc -f -p /var/run/myproc.pid /usr/bin/java/ jarfil.jar'
But noway.. I don't get a pid file at all. So, how should I do to get a valid pidfile for the process?
1) Choose a directory for the PID file to which you have write access. 2) Give yourself write access to /var/run. 3) Create (as root) a writable sub-directory of /var/run. I'd probably go with (3). And I'd also use a more distinctive name than "myproc.pid". Furthermore, the syntax for invoking a so-called "executable JAR file" is "java -jar jarfile.jar". (Assuming the java launcher's directory is in your PATH. Use the full path name, say "/usr/lib/java/bin/java", if it's not.) If the JAR file does not have the necessary manifest information to be "executable," you'll have to discover the name of the class that implements the static main() method that is the entry point for the application.
Anders.
Randall Schulz
On Saturday 09 April 2005 12:01, Anders Norrbring wrote:
I need to start a java process and get a valid pid for it to be able to kill it later.
So, I thought I could use startproc in this way 'startproc -f -p /var/run/myproc.pid /usr/bin/java/ jarfil.jar'
But noway.. I don't get a pid file at all. So, how should I do to get a valid pidfile for the process?
1) Choose a directory for the PID file to which you have write access. 2) Give yourself write access to /var/run. 3) Create (as root) a writable sub-directory of /var/run.
I'd probably go with (3). And I'd also use a more distinctive name than "myproc.pid".
Furthermore, the syntax for invoking a so-called "executable JAR file" is "java -jar jarfile.jar". (Assuming the java launcher's directory is in your PATH. Use the full path name, say "/usr/lib/java/bin/java", if it's not.) If the JAR file does not have the necessary manifest information to be "executable," you'll have to discover the name of the class that implements the static main() method that is the entry point for the application.
Randall, I don't know much about the jar file itself, it's the Azureus BitTorrent client. I guess I can find out somehow by unpacking it, or is that not possible? Anyway, I tried what you suggested about a new directory, but no file was written to it. Startproc displays the pid when it exists, but that's the only trace I can get without running a ps to look it up manually, which isn't good since I want to run it scripted. Startproc isn't a "must", I can start it with screen instead if that will make any difference. I thought I could make use of the bash $$ somehow, but it seems like I can't get it to echo the java process pid, but only the pid "before", the parent script's pid that is... Any other suggestions? Anders.
Anders, On Saturday 09 April 2005 14:20, Anders Norrbring wrote:
...
Randall,
I don't know much about the jar file itself, it's the Azureus BitTorrent client. I guess I can find out somehow by unpacking it, or is that not possible?
In all likelihood, it is equipped with the necessary annotations. If "java -jar jarfile.jar" launches the program, then that's probably all you need to know (aside from any arguments and options specific to the program itself). If not, then the documentation should tell you how to launch the program. You can unpack a JAR file using either the "jar" command or the zip, unzip and zipinfo commands (JAR files are the same format as PKZip files).
Anyway, I tried what you suggested about a new directory, but no file was written to it. Startproc displays the pid when it exists, but that's the only trace I can get without running a ps to look it up manually, which isn't good since I want to run it scripted.
I've never used startproc, so I don't know what the deal might be with it, but it wouldn't hurt to give the precise invocation you're using as well as "ls -l" (or -ld) listings of the various files and directories you're using so we can see what might be going wrong.
Startproc isn't a "must", I can start it with screen instead if that will make any difference. I thought I could make use of the bash $$ somehow, but it seems like I can't get it to echo the java process pid, but only the pid "before", the parent script's pid that is...
The value "$$" is the shell's process ID. Ordinarily shells fork (creates a new process) for each command executed, whether entered interactively or via a shell script. However, you can cause the shell to overlay itself with the new program (irreversibly) by using the "exec" built-in. In that case, the command so executed will be the same process as the shell a hence will have the same process ID. So something like this will leave a reliable record of the server's process ID: echo $$ >|.../server.PID exec java -jar jarfile.jar # Nothing from here on will be executed
Any other suggestions?
Are you sure killing the program is the proper way to shut it down? Java does not provide any mechanisms that allow the program to intercept signals and perform clean-up operations before exiting. I'd look through the documentation to see if there's a better-controlled way to shut down the server. Often such servers will accept operational commands through a special port, possibly only from processes connecting from the same machine on which it runs (for security purposes).
Anders.
Randall Schulz
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote:
I don't know much about the jar file itself, it's the Azureus BitTorrent client. I guess I can find out somehow by unpacking it, or is that not possible?
Anyway, I tried what you suggested about a new directory, but no file was written to it. Startproc displays the pid when it exists, but that's the only trace I can get without running a ps to look it up manually, which isn't good since I want to run it scripted.
Startproc isn't a "must", I can start it with screen instead if that will make any difference. I thought I could make use of the bash $$ somehow, but it seems like I can't get it to echo the java process pid, but only the pid "before", the parent script's pid that is...
Any other suggestions?
Anders.
If this is for a script, just start the program normaly and try somthing like: azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid -- Louis Richards
On Sun, 2005-04-10 at 09:02 -0400, Louis Richards wrote:
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote: If this is for a script, just start the program normaly and try somthing like:
azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid
If the name of the process shows up with the ps command than just use killproc <process name> to kill it. man killproc gives more detail. -- Ken Schneider UNIX since 1989, linux since 1994, SuSE since 1998 "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners." -Ernst Jan Plugge
On Sunday 10 April 2005 15.08, Ken Schneider wrote:
On Sun, 2005-04-10 at 09:02 -0400, Louis Richards wrote:
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote: If this is for a script, just start the program normaly and try somthing like:
azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid
If the name of the process shows up with the ps command than just use killproc <process name> to kill it. man killproc gives more detail.
-- Ken Schneider UNIX since 1989, linux since 1994, SuSE since 1998
"The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners." -Ernst Jan Plugge
Azureus starts a number of threads. so just ps'ing for it wont help. You'll get anywhere from one to x number of pid:s You COULD start it by adding "&" to the line box:> java -jar jarfile.jar & [1] 1861 box:> It gives the job number and the PID -- /Rikard " Sharing knowledge is the most fundamental act of friendship. Because it is a way you can give something without loosing something." -R. Stallman --------------------------------------------------------------- Rikard Johnels email : rikjoh@norweb.se Web : http://www.rikjoh.com/users/rikjoh Mob : +46 735 05 51 01 PGP : 0x461CEE56 ---------------------------------------------------------------
Rikard, On Sunday 10 April 2005 06:42, Rikard Johnels wrote:
On Sunday 10 April 2005 15.08, Ken Schneider wrote:
On Sun, 2005-04-10 at 09:02 -0400, Louis Richards wrote:
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote: If this is for a script, just start the program normaly and try somthing like:
azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid
If the name of the process shows up with the ps command than just use killproc <process name> to kill it. man killproc gives more detail.
-- Ken Schneider
Azureus starts a number of threads. so just ps'ing for it wont help. You'll get anywhere from one to x number of pid:s
That's only true if you're still running a 2.4-series kernel. In 2.6 the threading works differently and you don't see a separate process table entry or ps output line for each thread. By the way, all Java programs have at least two threads, the main one and the garbage collector thread.
You COULD start it by adding "&" to the line
box:> java -jar jarfile.jar &
[1] 1861 box:>
It gives the job number and the PID
In that case, the most expedient way to access the PID of the newly created process is the $! variable.
/Rikard
Randall Schulz
Citerar Randall R Schulz
Rikard,
On Sunday 10 April 2005 06:42, Rikard Johnels wrote:
On Sunday 10 April 2005 15.08, Ken Schneider wrote:
On Sun, 2005-04-10 at 09:02 -0400, Louis Richards wrote:
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote: If this is for a script, just start the program normaly and try somthing like:
azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid
If the name of the process shows up with the ps command than just use killproc <process name> to kill it. man killproc gives more detail.
-- Ken Schneider
Azureus starts a number of threads. so just ps'ing for it wont help. You'll get anywhere from one to x number of pid:s
That's only true if you're still running a 2.4-series kernel. In 2.6 the threading works differently and you don't see a separate process table entry or ps output line for each thread.
By the way, all Java programs have at least two threads, the main one and the garbage collector thread.
You COULD start it by adding "&" to the line
box:> java -jar jarfile.jar &
[1] 1861 box:>
It gives the job number and the PID
In that case, the most expedient way to access the PID of the newly created process is the $! variable.
Thanks to all of you that replied, however, Randall's alternative was the way to go. I start it in the script like 'java -jar prog.jar &' Then a 'echo $! > pidfile.pid' Works great! That way I can kill it without killing the other java processes. Someone suggested 'killall -KILL java' which in my case wouldn't be very good since I have a whole bunch of other java processes running! Later, Anders ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
On Sun, 2005-04-10 at 15:42 +0200, Rikard Johnels wrote:
On Sunday 10 April 2005 15.08, Ken Schneider wrote:
On Sun, 2005-04-10 at 09:02 -0400, Louis Richards wrote:
On Saturday 09 April 2005 05:20 pm, Anders Norrbring wrote: If this is for a script, just start the program normaly and try somthing like:
azureus_pid=`/bin/ps -C azureus -o pid=` kill $azureus_pid
If the name of the process shows up with the ps command than just use killproc <process name> to kill it. man killproc gives more detail.
Azureus starts a number of threads. so just ps'ing for it wont help. You'll get anywhere from one to x number of pid:s
You COULD start it by adding "&" to the line
box:> java -jar jarfile.jar &
Yes but killproc is used to kill -all- of the instances of the proc. -- Ken Schneider UNIX since 1989, linux since 1994, SuSE since 1998 "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners." -Ernst Jan Plugge
participants (5)
-
Anders Norrbring
-
Ken Schneider
-
Louis Richards
-
Randall R Schulz
-
Rikard Johnels