Process managers - How to run an exported sketch for ever

Hello, Processing community!

I have been trying to set up process managers for exported Processing sketches on Windows. Since the sketches will be used on permanent installs I have the following requirements for their execution:

  1. Autostart on boot
  2. Restart on crash/exit
  3. Pipe stdout and stderr to log files

I have tested using [pm2](https://pm2.keymetrics.io/) and [nssm](https://nssm.cc/) without success.

Using pm2, the process keeps being restarted without quitting; This means pm2 thinks the sketch crashed while the exe is still running. It will then restart is aka open another process/exe. I end up with about 30 instances of the same sketch running, thus crashing the PC.

Using nssm, nothing runs.

I can fall back to using the native Windows process management tools, with custom-written batch files, but I am wondering what is up with the Processing exports that make these tools not work.

If you have information or insights it would be much appreciated, I’m a bit in the weeds.

Renaud

Hi @renaudfv,

I didn’t know that pm2 could be used with non NodeJS applications but this is a nice feature :wink:

You are right, when I launch an exported app with pm2, it restarts the application over and over.

And doing this:

$ pm2 start .\processing_app.exe --no-autorestart
$ pm2 logs

PM2        | 2022-09-29T09:43:57: PM2 log: App [processing_app:0] starting in -fork mode-
PM2        | 2022-09-29T09:43:57: PM2 log: App [processing_app:0] online
PM2        | 2022-09-29T09:43:57: PM2 log: App [processing_app:0] exited with code [0] via signal [SIGINT]
PM2        | 2022-09-29T09:43:57: PM2 error: Error caught while calling pidusage
PM2        | 2022-09-29T09:43:57: PM2 error: Error: Aucune instance disponible.

Shows that the process directly exit with a zero return code which triggers the auto restart strategy.

I don’t really know how it work internally, I suppose it launches a new detached Java process running the sketch… (this is the case on Linux thought)

I think a solution might be to make a batch script that regularly checks if your application .exe name is still running and if not start it again (with a delay to prevent multiple instances).

EDIT:

Actually I found that Processing is using Launch4j to wrap the exported sketch into an .exe file on Windows. (link to the code)

And it sets the headerType value to gui which in the docs is this:

<headerType>
Type of the header used to wrap the application.

Header type Launcher Splash screen Wait for the application to close
gui javaw yes wrapper waits only if stayAlive is set to true, otherwise it terminates immediately or after closing the splash screen.
console java no always waits and returns application’s exit code.
jniGui32 (beta) launch4j yes always waits and returns application’s exit code.
jniConsole32 (beta) launch4j no always waits and returns application’s exit code.

<stayAlive>
Optional, defaults to false in GUI header, always true in console header. When enabled the launcher waits for the Java application to finish and returns it’s exit code.

And from the oracle docs:

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you do not want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

1 Like