Start script after another one (already running) finishes
Polling is probably the way to go, but it doesn't have to be horrible.
pid=$(ps -opid= -C your_script_name)
while [ -d /proc/$pid ] ; do
sleep 1
done && ./your_other_script
Linux: start a script after another has finished
Perhaps you didn't notice that the scripts you found were written for bash, not csh, but
you're trying to process them with the csh interpreter.
It looks like you've misunderstood what the original code was trying to do -- it was
intended to monitor an already-existing process, by looking up its process id using the process name.
You seem to be trying to start the first process from inside the ps
command. But
in that case, there's no need for you to do anything so complicated -- all you need
is:
#!/bin/csh
csh testscript1
csh testscript2
Unless you go out of your way to run one of the scripts in the background,
the second script will not run until the first script is finished.
Although this has nothing to do with your problem, csh is more oriented toward
interactive use; for script writing, it's considered a poor choice, so you might be
better off learning bash instead.
Run python script after another script ends
The first thing I thought of is running both Python programs from within a shell script so the 2nd one simply starts when the first one ends:
#!/bin/bash
python -m script1.py
python -m script2.py
If the first script is already running, or started outside of your control, you could use the ps command in a loop to detect when it finishes, and then launch the second the 2nd script:
#!/bin/bash
while [ 1 ]
do
cnt=`ps x | grep -v grep | grep -c script1.py`
if [[ "$cnt" -eq 0 ]]
then
break
fi
sleep 1
done
echo "Starting second program"
python -m script2.py
Tip: when using this approach, remember to use grep -v grep
before the grep that looks for the process you want, or your grep command will count itself in the results.
(Please be gentle; this is my first attempt at answering a question here.)
How to spawn n processes and start another after one finishes using bash?
wait -n `jobs -p`
Seems to do the job. Thanks for the guidance l'L'l and hek2mgl.
Bash script running program lines one after another
I think Bash is trying to eat some of your Python options. To fix that, wrap each line in quotes and put eval
at the front:
#!/bin/bash
eval 'python /path/to/someprogramm.py analyze --probes /path/to/myfile.bed --rpkm_dir /path/to/RPKM/ --output /path/to/hdf5/analysis.hdf5 --write_svals /path/to/SVD/singular_values.txt'
eval 'python /path/to/someprogramm.py call --input /path/to/hdf5/analysis.hdf5 --output /path/to/calls.txt'
eval 'python /path/to/someprogramm.py plot --input /path/to/hdf5/analysis.hdf5 --calls /path/to/calls.ng.txt --outputdir /path/to/call_images/'
echo 'Command sequence finished succesfully'
Run a script in parallel until another one finishes
You can use this loop:
#!/bin/bash
python A.py &
while [[ $(jobs -pr) ]]; do
python B.py
done
jobs -pr
lists the process IDs (-p
) of running jobs (-r
). If the output is empty, the backgrounds command has finished.
Run a job after another
One approach would be to see what the process ID of a.out
is, withtop
or ps
, and have a launcher program that checks once a second to
see if that PID is still active. When it isn't, the launcher runsb.out
.
Related Topics
Term Environment Variable Not Set
Replace Whitespace with a Comma in a Text File in Linux
How to Set a Custom Baud Rate on Linux
Best Way to Script Remote Ssh Commands in Batch (Windows)
Best Way to Get MAChine Id on Linux
How to Make Mv Create the Directory to Be Moved to If It Doesn't Exist
Tar Archiving That Takes Input from a List of Files
How to Create a Configure Script
Why Doesn't the Cd Command Work in My Shell Program
Bash: Set Array Env Variable and De-Referencing It from Any Shell Script Fails
Can Docker Solve a Problem of Mismatched C Shared Libraries
Delete All Files Older Than 30 Days, Based on File Name as Date
Linux/Unix Environment Variables
How to Add a Line of Text to the Middle of a File Using Bash
How to Hide the Mouse Pointer Under Linux/X11
Linux Capabilities (Setcap) Seems to Disable Ld_Library_Path