0

I need to track and launch few BASH scripts as process (if they for some reason crashed or etc). So i was trying as below: but not working

  def ps(self, command):
    process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE,  stdout=subprocess.PIPE)
    process.stdin.write(command + '\n')
    process.stdout.readline()

  ps("/var/tmp/KernelbootRun.sh")
  ps("ps aux | grep processCreator.py | awk '{print $2}' | xargs kill -9")

None is working.

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
  • possible duplicate of [Starting a background process in python](http://stackoverflow.com/questions/1196074/starting-a-background-process-in-python) – Kamiccolo Sep 18 '13 at 02:01

2 Answers2

1

How about running it through a subshell with disown:

import os
def ps(self, command):
  os.system(command + " & disown")

ps("/var/tmp/KernelbootRun.sh")
ps("ps aux | grep processCreator.py | awk '{print $2}' | xargs kill -9")

Note that sometimes you have to use a null input and output to keep your process active when the terminal is closed:

ps("</dev/null /var/tmp/KernelbootRun.sh >/dev/null 2>&1")
ps("</dev/null ps aux | grep processCreator.py | awk '{print $2}' | xargs kill -9 >/dev/null 2>&1")

Or perhaps define another function:

def psn(self, command):
  os.system("</dev/null " + command + " >/dev/null 2>&1 & disown")

psn("/var/tmp/KernelbootRun.sh")
psn("ps aux | grep processCreator.py | awk '{print $2}' | xargs kill -9")
konsolebox
  • 72,135
  • 12
  • 99
  • 105
0

This works great, as stand-alone process for my movie.

p.py:

import subprocess
subprocess.Popen("/var/tmp/runme.sh", shell=False, stdin=subprocess.PIPE,  stdout=subprocess.PIPE)

runme.sh:

#!/bin/bash
export DISPLAY=:0.0 
vlc /var/tmp/Terminator3.Movie.mp4
  • I think vlc does run independent of the terminal by default so I'm not sure if that would apply with other commands. – konsolebox Sep 18 '13 at 02:24