Prevent more than one instance of electron


Hello, so i noticed right now there is nothing to prevent a user from running electron.exe (or w/e you name it) 10 times, and having 10 of the applications open.

This becomes an issue, when only one instance of the app will support a system tray. So I need a way to prevent any more instances opening up.

I saw this code, however it did not seem to have any effect.

anyone have any solutions or ideas?

How do I prevent multiple instances of the same application built with electron?


this is the error you get when a second application loads up trying to make a system tray icon. Is there a way to catch that error? if so I can simply close the extra applications down at that point.


Could you catch that error and just exit? It won’t prevent them from starting another instance, but it won’t be around for long hopefully.


Hey Leedohm!

yeah, that’s exactly what I was asking, is there a way to catch that error?

Looking at the source, all it seems to do is

if (!result)
    LOG(WARNING) << "Unable to create status tray icon.";

and im not sure how to catch that when all it is doing is putting it in the log.

appIcon = new Tray(__dirname + '/images/icon.png');

is what im calling, so I know i would have to catch the error there, im just not sure how.


Where are you finding this code?

if (!result)
    LOG(WARNING) << "Unable to create status tray icon.";


edit: also to note. Im using electron 0.30.5 . We must use this version due to it being the latest version electron-edge supports


I’d file it as a bug on Electron and ask for support for that scenario. The problem is that it may be a platform-specific behavior which makes it harder to handle.

The other thing you can do is have some sort of sentinel that allows you to detect that there is more than one copy of the same app running. This can be as simple as a file on disk or just checking the process list for the current user.


You may want to look at how the Atom Editor itself handles this case.

Atom uses a unix domain socket to listen for the opening of a new instance. If an instance already exists, it closes the newly created instance.

I am not sure your exact use case, but the one caveat of this approach is that it is possible for two instances to exist momentarily.

EDIT: I see now that your use case may be in a need of a way to completely prevent opening (or at least catch the error you mentioned). Good luck!


Thank you both for your replies :smile:

@ leedohm I may do that so there is a report in and they can look at it for future users. However due to me being stuck on 0.30.5, a bugfix will not be able to help

I was able to do a work around, that is not ideal, but works. I call a C# function to get the number of processes. If its greater than 1 (as opening electron is 1 process no matter what) it will close down before attempting to open a new window or make a second tray.

was a bit tricky to get setup right, but seems to be working well enough right now.


Looks like I ran into an even bigger issue with tray.

so my app, works. If you pull the app to any computer from our repo, it works.

If you rename the folder, it works.

However, the SECOND you copy the folder, the app will crash and say it can not make the system tray icon.

sadly I can not see anyone else with a similar issue here.
when looking at the line where it errors in the electron source the comments say

// This can happen if the explorer process isn’t running when we try to
// create the icon for some reason (for example, at startup).

but there is no way thats the issue

EDIT - Solution found for anyone googling
I feel like this would have to be a bug, but ill bring this up as a ticket. You must go to resources/yourappfolder/package.json and rename the app to anything else, add a 2, add anything, and the tray icon will work. Appearntly if you have two on your computer with the same name, the tray icon wont load and will error

Found it

/* Single Instance Check */
var iShouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
    if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore();;
    return true;


@thinkdj i have done same,
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()

if (shouldQuit) {

But when one instance of app is open it do not open new intsance(which is correct),but is not restoring the current instance(which is Minimized)
Any idea?