This is something that I recently did for my Data Collector for Twitter. It frequently ran out of heap space and terminated as a result. Since I wanted my Data collection to run 24x7, I had to Google quite a bit before I finally found the solution. I will summarize what I did here. Its simple, we will just set up a shell script which will run the program and then when the program gets terminated, it will wait for a few seconds and then start it again.

[gist id=14212877b05eb8b3459a]

This doesn’t need much explanation. You can probably skip the MAVEN_OPTS part as it is just to increase the heap space for my program. You can specify the main class with the -Dexec.mainClass argument. This is a shell script so you can directly execute it from command line using the sh command.