Don't Want the 2 Copies of Program Running

Linux specific forum
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Don't Want the 2 Copies of Program Running

Post by Alex »

How do I keep a program from running two copies of itself at once? Can I write the program so that it checks the currently running processes, and exits if it discovers that there is already a copy of itself in memory?

In BASH I'd simply do something like this:

Code: Select all

RESULT=`ps -A | grep -c foobar`
if [ RESULT > 1 ]
then
    exit
fi

Is there an equivalent for purebasic?

Thanks,

Alex
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Try RunOnlyOneInstance() from the Droopy Lib
Author : netmaestro
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

Thanks, I'll try it!

Alex
Droopy wrote:Try RunOnlyOneInstance() from the Droopy Lib
Author : netmaestro
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Good luck trying to run Windows library on Linux OS :)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Paul wrote:Good luck trying to run Windows library on Linux OS :)
:lol:
Good programmers don't comment their code. It was hard to write, should be hard to read.
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

Thanks for the heads up! I hadn't had a chance to look at the library yet.

Do you have a good suggestion for doing the same thing under Linux?

Thanks,

Alex
Paul wrote:Good luck trying to run Windows library on Linux OS :)
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Sorry :oops:
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

No objections. At least you tried. Is your library written in purebasic? Could it possibly be compiled on Linux?
Droopy wrote:Sorry :oops:
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Code: Select all

ProcedureDLL RunOnlyOneInstance()
  *a = CreateSemaphore_(NULL,0,1,GetProgramName()) 
  If *a <> 0 And GetLastError_()= #ERROR_ALREADY_EXISTS 
    CloseHandle_(*a) 
    End
  EndIf
EndProcedure
CreateSemaphore is Win32 API function.
You must look as the same function in the GTK ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

See on: http://lidn.sourceforge.net/ and search for: gdk_threads_mutex
i have no example for you :cry:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
olejr
Enthusiast
Enthusiast
Posts: 152
Joined: Sun Jul 11, 2004 7:48 pm
Location: Lillehammer, No(r)way
Contact:

Post by olejr »

I made a quick PB Library with only one function in it: AppRunning()..
There's no docs, or anything.. Just axtract and copy the apprunning_lib to the purelibraries/userlibraries folder..

Download: apprunning_lib.tar.gz
(source included)

Usage:

Code: Select all

;
; AppRunning(Name.s)  ; return 1 if app allready running, else 0
; 										; Name.s can be anything unique
;
; Compile the example like: pbcompiler test.pb -e exename
;
If(AppRunning("hello1224"))
  PrintN("App allready running...")
  End
EndIf

PrintN("Enter to exit")
Input()
End
Only "problem" is that if your app crash, or you quit by <ctrl-c>,
it take some time before the semaphore is released by the system..

Oh.. btw.. have no idea if the C source is the "right" way of doing a BP lib, but it seem to work.. :wink:
Alex
User
User
Posts: 41
Joined: Fri Aug 19, 2005 8:14 pm

Post by Alex »

Thanks, I'll give it a try.

Alex
olejr wrote:I made a quick PB Library with only one function in it: AppRunning()..
There's no docs, or anything.. Just axtract and copy the apprunning_lib to the purelibraries/userlibraries folder..

Download: apprunning_lib.tar.gz
(source included)

Usage:

Code: Select all

;
; AppRunning(Name.s)  ; return 1 if app allready running, else 0
; 										; Name.s can be anything unique
;
; Compile the example like: pbcompiler test.pb -e exename
;
If(AppRunning("hello1224"))
  PrintN("App allready running...")
  End
EndIf

PrintN("Enter to exit")
Input()
End
Only "problem" is that if your app crash, or you quit by <ctrl-c>,
it take some time before the semaphore is released by the system..

Oh.. btw.. have no idea if the C source is the "right" way of doing a BP lib, but it seem to work.. :wink:
"How is it possible to bring order out of memory? I should like to begin at the beginning, patiently, like a weaver at his loom. I should like to say, 'This is the place to start, there can be no other.'

-Beryl Markham
Post Reply