running a python script in the background

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arun

    #1

    running a python script in the background

    Hi,

    This is a scripting question, but since I am writing the script in
    python I am posting this question here:

    I have a python script that runs a simulator (that was written in c++,
    so I use the system() function to run the simulator) over a list of
    config files. I want the python script to itself run as well as start
    the simulations totally in the background (the simulations run for a
    day or two, so basically I want to start them in background and log
    off).

    The structure of script is somewhat like this:
    --------------------------------

    Create the config files

    Loop through the list of config files:
    system("simulat or configfile");

    ---------------------------------

    Now if I start the script in the background with "script.py &", it
    doesn't work. I figure the reason is the system() call spawns the
    simulator in the foreground.

    If I change the system call to system("simulat or configfile &"), then
    the script will spawn off the simulator with the config files all at
    once.

    Is there a way I can make this all run in the background, while at the
    same time ensuring that only one instance of the simulator is running?

    Thanks!,
    Arun
  • Josiah Carlson

    #2
    Re: running a python script in the background


    arun.sivakumara n@gmail.com (Arun) wrote:[color=blue]
    > the simulations totally in the background (the simulations run for a
    > day or two, so basically I want to start them in background and log
    > off).[/color]
    [color=blue]
    > Now if I start the script in the background with "script.py &", it
    > doesn't work. I figure the reason is the system() call spawns the
    > simulator in the foreground.[/color]
    [color=blue]
    > Is there a way I can make this all run in the background, while at the
    > same time ensuring that only one instance of the simulator is running?[/color]

    It sounds like you are running this on *nix. The Python interpreter is
    likely dying when you log-off due to a 'hangup' signal being called when
    you log out.

    What has worked for me in the past is to SSH to localhost, executing:
    "nohup python script.py &" and disconnecting. It should work running
    from a local terminal (without SSH), but I can't guarantee it.

    - Josiah

    Comment

    • Arun Sivakumaran

      #3
      Re: running a python script in the background

      Actually, I am working from a 32-bit linux client and am SSHing to a
      64-bit itanium server (not sure what's being run there).

      I had tried the nohup option too before, that doesn't seem to help.

      Thanks,
      Arun


      On Fri, 29 Oct 2004 23:08:25 -0700, Josiah Carlson <jcarlson@uci.e du> wrote:[color=blue]
      >
      > arun.sivakumara n@gmail.com (Arun) wrote:[color=green]
      > > the simulations totally in the background (the simulations run for a
      > > day or two, so basically I want to start them in background and log
      > > off).[/color]
      >[color=green]
      > > Now if I start the script in the background with "script.py &", it
      > > doesn't work. I figure the reason is the system() call spawns the
      > > simulator in the foreground.[/color]
      >[color=green]
      > > Is there a way I can make this all run in the background, while at the
      > > same time ensuring that only one instance of the simulator is running?[/color]
      >
      > It sounds like you are running this on *nix. The Python interpreter is
      > likely dying when you log-off due to a 'hangup' signal being called when
      > you log out.
      >
      > What has worked for me in the past is to SSH to localhost, executing:
      > "nohup python script.py &" and disconnecting. It should work running
      > from a local terminal (without SSH), but I can't guarantee it.
      >
      > - Josiah
      >
      >[/color]


      --
      -AP
      Arun.Sivakumara n@gmail.com
      (720)-771-7543

      Comment

      • Cliff Wells

        #4
        Re: running a python script in the background

        On Sat, 2004-10-30 at 02:42 -0600, Arun Sivakumaran wrote:[color=blue]
        > Actually, I am working from a 32-bit linux client and am SSHing to a
        > 64-bit itanium server (not sure what's being run there).
        >
        > I had tried the nohup option too before, that doesn't seem to help.[/color]



        Regards,
        Cliff

        --
        Cliff Wells <clifford.wells @comcast.net>

        Comment

        • Dirkjan Ochtman

          #5
          Re: running a python script in the background

          I've found in the past that it might work better when you redirect
          stdout and stderr, since errors will arise when the Python script
          suddenly doesn't have either anymore because your session ended.

          python script.py >& /dev/null &

          HTH,

          Dirkjan
          [color=blue][color=green]
          >>What has worked for me in the past is to SSH to localhost, executing:
          >>"nohup python script.py &" and disconnecting. It should work running
          >>from a local terminal (without SSH), but I can't guarantee it.
          >>
          >> - Josiah[/color][/color]

          Comment

          • Pierre Barbier de Reuille

            #6
            Re: running a python script in the background

            I'd use "screen". This is a command that creates shells you can
            disconnect/reconnect.

            So you launch screen and launch your program inside it. Then, you can
            disconnect from the screen and log out. When you'll log in, you'll be
            able to reconnect to your screen and see the result of your program.

            Pierre

            Comment

            • Arun

              #7
              Re: running a python script in the background

              Pierre Barbier de Reuille <pierre.barbier @cirad.fr> wrote in message news:<4183abf9$ 0$28744$636a15c e@news.free.fr> ...[color=blue]
              > I'd use "screen". This is a command that creates shells you can
              > disconnect/reconnect.
              >
              > So you launch screen and launch your program inside it. Then, you can
              > disconnect from the screen and log out. When you'll log in, you'll be
              > able to reconnect to your screen and see the result of your program.
              >
              > Pierre[/color]

              Hi all,

              Thanks for those replies. I'll try out the solutions suggested.

              -Arun

              Comment

              • Arun

                #8
                Re: running a python script in the background

                The reason I wasn't able to run the script in the background was
                because my simulator was outputting debug messages in the foreground.
                The following seemed to do the trick:

                script.py
                --------------------------------

                Create the config files

                Loop through the list of config files:
                system("simulat or configfile >> & logfile");

                ---------------------------------[color=blue][color=green]
                >>script.py >> & logfile &[/color][/color]


                Thanks!
                Aun

                Comment

                Working...