Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4340922
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:24:54+00:00 2026-05-21T11:24:54+00:00

I am trying to write a code in python that will take some information

  • 0

I am trying to write a code in python that will take some information from top and put it into a file.
I want to just write the name of the application and generate the file. The problem i am having is that i can’t get the output of the pidof command so i can use it in python. My code looks like this :

import os

a = input('Name of the application')
val=os.system('pidof ' + str(a)) 
os.system('top -d 30 | grep' + str(val) + '> test.txt')
os.system('awk '{print $10, $11}' test.txt > test2.txt')

The problem is that val always has 0 but the command is returning the pid i want. Any input would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:24:54+00:00Added an answer on May 21, 2026 at 11:24 am

    First up, the use of input() is discouraged as it expects the user to type in valid Python expressions. Use raw_input() instead:

    app = raw_input('Name of the application: ')
    

    Next up, the return value from system('pidof') isn’t the PID, it’s the exit code from the pidof command, i.e. zero on success, non-zero on failure. You want to capture the output of pidof.

    import subprocess
    
    # Python 2.7 only
    pid = int(subprocess.check_output(['pidof', app]))
    
    # Python 2.4+
    pid = int(subprocess.Popen(['pidof', app], stdout=subprocess.PIPE).communicate()[0])
    
    # Older (deprecated)
    pid = int(os.popen('pidof ' + app).read())
    

    The next line is missing a space after the grep and would have resulted in a command like grep1234. Using the string formatting operator % will make this a little easier to spot:

    os.system('top -d 30 | grep %d > test.txt' % (pid))
    

    The third line is badly quoted and should have caused a syntax error. Watch out for the single quotes inside of single quotes.

    os.system("awk '{print $10, $11}' test.txt > test2.txt")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some Python code that will establish an invisible relay between
I'm trying to write some C# code that calls a method from an unmanaged
I'm trying to write some code to work with an htdigest password file. The
I was trying to write some code that would check if an item has
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed
I have a mobile platform that I am trying to write some communications code
I am trying to write a function that will take an xml object, an
I am trying to write a Python script that will access and modify the
I'm trying to create a python server that will serve calls from outer source
I was trying to write code that would auto-close a Toplevel Tk window in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.