Reputation: 35
So I'm running a python script: It starts a logging program in the background Does a bunch of stuff. Then stops the logging program.
So I have two questions: 1) I understand to run a background program, I could call: os.system("test_log_prog &") but can I also do: os.system("test_echo_prog > logfile.log &") or something equivalent?
2) How can I close test_echo_prog? for the program, I was using pkill "test_log_prog" but for some reason it doesn't work when using > logfile.log.....
Thanks in advance!
Cheers!
Upvotes: 1
Views: 128
Reputation: 7177
You can use Python's os.kill to kill a process by its pid, sending a mostly-arbitrary signal to it like SIGTERM or SIGINT. If it won't die, try SIGKILL.
You can look up a process' pid using the pidof program, or if you use subprocess.Popen you can get the pid from the popen object without needing to spawn another subprogram. os.system is no longer in great favor, at least not compared to subprocess.Popen.
Upvotes: 3