0

I want to execute a command and then log its output to a log file using logging.info

I am currently using

cmd = """var=$(cat ip.txt | head -1 | sed 's/[^|]//g' | awk '{ print length }')"""
logging.info(cmd)
process = os.popen(cmd)
processClose = process.close()


logging.info($var)

but it gives me an error as $ is an invalid syntax.

I want the output in the log file to print the value of the variable (var)

2 Answers 2

1

The below method of execution works with logging.info

success = sp.call(cmd, stdout=open('temp_log', 'w'), stderr=open('temp_err_log', 'w'), shell = True)
outText = open('temp_log').readlines()
outText = ''.join(outText)
logging.info('Dump stderr:\n%s'%(outText))
Sign up to request clarification or add additional context in comments.

Comments

0

$var is invalid in your code. It is invalid because the process space in which is was created is gone.

Try this:

    import subprocess
    cmd = "cat ip.txt | head -1 | sed 's/[^|]//g' | awk '{ print length }'"
    p = subprocess.Popen([cmd,], stdout=subprocess.PIPE, shell=True)
    p.stdout.readline()

Hope this helps.

1 Comment

problem is not with the execution of the script. I am not able to log the output in a file. cmd = """cat ip.txt | head -1 | sed 's/[^|]//g' | awk '{ print length }'""" logging.info(cmd) process = os.popen(cmd) processClose = process.close() This works perfectly. I just want to use logging.info to see the output in a log file.

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.