8

I need to make a script that gets from the user the following:

1) Process name (on linux).

2) The log file name that this process write to it.

It needs to kill the process and verify that the process is down. Change the log file name to a new file name with the time and date. And then run the process again, verify that it's up in order it will continue to write to the log file.

Thanks in advance for the help.

Bar Aviv
  • 83
  • 1
  • 1
  • 4
  • Process names aren't unique on linux. What if there's more than one process with the same name? Also, how does the system know how to restart the process? You seem to be duplicating the logrotate functionality.... – Spacedman Nov 18 '10 at 12:38
  • How can you be sure the process killed is actually writing to that log file? Sounds dangerous to assume the user won't make mistake. Are those mistake creating critical unwanted situations? – Danosaure Nov 18 '10 at 13:34
  • Although that is true about subprocess, for really simple commands where the user does not need any of the extra complexity of subprocesses using os.system is actually easier to both code and understand. For my work I have used os.system without any problems. – Zachary Kraus Sep 30 '12 at 07:23

2 Answers2

21

You can retrieve the process id (PID) given it name using pgrep command like this:

import subprocess
import signal
import os
from datetime import datetime as dt


process_name = sys.argv[1]
log_file_name = sys.argv[2]


proc = subprocess.Popen(["pgrep", process_name], stdout=subprocess.PIPE) 

# Kill process.
for pid in proc.stdout:
    os.kill(int(pid), signal.SIGTERM)
    # Check if the process that we killed is alive.
    try: 
       os.kill(int(pid), 0)
       raise Exception("""wasn't able to kill the process 
                          HINT:use signal.SIGKILL or signal.SIGABORT""")
    except OSError as ex:
       continue

# Save old logging file and create a new one.
os.system("cp {0} '{0}-dup-{1}'".format(log_file_name, dt.now()))

# Empty the logging file.
with open(log_file_name, "w") as f:
    pass

# Run the process again.
os.sytsem("<command to run the process>") 
# you can use os.exec* if you want to replace this process with the new one which i think is much better in this case.

# the os.system() or os.exec* call will failed if something go wrong like this you can check if the process is runninh again.

Hope this can help

Ryaner
  • 751
  • 6
  • 16
mouad
  • 67,571
  • 18
  • 114
  • 106
2

If you know how to do it in the terminal, then you could use the following:

import os
os.system("your_command_here; second_command; third; etc")

So that you end up having sort of a mini shell script inside python. I would also consider making this shell script exist on its own and then call it from python:

import os
os.system("path/to/my_script.sh")

Cheers!

Morlock
  • 6,880
  • 16
  • 43
  • 50
  • I like the simplicity, and have one thing to add 'os.system' returns the exit status code so you can check for the success of the process and handle errors by code. – snarkyname77 Nov 18 '10 at 13:38
  • Generally, programmers are discouraged from using os.system any more, since subprocess.Popen gives many more options and support. In particular, it handles command-line arguments much better and allows piping output. – James Broadhead Nov 18 '10 at 14:37
  • The non-depreciated version of `os.system("myScript")` is `subprocess.run("myScript", shell=True)`. If you don't set shell=True, you'll need to give a list of strings instead of a string and put each new entity in its own string (a reason for doing a list is so you can put multiple paths in without having to do a bunch of fancy stuff to escape spaces and such in each path in your arguments). – Brōtsyorfuzthrāx Nov 30 '22 at 11:57