Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I silence the "Terminated" message when my command is killed by timeout?

Tags:

bash

shell

By referencing bash: silently kill background function process and Timeout a command in bash without unnecessary delay, I wrote my own script to set a timeout for a command, as well as silencing the kill message.

But I still am getting a "Terminated" message when my process gets killed. What's wrong with my code?

#!/bin/bash
silent_kill() {
    kill $1 2>/dev/null
    wait $1 2>/dev/null
}
timeout() {
    limit=$1 #timeout limit
    shift
    command=$* #command to run
    interval=1 #default interval between checks if the process is still alive
    delay=1 #default delay between SIGTERM and SIGKILL
    (
        ((t = limit))
        while ((t > 0)); do
            sleep $interval;
            #kill -0 $$ || exit 0
            ((t -= interval))
        done
        silent_kill $$
        #kill -s SIGTERM $$ && kill -0 $$ || exit 0 
        sleep $delay
        #kill -s SIGKILL $$
    ) &> /dev/null &
    exec $*
}
timeout 1 sleep 10
like image 820
ggaaooppeenngg Avatar asked Sep 13 '15 16:09

ggaaooppeenngg


People also ask

How do you silent a bash script?

To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.

What is timeout in shell script?

If no signal is given, timeout sends the SIGTERM signal to the managed command when the time limit is reached. You can specify which signal to send using the -s ( --signal ) option. For example, to send SIGKILL to the ping command after one minute, you would use: sudo timeout -s SIGKILL ping 8.8.8.8.


1 Answers

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>
like image 81
JB. Avatar answered Oct 15 '22 08:10

JB.