Headless macro does not exit after finishing

Hi all,

with current Fiji we are facing pretty much the same problem as described in bug #874, except that in our case neither of the suggested

run("Quit");
eval("script", "System.exit(0);");

is helping - Fiji does not exit at all any more (at least not during the time I was willing to wait, which were several minutes).

Tested this with a vanilla Fiji on CentOS-7 and Ubuntu 15.04.

Any hints?

Cheers,
~Niko

1 Like

Yep, there are definitely bugs in the current headless behavior; I updated the bug report. Hoping the problems will be resolved with these changes:

  1. Clean up single-instance listener
  2. Dispose the ImageJ context when headless

Hoping to have a release uploaded within the next day or so.

3 Likes

Thanks a lot for the update, @hinerm - I’ll keep an eye on it and try it in the next days!

Oh, that’s funny. I had the exact same question that I was drafting yesterday and got distracted. Finished it this morning without seeing yours (did not come up on the sidebar list of similar questions).

Note: Just saw this same question (was not there when I started drafting my question over the weekend). Pretty much a duplicate.

I am setting up to run some batch processing on a linux cluster using headless mode. I can successfully run a test script, and also output to stdout with the print statement. However, the function doesn’t return. I tried exit(); but got:

ReferenceError: "exit" is not defined. (line#12)

I also tried throw("not an error...just want to quit");. The error message displays, but the function still doesn’t return.

arg_test.js

arg = getArgument();

if (arg=="")
{
        print("No argument!");
}
else
{
        print("The argument is: " + arg);
}

Output:

balter@WorkVDI:~/Documents$ ./fiji --headless -macro arg_test.js
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
No argument!
<CTRL-C>
balter@WorkVDI:~/Documents$ 
balter@WorkVDI:~/Documents$ ./fiji --headless -macro arg_test.js hello
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
The argument is: hello

How do I get the script to return? Do I need to appeal to a Java method? Also, do I need to worry about the warnings?

Looks like the same problem as this thread. I think your code is fine, it’s just a bug in the headless operation. We’ll try to get a fix pushed out soon.

Yup. Same problem. See my note at the top of my post and the reply to the other one. (I was drafting mine over the weekend, but got distracted. Finished it this morning, but hadn’t gone back to the main list before I submitted).

Thanks for working on it! I’ll stay posted for a solution. Would you mind updating our posts when you have one?

Just uploaded the fixes. You can get the latest update with Help > Update... and the Java 8 update site enabled.

Note that I updated the headless scripting guide recently.

Let me know if you have any more problems!

2 Likes

This is my first time trying to run this in headless mode, I’m so new to all types of programing I’m not sure if I am trying to call this correctly etc. I have the macro working fine with the graphical interface

is this the same problem or am I making a novice mistake? If its the same problem is there a work around? Is not can someone help me?

mlg-pacmanfrog:~ sms$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless -macro  ~/Desktop/MME.ijm
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Macro or script not found:
 
/Users/sms/Desktop/MME.ijm
Error while executing the main() method of class 'net.imagej.Main':
java.lang.RuntimeException: Macro canceled
    at ij.Macro.abort(Macro.java:76)
    at ij.IJ.error(IJ.java:654)
    at ij.plugin.Macro_Runner.runMacroFile(Macro_Runner.java:121)
    at ij.IJ.runMacroFile(IJ.java:148)
    at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:952)
    at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:949)
    at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:899)
    at net.imagej.legacy.IJ1Helper.runMacroFile(IJ1Helper.java:949)
    at net.imagej.legacy.LegacyCommandline$Macro.handle(LegacyCommandline.java:188)
    at org.scijava.console.DefaultConsoleService.processArgs(DefaultConsoleService.java:93)
    at net.imagej.legacy.LegacyConsoleService.processArgs(LegacyConsoleService.java:76)
    at net.imagej.Main.launch(Main.java:64)
    at net.imagej.Main.main(Main.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:279)
    at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:186)
    at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:77)

Double check that your macro file really exists at that location.

1 Like

