[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: check if another emacs instance is running

From: harven
Subject: Re: check if another emacs instance is running
Date: Sun, 29 Nov 2009 18:45:38 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

address@hidden writes:

> On Sun, Nov 29, 2009 at 01:22:28PM +0100, harven wrote:
>> Is there a function that checks if another instance of emacs 
>> is currently running ?
> Hm. What's the scope? Same user/same machine? Any user/same machine? On
> the local network?

I am interested in same user same machine at the moment.
But if you have some hints for all users on the same machine, I would
also appreciate your advices.

>> I didn't find anything. Yet there is a command that tests if a server
>> is running, and it seems to be reliable (works on all architectures, does not
>> depend on the way the server was started).
> At the start, the server puts a "cookie" file in a well-known place (for
> the current user). The client reads this file and knows how (which
> connection) to talk to the server. Besides, it can show the server that
> it's allowed to.

Indeed server-socket-dir defaults to /tmp/emacs1000. I guess that's the "file"
that is tested.

>>                                            Unfortunately I don't see how
>> it can be modified so as to detect emacs instances. I tried some workarounds
>> but none seems to be very satisfying.
> First you have to be clear on exactly what you want (see above).
>>   Use some external utility to list current processes e.g.
>>   (when (< 1 (string-to-number 
>>               (shell-command-to-string "ps -A |grep -c emacs23")))
>>     (message "ok"))
> I.e. any user in the system?

This would test for all users, ps ux would work for the current user I think.
Depends on the OS flavor... The kind of problem I would like to avoid.

>>   ;; but this is system-specific.
>>   Set an environment variable from emacs at start
>>   ;; won't detect instances launched as emacs -Q.
> Besides: this env variable will only be available to sub-shells of
> Emacs...

>>   Open a file and see if there is a lock
>>   ;; I had a look at userlock.el but did not understand how it works.

> Which file?

Yes, that's the problem, emacs -Q does not seem to open any file
so I don't see how to check for a lock.

> I think mimicking the server is the best way (if you have any influence
> on Emacs startup). I.e. writing a file in a well-known place. From there
> you might try to take measures to catch stale files, etc.

Same problem as above. If some previous instance was launched with the -Q 
I would like to be able to detect it. Maybe it still writes something somewhere.

I think checking processes is the way to go. I will follow Juanma advice
to use list-system-processes. It only works with emacs-version 23 and above 

> Regards
> -- tomás

reply via email to

[Prev in Thread] Current Thread [Next in Thread]