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?

Burger King
  • 2,945
  • 3
  • 20
  • 45
  • 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? – Eric Renouf Sep 09 '15 at 03:04
  • Also, you might want to check this question: [Process list on Linux via Python](http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python) – Eric Renouf Sep 09 '15 at 03:08
  • sorry, I type error command. – Burger King Sep 09 '15 at 05:41
  • Do you mean to `control a processes` ? – luoluo Sep 09 '15 at 05:54
  • I just want to know the process whether it is existent or not. – Burger King Sep 09 '15 at 07:05

1 Answers1

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