How to exit from bash script if it started with timeout? - linux

I run my scripts with timeout function
timeout 86400 ./
How can I terminate script before timeout counted down?
Ctr+C does not work in this case
Closing terminal still leaves process on background
This is my full script:
python3 5 2 10
timeout 86400 ./
sleep 4
python3 10 4 20
timeout 86400 ./
sleep 4
python3 20 4 40
timeout 86400 ./
sleep 4
python3 30 8 50
timeout 86400 ./
sleep 4
python3 50 9 70
exit 0
I would like to exit from my bash script before it finish without using ps -A all the time.

find a list of processes by name using grep
pgrep timeout
kill all processes by name
pkill timeout

The below will kill your script directly
pkill -f ""


Stop Bash Script after a Particular Interval [duplicate]

This question already has an answer here:
Timeout a command in bash without unnecessary delay
22 answers
ls -l /bin/bash
ls -l /sbin/bash
ls -l /usr/local/bin/bash
ls -l /usr/bin/bash
ls -l /usr/sbin/bash
ls -l /usr/local/sbin/bash
After running this script I want to stop this script If time is more than 60 seconds.
Assume 1st commands execute in 10 and second commands take 50 seconds after that I have to stop the execution of the script.
Use timeout(1)
timeout 60 ./
Or kill it afterwards:
./ & :
sleep 60 && kill $!

Tmeout in Linux in order to run a command for a certain time

I am trying to run this command for 1 second:
# rsync using variables
epoch=$(date +%s)
timeout 5 echo $id",Danny_"$id","$id","$epoch","$u_id >> lfs.csv
It doesn't seem to run 5 seconds, so I tried to run it before execution of my script:
timeout 5 Which did not do the trick as well.
timeout command is limiting the command that you are running for the specific amount of time that you set.
in your case the command is finishing way before 5 seconds that you set.
[aa#vm007 ~]$ timeout 5 find / 1>/dev/null 2>/dev/null <-- finished after 5 seconds
[aa#vm007 ~]$ time timeout 5 find / 1>/dev/null 2>/dev/null <-- actual time that it takes to run with timeout command
and the output:
real 0m5.016s
user 0m0.008s
sys 0m0.037s

Linux - how does the kill -k switch work in timeout command

I have two one liners:
In first. I've expected killing sh -c "..." command after 5 seconds but it exists until the timeout exits (for 10 seconds)
timeout -k 5s 10s sh -c 'sleep 20s && echo "Long running command which is visible under: ps -elf | grep sleep during whole life (10s) time of timeout command"'
In second. I've expected that timeout will exit with return code 124 (because the sh -c "..." command is still running) while the command sh -c "..." will continue to run (because of kill option for timeout was not set)
timeout 10s sh -c 'sleep 20s && echo "Long running command which is visible under: ps -elf | grep sleep during whole life (10s) time of timeout command"'
It seems that argument passed to timeout runs for exact time as timeout command itself (it is not killed earlier nor survive timeout) what is the purpose of kill option then?
The option -k is to send KILL signal after the specified seconds if the process couldn't be terminated after the timeout.
timeout first sends the TERM signal. If -k is specified, then it'll also send KILL signal, following the real timeout value.
For example
timeout -k 5 10 someCommand
timeout sends TERM signal after the 10 seconds. If someCommand didn't respond to TERM (e.g. it could block the TERM signal) then timeout sends KILL signal after 5 more seconds (i.e. at the 15th second since the start of execution). The signal KILL can't be blocked.

Why does timeout not work within a bash script?

I tried to kill a process if it exceeds more than a few seconds.
The following works just fine when I run it in the terminal.
timeout 2 sleep 5
But when I have a script -
timeout 2 sleep 5
it says
timeout: command not found
Why so? What is the workaround?
On executing type timeout, it says -
timeout is a shell function
It's seems your environment $PATH variable does not include /usr/bin/ path or may be timeout binary exists in somewhere else.
So just check path of timeout command using :
command -v timeout
and use absolute path in your script
/usr/bin/timeout 2 sleep 5
Update 1#
As per your update in question, it is function created in your shell. you can use absolute path in your script as mentioned in above example.
Update 2#
timeout command added from coreutils version => 8.12.197-032bb, If GNU timeout is not available you can use expect (Mac OS X, BSD, ... do not usually have GNU tools and utilities by default).
# Executes command with a timeout
# Params:
# $1 timeout in seconds
# $2 command
# Returns 1 if timed out 0 otherwise
timeout() {
# start the command in a subshell to avoid problem with pipes
# (spawn accepts one command)
command="/bin/sh -c \"$2\""
expect -c "set echo \"-noecho\"; set timeout $time; spawn -noecho $command; expect timeout { exit 1 } eof { exit 0 }"
if [ $? = 1 ] ; then
echo "Timeout after ${time} seconds"
timeout 10 "ls ${HOME}"

LInux replace sleep calls with process status check

Linux bash commend:
I have .sh scripts as follows which has couple of sleep calls, is there a way i can replace sleep calls b/c sleep time may not be accurate, i want to check process running time and continue once previous process finish.
sleep 60
sleep 60
./ stop
Help appreciated.
-- Find the PID
$ pgrep gcalctool
wait un-child process to be wait
while [ -e /proc/15435 ]; do sleep 0.1; done