#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    start stop service


    hi folks of this forum,

    I want to make service with python for some rotating procedures for myself under xp.

    I want to start and stop this service from console like that;

    myservice start

    myservice stop

    my codes like that


    import os
    import sys

    param = sys.argv[1]

    print "service is starting"

    while param == "start":
    # all procedures



    print "service is stopping"

    but something is wrong, because it doesn't work

    regards
  2. #2
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    Where it says do whatever here in the code below, have it start the service.. using os.system() or whatever
    Code:
    import os
    import sys
    
    if sys.argv[1].lower() == "start":
        print "service is starting"
        # Do whatever here
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just one more idea. Give it a try this...

    Code:
    #!/usr/bin/env python
    
    import os, sys
    
    arguments = sys.argv[1:] #You may want to convert these to lower case here i.e. using map().
    commands = {'start': 'command', 'stop': 'command'}
    
    for argument in arguments:
        for command in commands:
            if argument == command:
                os.system(commands[command])
    What this does is creates a copy of sys.argv without the first argument (the files name) and a dictionary of valid commands. This acts something like a switch statment in other languages (in that it does what you would have to do with multiple elif blocks.)

    One advantage of this aproch is that you can pass multiple commands at the same time i.e. you could start the proccess then stop it - pointless with this example but it could be useful for others.

    Note: I don't have access to Python right now so the code itself is untested. But it should give you some ideas .

    Hope this helps,

    Mark.
    Last edited by netytan; September 22nd, 2004 at 02:46 AM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    thanks but


    thanks but, I need that it can be stayed alive procedures when I stop it ;

    import os
    import sys
    import time

    while sys.argv[1].lower() == "start":
    print "service is starting"
    # Do whatever here
    time.sleep(60)

    and when write this command from command line ;

    myservice stop

    it has to finish
    thats all , perhaps it is too easy but I am new for python, just I started try somethings two months ago.

    best regards
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sorry, I've never seen what you want done - arguments from the command line are usually only read when the program first starts; not through-out the execution - but what exactly is the process you want to run?

    There are of course a few hackish ways to do this that I can think of.

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    thanks netytan


    thanks netytan, allways you answer to my questions and most of them are here.

    my suppose was start and stop my aplication like apache.

    myservice start ... and
    myservice stop

    something like that. perhaps I can find a solution for it using two programs.

    however, I writed an aplication which checking two mysql server and comparing them. It is working by periods with time.sleep() . and I compiled it with py2exe and it does work well.
    I made it with your helps. many thanks for everything

    best regards
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    mohikan,

    If you don't already have it - get a copy of "Python programming on Win32" by Mark Hammaond - then read the chapter on Windows NT services.

    You must have the Win32 extensions installed.
    Win32
    C:\Python23\Lib\site-packages\win32\Demos\service
    has an example of a service and a client.

    It is not a small topic and the code you posted is not enough for us to help in any detail.

    grim
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Your very welcome. Anyway, a simple solution would be to have your command line interface start the program and write 1 to a file when 'start' is passed to it.

    Then have your main program check the contents of a file in order to deside if it needs to keep running.

    If the contents of the file isn't 1, say it changes to 0, then your program will break from the loop and exit (the process will stop).

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    36
    Rep Power
    11

    thanks alot both of you guys


    I tried two weeks ago what you said netytan, and it did work.
    I installed win32 and I checked your advice grim. It is helpfull.


    many thanks both of you

    best regards

IMN logo majestic logo threadwatch logo seochat tools logo