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.

4
  • Possible duplicate of Python subprocess: pipe an image blob to imagemagick shell command Commented Dec 4, 2017 at 9: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}}'" Commented Dec 4, 2017 at 9:24
  • You have a unclosed quote. I suggest you running it in your terminal before you actually do it in python. Commented Dec 4, 2017 at 9: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? Commented Dec 4, 2017 at 9:26

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

In that case I get a: No such file or directory
That could be the output for the df command. df should be used on filesystems and not on disks.
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()

1 Comment

shell=True is not recommended
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.