Why I cannot use exit on macro file?

Dear All,

I am using a macro file when I run the compiled geant4 simulation program.
I can use all other commands in the macro file, but not “exit” (white picture)
the Geant4 simulation cannot read “exit” from the macro files, and the simulation is not terminated and is still in the edle (black picture)
I really want to use “exit” in the macro files, so I can make a shell script that runs the Geant4 simulation and close it, and run another Geant4 simulation and close it and do this.

please help me to use “exit”.

How is your main() function structured? The canonical way to implement a program that permits interactive or batch processing can be seen in the basic “B1” example, something like:

nt main(int argc,char** argv)
  // Detect interactive mode (if no arguments) and define UI session
  G4UIExecutive* ui = nullptr;
  if ( argc == 1 ) { ui = new G4UIExecutive(argc, argv); }


    // Get the pointer to the User Interface manager
  G4UImanager* UImanager = G4UImanager::GetUIpointer();

  // Process macro or start UI session
  if ( ! ui ) {
    // batch mode
    G4String command = "/control/execute ";
    G4String fileName = argv[1];
  else {
    // interactive mode
    UImanager->ApplyCommand("/control/execute init_vis.mac");
    delete ui;


  return 0;

This then runs in interactive mode (i.e. with a GUI or terminal UI) when run as

$ ./exampleB1

or in batch mode when run with an argument (path to macro):

$ ./exampleB1 mymacro.mac

which means you can have a bash script that does


exampleB1 first.mac
exampleB1 second.mac

and so on.

1 Like

Oh, Thank you so much. It solves the problem.
I added the UI on the batch processing line in my code a long time ago.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.