2

I am trying to run the following awk command inside python but I get a syntax error related to the quotes:

import subprocess
COMMAND = "df /dev/sda1 | awk /'NR==2 {sub("%","",$5); if ($5 >= 80) {printf "Warning! Space usage is %d%%", $5}}"

subprocess.call(COMMAND, shell=True)

I tried to escape the quotes but I am still getting the same error.

Ma0
  • 15,057
  • 4
  • 35
  • 65
user1919
  • 3,818
  • 17
  • 62
  • 97
  • Possible duplicate of [Python subprocess: pipe an image blob to imagemagick shell command](https://stackoverflow.com/questions/30105904/python-subprocess-pipe-an-image-blob-to-imagemagick-shell-command) – Maurice Meyer Dec 04 '17 at 09:22
  • Proper escaping would be like this: `COMMAND = "df /dev/sda1 | awk 'NR==2 {sub(\"%\",\"\",$5); if ($5 >= 80) {printf \"Warning! Space usage is %d%%\", $5}}'"` – yolenoyer Dec 04 '17 at 09:24
  • You have a unclosed quote. I suggest you running it in your terminal before you actually do it in python. – danieltan95 Dec 04 '17 at 09:25
  • Use triple quotes on the `COMMAND` string. But why are you doing it like this? Just use awk, or just use Python. What's the point of using both of them? – PM 2Ring Dec 04 '17 at 09:26

3 Answers3

4

You may want to put ''' or """ around the string since you have both ' and ".

import subprocess
COMMAND = '''"df /dev/sda1 | awk /'NR==2 {sub("%","",$5); if ($5 >= 80) {printf "Warning! Space usage is %d%%", $5}}"'''

subprocess.call(COMMAND, shell=True)

There also seems to be a relevant answer already for this as well: awk commands within python script

2

Try this:

import subprocess
COMMAND="df /dev/sda1 | awk 'NR==2 {sub(\"%\",\"\",$5); if ($5 >= 80) {printf \"Warning! Space usage is %d%%\", $5}}'"

subprocess.Popen(COMMAND,stdin=subprocess.PIPE,stdout=subprocess.PIPE, shell=True).stdout.read()
Yoni Rabinovitch
  • 5,171
  • 1
  • 23
  • 34
2

I was writing a python script for my deployment purpose and one part of the script was to explicitely kill the process if its not stopped successfully.

Below is the python code which actually performs Find the processId of the process named myApplication ps -ef | grep myApplication | grep -v grep | awk {'print $2'} and then perform kill -9 PID //where PID is output of earlier command

  import subprocess
  import signal

  def killApplicationProcessIfStillRunning(app_name):
      p1 = subprocess.Popen(['ps', '-ef'], stdout=subprocess.PIPE)
      p2 = subprocess.Popen(['grep', app_name],stdin=p1.stdout, stdout=subprocess.PIPE)
      p3 = subprocess.Popen(['grep', '-v' , 'grep'],stdin=p2.stdout, stdout=subprocess.PIPE)
      p4 = subprocess.Popen(['awk', '{print $2}'],stdin=p3.stdout, stdout=subprocess.PIPE)
      out, err = p4.communicate()
      if out:
          print 'Attempting to kill '+app_name +' process with PID ' +out.splitlines()[0]
          os.kill(int(out.splitlines()[0]),signal.SIGKILL)

Now invoke the above method as

killApplicationProcessIfStillRunning(myApplication)

Hope it helps someone.

alper
  • 2,919
  • 9
  • 53
  • 102
Sanjay Bharwani
  • 3,317
  • 34
  • 31