bash: silently kill background function process
kill $foo_pid
wait $foo_pid 2>/dev/null
BTW, I don't know about your massively cool progress bar, but have you seen Pipe Viewer (pv)? http://www.ivarch.com/programs/pv.shtml
Start a process in background, do a task, then kill the process in the background
You can probably save the PID of the process in a variable, then use the kill command to kill it.
pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
serverPID=$!
cd web/code/protected/tests/
phpunit functional/
kill $serverPID
popd
I haven't tested it myself, I'd like to write it on a comment, but not enough reputation yet :)
How can I silence the Terminated message when my command is killed by timeout?
There's nothing wrong with your code, that "Terminated" message doesn't come from your script but from the invoking shell (the one you launch your script
from).
You can deactivate if by disabling job control:
$ set +m
$ bash <your timeout script>
How to suppress Terminated message after killing in bash?
The short answer is that you can't. Bash always prints the status of foreground jobs. The monitoring flag only applies for background jobs, and only for interactive shells, not scripts.
see notify_of_job_status() in jobs.c.
As you say, you can redirect so standard error is pointing to /dev/null but then you miss any other error messages. You can make it temporary by doing the redirection in a subshell which runs the script. This leaves the original environment alone.
(script 2> /dev/null)
which will lose all error messages, but just from that script, not from anything else run in that shell.
You can save and restore standard error, by redirecting a new filedescriptor to point there:
exec 3>&2 # 3 is now a copy of 2
exec 2> /dev/null # 2 now points to /dev/null
script # run script with redirected stderr
exec 2>&3 # restore stderr to saved
exec 3>&- # close saved version
But I wouldn't recommend this -- the only upside from the first one is that it saves a sub-shell invocation, while being more complicated and, possibly even altering the behavior of the script, if the script alters file descriptors.
EDIT:
For more appropriate answer check answer given by Mark Edgar
How to shield the kill output
The message isn't coming from either kill
or the background command, it's coming from bash when it discovers that one of its background jobs has been killed. To avoid the message, use disown
to remove it from bash's job control:
sleep 20 &
PID=$!
disown $PID
kill -9 $PID
Is there a way to make bash job control quiet?
You can use parentheses to run a background command in a subshell, and that will silence the job control messages. For example:
(sleep 10 & )
How do I terminate all the subshell processes?
Here's a simpler solution -- just add the following line at the top of your script:
trap "kill 0" SIGINT
Killing 0
sends the signal to all processes in the current process group.
Related Topics
File Names with Spaces in Bash
Restarting Cron After Changing Crontab File
Creating Executable Files in Linux
What Is the Default Register State When Program Launches (Asm, Linux)
Why Does Printf Overwrite the Ecx Register
Linux: Where Are Environment Variables Stored
What Is Global _Start in Assembly Language
How to Undo Strip - I.E. Add Symbols Back to Stripped Binary
Write a Bash Shell Script That Consumes a Constant Amount of Ram for a User Defined Time
Return Code When Oom Killer Kills a Process
How to Append One File to Another in Linux from the Shell
What Linux Shell Command Returns a Part of a String
How to Upgrade Aws Cli to the Latest Version
Using Mono-Service to Wrap a Windows Service on Linux
What Is an Anonymous Inode in Linux
Does Gcc Have Any Options to Add Version Info in Elf Binary File