30

Is there a way to shutdown Eclipse cleanly from the command line, such that files and workspaces are saved? kill -3 doesn't do anything. kill -1 and kill -15 (default) causes Eclipse to exit abruptly with JVM termination popup. kill -9 does the same thing.

The use case is that I'm working remotely on a machine with Eclipse loaded on it, and I want to save memory by closing Eclipse, but I want Eclipse to save its state first.

I could use VNC or some alternative desktop sharing software, but that's really heavy-weight, and I'd much prefer a command line solution.

EDIT: System info: RHEL5.1 64-bit using GNOME

Maian
  • 528
  • 2
  • 5
  • 11

7 Answers7

43

I figured this out with the help of gigi's answer and another question. You're going to need the wmctrl and xdotool utilities from your package manager.

Unless you're running in a terminal emulator on the same display, you need to set the right display:

$ export DISPLAY=:0.0

Then (irrelevant windows elided from example):

# List windows
$ wmctrl -l
...
0x030000fa  0 kcirb Java - Eclipse

# Tell Eclipse window to close gracefully
$ wmctrl -c eclipse

# Darn, there's a confirmation dialog
$ wmctrl -l
...
0x030000fa  0 kcirb Java - Eclipse 
0x03003c2d  0 kcirb Confirm Exit 

# Send return key to the window
$ xdotool key --window 0x03003c2d Return

Worked for me on Ubuntu 12.04, at least.

EDIT: See Scarabeetle's answer for the tweaks you need to make it work from a script.

pidge
  • 1,037
  • 9
  • 26
  • 3
    thanks! That worked perfect. One minor point of improvement `xdotool search Exit` is unnecessary as it returns the decimal form of `0x03003c2d`. You can directly use the hex Window ID from `wmctrl -l` like `xdotool key --window 0x03003c2d Return` and skip the search step. – rkyser Dec 19 '13 at 21:18
7

Not enough reputation to comment on pidge's answer above... It almost works, but I needed to wait for some Gnome3 animation to finish and then give focus to the "Confirm Exit" window:

export DISPLAY=:0.0        # Do this in main X session
wmctrl -c "Eclipse SDK"    # Close main window
sleep 1                    # Wait for animation
wmctrl -a "Confirm Exit"   # Give focus to the dialog
# Send a Return keypress to press the OK button
xdotool key --window $(xdotool search "Confirm Exit") Return
Community
  • 1
  • 1
Scarabeetle
  • 525
  • 5
  • 6
3

Try killing java process(es). Do ps -ea | grep java

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
3

Any added ShutdownHooks (more info here) should be executed by the JVM when terminated by SIGTERM. Therefore, I think the problem is the way Eclipse is programmed to deal with such signals.

As I don't know how the cleanup process is implemented in Eclipse, I can only assume that it is not called by any ShutdownHook (and rather by an Action or something similar).

Edit: pidge has provided an answer below however which details steps which should allow you to shutdown Eclipse cleanly from the command line.

Community
  • 1
  • 1
vehk
  • 926
  • 7
  • 7
  • 1
    This is right. A plugin developer could write a plugin to listen on a socket for a close command, and call `IWorkbench.close()`. Or eclipse and the launcher could be updated to support more than just the openFile action. But there's nothing that comes with eclipse by default. – Paul Webster Jun 20 '11 at 14:30
  • today I just call $ kill pid – jorgeu Mar 04 '15 at 17:43
  • kill -HUP pid works well for me. This gets eclipse to shutdown but still gives it a change to delete all its lock files etc. – Andrew Rice May 18 '16 at 08:10
1

Did you tried with wmctrl? wmtrl -l lists the windows and wmlctrl -c -P should close the window. Anyway you could have problems with the confirmation dialog of eclipse.

gigi
  • 796
  • 9
  • 21
  • Installed it and twiddled with it a bit. Didn't work remotely (via ssh shell). Locally, it doesn't even list the eclipse window. Maybe I'm just using it wrong? – Maian Jun 17 '11 at 21:54
  • You need to set the DISPLAY environment variable `export DISPLAY=:0.0` if you're running the command remotely. See my answer for a walkthrough. – pidge Oct 13 '12 at 06:36
0

The answer to this question was helpful to me in a similar issue: Eclipse hanging, how to kill it properly?

After I killed the eclipse process the Eclipse window kept there until I killed the java process (I didn't have a javaw process as in the answer above. I had only one "java" process that when killed fixed the problem).

Community
  • 1
  • 1
user3131978
  • 37
  • 1
  • 1
  • 7
0

Did you try kill -HUP (kill -1)? -- that's the canonical way to tell a process that whoever was interacting with it has gone away and it should clean up appropriately

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226