This question already has answers here:
Bash Run command for certain time?
(5 answers)
Timeout a command in bash without unnecessary delay
(24 answers)
Closed 5 years ago.
I need to execute bash command that may take a long time, but I need to kill it if this time exceeds 5 minutes for example!
Any ideas?
You can use timeout command. For example
timeout 300 some_cmd
will kill some_cmd if it's still running after 300 seconds.
Related
This question already has answers here:
Timeout a command in bash without unnecessary delay
(24 answers)
Closed 1 year ago.
In my bash script I run a command that activates a script. I repeat this command many times in a for loop and as such want to wait until the script is finished before running it again. My bash script is as follows
for k in $(seq 1 5)
do
sed_param='s/mu = .*/mu = '${mu}';/'
sed -i "$sed_param" brusselator.c
make brusselator.tst &
done
As far as I know the & at the end lets the script know to wait until the command is finished, but this isn't working. Is there some other way?
Furthermore, sometimes the command can take very very long, in this case I would maximally want to wait 5 seconds. But if the command is done earlier I would't want to wait 5 seconds. Is there some way to achieve this?
There is the timeout command. You would use it like
timeout -k 5 make brusselator.tst
Maybe you would like to see also if it exited successfully, failed or was killed because it timed out.
timeout -k 5 make brusselator.tst && echo OK || echo Failed, status $?
If the command times out, and --preserve-status is not set, then command exits with status 124. Different status would mean that make failed for different reason before timing out.
This question already has answers here:
Bash sleep in milliseconds
(3 answers)
Closed 2 years ago.
I am writing a bash script which simply needs to sleep for less than a second.
However 'sleep' only accepts seconds as input.
Is there any command to sleep less than 1000 ms?
sleep from GNU Core Utilities does accept decimal numbers. From sleep(1):
Pause for NUMBER seconds. SUFFIX may be s for seconds (the
default), m for minutes, h for hours or d for days. NUMBER
need not be an integer. Given two or more arguments, pause for the
amount of time specified by the sum of their values.
I also tested BusyBox version of sleep and confirmed that it also supports decimal numbers. This should clear any issues with even Alpine Linux.
The easiest workaround I could find is using bash's read builtin, which accepts milliseconds:
read -t 0.5
or with non bash scripts, for example fish:
bash -c 'read -t 0.5'
This question already has answers here:
Bash: limit the number of concurrent jobs? [duplicate]
(14 answers)
How to run a fixed number of processes in a loop?
(4 answers)
Use bash wait in for-loop [duplicate]
(4 answers)
Closed 2 years ago.
So i have like 100 test cases
for test in test_list
do
sh test &
done
wait
But i want 5 to run at a time, rather than all 100 as i end up with brokenpipe errors if i run too many at 1 time.
Is there a way to always have 5 running. So when 1 of the 5 finished, 1 more starts until we complete?
This question already has answers here:
Run command after 1 hour in Linux
(3 answers)
Closed 3 years ago.
I have a bash script to lock my computer. I want to set it to lock in 30 minutes from right now.
This is my bash script for my mac computer to lock my screen
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
I can execute it with this code
sh ~/Documents/Misc/lock_computer
You can use the sleep 30m command.
More details here: https://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/
echo 'sh ~/Documents/Misc/lock_computer' | at 'now + 30 minutes'
This question already has answers here:
Timeout a command in bash without unnecessary delay
(24 answers)
Closed 9 years ago.
I'm writing a script and would like to know how to ask one of the commands to exit after few seconds. For eg. let's suppose my script runs 2 application commands in it.
#!/bin/bash
for i in `cat servers`
do
<command 1> $i >> Output_file #Consistency command
<command 2> $i >> Output_file #Communication check
done
These commands are to check consistency & communication to/from application. I want to know how do I make sure that command 1 & 2 runs for only few seconds and if there is no response from particular host, move on to next command.
bash coreutils has got 'timeout` command.
From manual:
DESCRIPTION
Start COMMAND, and kill it if still running after NUMBER seconds. SUFFIX may be "s" for seconds (the default), "m" for
minutes, "h" for hours or "d" for days.
for example:
timeout 5 sleep 6