Yup Novice error :slight_smile:. I had moved it several places trying to get it to run… I find the error codes to be jibberish often so while I try to read them more often than not, I don;t have the language to understand them. That said… I’m making progress and each time it get easier! Thanks for your help!

1 Like

I’m still having trouble with this bug, I cannot get my macro script to exit ImageJ after running. I have ImageJ version 1.51, updated with java8 update site enabled, running on windows 10. I’ve tried exit();, run("Quit"); and eval("script", "System.exit(0);"); to exit, but none of these work. I’m running my script as follows:

ImageJ-win64.exe --headless --console -macro test.ijm

The test.ijm macro file is located in the main folder together with ImageJ-win64.exe

Welcome to the forum, @Jhsmit!

As a first idea: are you using Java 8 with the Java-8 update site enabled? The old Java-6 version does not receive updates anymore.

If not, try downloading a fresh Fiji.

Hi, thanks for your reply. I think I’ve been using java 8 with java8 update sites enabled. To be sure I’ve tryed a downloading a fresh Fiji, as you suggested. Is it correct that this comes with java 8 bundled by default and java 8 update sites enabled? After downloading, I ran the updater which told me it is indeed up-to-date. In Plugins > Utilities > ImageJ Properties I find for java.version: 1.8.0_66, afaik this does correspond to Java.

Here is what I get in the console:

Smit@HPLAPTOP C:\Users\Smit\data_software\Fiji.app
$ ImageJ-win64.exe --headless --console -macro test.ijm

Smit@HPLAPTOP C:\Users\Smit\data_software\Fiji.app
$ Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Hello World

After which I have to ctrl-C to exit, which is of course only a slight inconvenience but I’m wondering if I’m doing something wrong. Also, I just noticed that after I run the command a new ‘line’ in the command line is opened (this smit@HPLAPTOP …). I’m not sure if this is related to the issue I’m having.

The script I’m trying to run:

print("Hello World")
eval("script", "System.exit(0);");

Where I replace the last line with the other options, run("Quit") and exit();
Finally, is it possible that other installations of ImageJ are interfering with this instance? I sometimes get the error:

[ERROR] Could not connect to existing ImageJ instance. If problem persists, please delete file: C:\Users\Smit\AppData\Local\Temp\ImageJ-Smit-7.stub

The ImageJ launcher does not connect properly to the Windows console, even with the --console flag set. It will emit output, but you’ll be returned to the prompt. Instead of pressing Ctrl+C, try pressing just Enter after you launch your script, and see if another prompt appears.

It also looks like you aren’t using the Windows Command Prompt, but are instead using a bash shell maybe? The ImageJ launcher has not been well tested from such shells on Windows.

The thing to check is whether your process is indeed not terminating. You can check the Task Manager to see if the process remains after, say, 10 seconds of launching your script, or whether it has shut down successfully. (The process might be called ImageJ-win64.exe or it might be javaw.exe; I am not sure.)

Pressing enter has the same effect as Ctrl-C, I ‘return’ to the console. Not sure how thats called exactly, but I see the file path where I am again and I can enter new commands.

I indeed don’t use Windows Command Prompt, but ConEmu. I dont think it should make a difference, I’ve switched to Windows Command Prompt which did not make any difference.

When I check the Task Manger I see that the process (ImageJ-win64.exe) is indeed being terminated. Interestingly, it also terminates if I omit the eval("script", "System.exit(0);"); line (or the other 2 options) from my script the process also terminates.

Does this mean the issue I’m experiencing is more related to ImageJ not properly connecting to the console than the process not terminating?

In any case, what I’m aiming to do is to call a macro from Python by using subprocess, and this does work fine.
(Other alternative to do this seems to be using CellProfilers RunImageJ plugin, but subprocess works for me and is way simpler)

Yes. I think there is no real problem here, actually. If you notice processes sticking around, though, that is a different story.

Great. That seems good indeed.

In the longer term, you might also be interested in this excitingly active project we have going on right now! It is an image server backed by ImageJ, callable via REST API from other languages. The hope is to update CellProfiler to use it for its ImageJ integration in the future, but it will have many other uses too.

Thanks for your help, I’ll definitely have a look at this in the future. An ImageJ server does sound very interesting to have!

1 Like