0

Now I have a udp_server.py to run.

According to my less experience, I will do a python script in crontab as bellow.

import commands
output = commands.getoutput("ps aux | grep udp_server.py")
'''print output 
user      8695  0.0  0.3  50948 14196 pts/0    S    10:21   0:00 python udp_server.py
user      9486  0.0  0.0   4400   584 pts/0    S+   10:34   0:00 sh -c {  ps aux | grep 'udp_server.py'; } 2>&1
user      9488  0.0  0.0   9384   888 pts/0    R+   10:34   0:00 grep udp_server.py
'''
if 'python udp_server.py' in output:
    print "The server is active."

is this right?

5
  • 1
    What is "right" for you? Does running udp_server.py print that kind of output? Are you trying to run the pgrep command to check if the process is running already? Is this being run within udp_server.py to see if it's already running from the cron job and as such would have to ignore itself in the list? Commented Sep 9, 2015 at 3:04
  • Also, you might want to check this question: Process list on Linux via Python Commented Sep 9, 2015 at 3:08
  • sorry, I type error command. Commented Sep 9, 2015 at 5:41
  • Do you mean to control a processes ? Commented Sep 9, 2015 at 5:54
  • I just want to know the process whether it is existent or not. Commented Sep 9, 2015 at 7:05

1 Answer 1

1

If you want to type command line as like typing in unix console, it's right.

Suppose you will package the code in a file, maybe you will encounter a problem: it can't excute command. It's because your account in linux hasn't enough authority, I suggest you do as the following:

sudo python command_file.py

By the way, if you really work in python2.6+, please notice the api, deprecated since version 2.6: The commands module has been removed in Python 3. Use the subprocess module instead. So I suggest you replace it form subprocess. Reference hypelink: commands

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

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.