user285594
user285594

Reputation:

How can i let the BASH script run as process? So that even the Python script is killed the BASH script runs forever?

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.

Upvotes: 0

Views: 340

Answers (2)

user285594
user285594

Reputation:

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

Upvotes: 0

konsolebox
konsolebox

Reputation: 75488

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")

Upvotes: 1

Related Questions