I have a bash script which is runnnig another script. What i want is something like this :
#in scriptA.sh
./scriptB
wait 10 seconds and kill scriptB
Is it doable ?
Thanks.
./scriptB &
sleep 10s
kill $!
How it works
./scriptB &
This starts scriptB in the background.
sleep 10s
This waits 10 seconds
kill $!
This kills the most recently executed background process.
kill will kill any process given is process ID. $! is the process ID of the most recently executed background process.
Related
I have two programs in two files that I run with bash:
The first:
(sleep 100) &
wait
The second:
sleep 100 &
wait
If I send a SIGINT to the first program, it also kills my sleep command. But for the second the sleep command remains and isn't killed.
Why the difference?
Thanks so much!
How do I repeatedly start and kill a bash script that takes a long time. I have a analyze_realtime.sh that runs indefinitely, but I only want to run it for X second bursts (just say 15s for now).
while true; do analyze_realtime.sh; sleep 15; done
The problem with this is that analyze_realtime.sh never finishes, so this logic doesn't work. Is there a way to kill the process after 15 seconds, then start it again?
I was thinking something with analyze_realtime.sh&, ps, and kill may work. Is there anything simpler?
while true;
do
analyze_realtime.sh &
jobpid=$! # This gets the pid of the bg job
sleep 15
kill $jobpid
if ps -p $jobpid &>/dev/null; then
echo "$jobpid didn't get killed. Moving on..."
fi
done
You can do more under the if-statement, sending other SIGNALs if SIGHUP didn't work.
Try this out
while true; do
analyze_realtime.sh & # put script execution in background
sleep 15
kill %1
done
Explanation
%1 refer to the latest process ran in background
You can use timeout utility from coreutils:
while true; do
timeout 15 analyze_realtime.sh
done
(Inspired by this answer)
Is there any built in feature in bash to wait for 1 out of many processes to finish? And then kill remaining processes?
pids=""
# Run five concurrent processes
for i in {1..5}; do
( longprocess ) &
# store PID of process
pids+=" $!"
done
if [ "one of them finished" ]; then
kill_rest_of_them;
fi
I'm looking for "one of them finished" command. Is there any?
bash 4.3 added a -n flag to the built-in wait command, which causes the script to wait for the next child to complete. The -p option to jobs also means you don't need to store the list of pids, as long as there aren't any background jobs that you don't want to wait on.
# Run five concurrent processes
for i in {1..5}; do
( longprocess ) &
done
wait -n
kill $(jobs -p)
Note that if there is another background job other than the 5 long processes that completes first, wait -n will exit when it completes. That would also mean you would still want to save the list of process ids to kill, rather than killing whatever jobs -p returns.
It's actually fairly easy:
#!/bin/bash
set -o monitor
killAll()
{
# code to kill all child processes
}
# call function to kill all children on SIGCHLD from the first one
trap killAll SIGCHLD
# start your child processes here
# now wait for them to finish
wait
You just have to be really careful in your script to use only bash built-in commands. You can't start any utilities that run as a separate process after you issue the trap command - any child process exiting will send SIGCHLD - and you can't tell where it came from.
I am trying to write a script that displays an image for a time, then returns back to the shell script. I tried:
feh outfile.jpeg | sleep 10 | exit
but this doesn't work. Im not interested in exiting the script, only in getting back to the other functions of the script.
I just want to show the image for a short time, then return to what the script is doing.
How might I achieve this?
You could do:
feh outfile.jpeg & # run process in background
pid=$! # obtain PID of last backgrounded process
sleep 10
kill $pid # kill feh
Here is a working solution:
(feh outfile.jpeg&) && (sleep 10 && pkill feh)
It will run feh in backgroud and then count to 10 before killing feh. A better way would be to kill the right pid though...
In my bash script I am writing, I am trying to start a process (sleep) in the background and then suspend it. Finally, the process with be finished. For some reason through, when I send the kill command with the stop signal, it just keeps running as if it received nothing. I can do this from the command line, but the bash script is not working as intended.
sleep 15&
pid=$!
kill -s STOP $pid
jobs
kill -s CONT $pid
You can make it work by enabling 'monitor mode' in your script: set -m
Please see why-cant-i-use-job-control-in-a-bash-script for further information