[Tutor] Watch and control access to an executable
Bill Burns
billburns at pennswoods.net
Sat Apr 8 18:02:41 CEST 2006
[Bill]
>> Desktop to the executable). The problem is, only *one* person at a time
>> should run the program.
>
[Alan]
> The usual way of doing this is simply to create an empty file
> when the program starts and delete it when the program closes
> In python:
>
> ##############
> # File : exRun.py
> ''' usage python exRun.py foo.exe
>
> prevents more than one copy of the guarded program running
> '''
>
> import os, sys
>
> if len(sys.argv) != 2:
> print "Usage: python exRun.py <application name>"
> sys.exit()
>
> prog = sys.argv[1]
> fname = prog+'running.dat'
> try:
> open(fname,'w').close() # create an empty file.
> os.system(prog)
> os.remove(fname)
> except IOError:
> print prog, 'is already running, try again later'
> sys.exit()
> ################
Alan,
Thank you for the code above . I'll take a look at it. Much shorter than
my approach :-)
[Bill]
>> 4. If 'otherProg' is not running, start it and write to the config file
>> that 'otherProg' is running (also write who is running it).
>> 5. 'myProg' continues to run on the user's computer, continuously
>> checking the PID (of 'otherProg') to see if the process is still alive.
>> 6. When we don't find the PID anymore, write to the config file that
>> 'otherProg' isn't running.
>> 7. Shutdown 'myProg'.
[Alan]
> The design above will work too of course but uses more CPU cycles
> and may be slightly harder to fix if the machine crashes while the app
> is running sincve the open config file could be corrupted.
My first attempt at continuously checking the PID was using a lot of CPU
time. Then I added a sleep(.5) in my loop and the CPU % dropped to about
0% (at least according to Task Manager).
[Bill]
>> Now in step #3 above - if 'myProg' reads the config file and finds that
>> the 'otherProg' is currently running, the user is warned that 'The
>> program is currently in use by <insert username from config file>!' And
>> 'otherProg' is not started.
[Alan]
> The current user name can be obtained from getpass.getuser() if you need
> it.
Yeah, my code uses that too.
[Bill]
>> 2. 'myProg.exe' and the config file live on the network in the same
>> directory as 'otherProg'.
[Alan]
> There's no real need for that. My version usually lives in a common
> area since it can be used to control multiple apps, and it usually writes
> its file into a semi-hidden folder to avoid unscrupulous users deleting
> the file!.
I think I was just trying to make it easy for my program to find the
program to start (since it'll look in the current directory first). I
didn't want to hard-code any paths. But it looks like with your
approach, I just issue the path to the executable on the command line
and be done with it.
Regarding the config file deletion - I've set the permissions on the
.ini so that users can read & write to the file, but not delete it. But
certainly a user with Admin privs could still delete it. Also , if my
program doesn't find the .ini file - it creates a new one (although it
must be setup again).
[Bill]
>> BTW, I've never stopped to consider if there was a simple 'Windows
>> networking / permissions' type solution to the problem. I just went
>> straight to Python :-)
[Alan]
> You can do it all using DOS style BAT files if you use my approach.
I just knew someone would come up with an easier way :-)
Thanks again, Alan! I'll play around with the code you posted.
Bill
More information about the Tutor
mailing list