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
Related
In my current application, I have a Python 2.7 script called main.py that launches another Python 2.7 script called calculator.py using GNU Parallel like the following:
os.system("seq 10000 | parallel -N0 -j 50 nohup python calculator.py &")
print "Done"
This works pretty well, with one exception: I need to resume executing other commands in main.py (that is, after the os.system call, e.g. the print "Done" line) just after all the 10000 instances spawned with GNU Parallel finish running.
Is there a proper way to do that? Solutions with os.spawn and Python 2.7 subprocess are both welcome, but using GNU Parallel is absolutely mandatory.
EDIT: Here are my requirements:
1) it is crucial to me that the many instances of calculator.py that are spawned keep running if the terminal closes (hence the nohup)
2) I need it to not block current terminal session (hence the &)
3) I need it to print "Done" in the example above gets executed only after the 10000 jobs finish
If achieving all above at the same time is not possible, I think I could then manually keep a log of all launched processes and then manually force the rest of the code "main.py" code to continue after all those processes end. This, of course, is a cumbersome last-resource option.
This question already has answers here:
How to terminate a python subprocess launched with shell=True
(11 answers)
Closed 7 years ago.
I have two subprocesses as I showed below
cmd1='arecord -d 0 -f cd -t wav test.wav'
cmd2='raspivid -o video.h264 -fps 25 -t 0'
pro1 = subprocess.Popen(cmd1, shell=True)
pro2 = subprocess.Popen(cmd2, shell=True)
I want to record audio and video at the same time with raspberry pi. From now on I can start both programs but I cannot stop these programs. Can anybody help me?
Or any idea for doing by simple way this process?
From the docs:
Popen.terminate() Stop the child. On Posix OSs the method sends
SIGTERM to the child. On Windows the Win32 API function
TerminateProcess() is called to stop the child.
New in version 2.6.
Popen.kill() Kills the child. On Posix OSs the function sends SIGKILL
to the child. On Windows kill() is an alias for terminate().
The documentation for Python's subprocess module includes a description of Popen.terminate() and Popen.kill().
I need to make sure to run two processes (python scripts) almost at the same time. But I want the program to continue until one of them is finished. I am running these processes from a C++ program using system.
Is this the right way to run script1 and script2 at the same time and continue just after script2 is finished?
python ./script1.py & python ./script2.py
Thank you!
Your snippet won't work because it will continue as soon as script2 finishes. script1 may still be working at the background.
If you are using bash shell you can do the following:
python ./script1.py &
PID1=$!
python ./script2.py
wait $PID1
$! has the process id of the previously background command. So we run script1 in the background, then we run script2 until completion, and then we wait for script1 to finish (if not already finished).
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I determine if a python script is executed from crontab?
Is there a way to determine if a python script was started by cron or not?
Not per se, but you could set an environment variable in the crontab and check it in the script.
* * * * * CRONRUN=y /srv/cron/foo.py
...
if 'CRONRUN' in os.environ:
...
Without knowing what your script does, I can give a couple of suggestions.
If the script runs then terminates, have the script log its start and end time in a log file.
If the script is a process, or runs as a daemon, and you are using cron to start the script on a reboot (depending on the cron daemon you are using), you can use the program ps to view a snapshot of the current processes.