This question already has answers here:
Find and kill a process in one line using bash and regex
(30 answers)
Closed 9 years ago.
How to kill a running python in shell script when we know the python file name xxx.py?
(it is executed by cmd python xxx.py)
I can use ps aux | grep python to get the pid of it. and then
kill pid to terminate it.
but every time, I have to execute two cmd. Is there a good way to do it?
The pkill utility can look at command lines when sending signals:
pkill -f xxx.py
Related
This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
Closed 2 years ago.
I have lots of python scripts to be run and I want to automate them. All of them accept inputs at certain times (3 in this case). I have tried something like this but since echo does not have an EOF it did not work:
os.system("echo 4 | echo 5 | echo 6 | python script.py")
I cannot change the content of script.py and it does not accept arguments to it.
How can I automatically input using a line(s) of python code? Thanks.
Check out Pexpect:
Pexpect is a pure Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect works like Don Libes’ Expect. Pexpect allows your script to
spawn a child application and control it as if a human were typing
commands.
This question already has answers here:
Waiting for background processes to finish before exiting script
(5 answers)
Closed 3 years ago.
I want to write a shell script which can start two python scripts parallelly. Other commands in shell script will run after the two parallel python processes are done.
In conclusion, two key-points:
run the two python processes parallelly;
run other commands after the python processes are done.
How should I do that?
declare -a pids
launch_first_program &
pids+=($!)
launch_second_program &
pids+=($!)
wait "${pids[#]}"
continue_with_our_day
After a process is launched in background with &, the variable $! will contain the child's PID. We can collect it into an array variable, then use wait to block till all of the listed processes are done.
You can use wait in bash to wait for background processes to finish
python script1.py &
python script2.py &
wait
Other commands
This question already has answers here:
Interacting with program after execution
(3 answers)
Closed 7 years ago.
I want a script to run an then finish on the python shell with all variables and methods:
$ python myprogram.py
...
program output
...
>>>
And with #!/usr/bin/python is posible? so I double-click and it just works?
Sounds like you want Python's i flag. From the help menu:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So the full command would be
python -i yourscriptname.py
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Run a linux system command as a superuser, using a python script
(6 answers)
Closed 9 years ago.
I wanna to run my own non-system external commands in python.
Such as "sudo insteon on 23". Subprocess and os.system are designed for system calls.
Does anybody know how to do it?
Thanks
You can use subprocess.Popen for this:
import shlex
import subprocess
proc = subprocess.Popen(shlex.split('sudo insteon on 23'))
proc.communicate()
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Python subprocess
child subprocess kill in python daemon
How can I run commands in the windows command line through python?
I'm trying to have it so that it starts a subprocess, I don't care about the output, but I want to be able to kill it at any time.
subprocess.Popen: http://docs.python.org/library/subprocess.html