How to sleep less than a second in linux bash [duplicate] - bash

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'

Related

Bash - kill a command after a certain time [duplicate]

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.

Run bash command and kill it if it exceeds specific time [duplicate]

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.

Running a program with timeout

I am running the timeout command of GNU Coreutils,
gtimeout 600 python myprogram.py
According to the manual,
duration is a floating point number followed by an optional unit:
‘s’ for seconds (the default) ‘m’ for minutes ‘h’ for hours ‘d’ for
days
Thus, the 'python myprogram.py part should terminate within 600 seconds (10 minutes). To my surprise, the command actually timeouts after 1 hour. Why?
It's possible that your program ignores SIGTERM, the signal which is used by gtimeout to "kindly ask the program to terminate".
You can have gtimeout use SIGKILL instead, which can't be ignored or blocked, by adding the parameter -s 9 like this:
gtimeout -s 9 python myprogram.py

Running a shell script once a day at random time [duplicate]

This question already has answers here:
Cron jobs and random times, within given hours
(13 answers)
Closed 9 years ago.
Need run a shell script once a day at random time. (so once every day between 00:00-23:59).
I know the sleep command, and the cron too, but
the cron has not random times
and the sleep solution - not very nice - my idea is launch the script every midnight and sleep random time at the start of the script.
Is here something more elegant?
If you have the at command, you can combinte the cron and the at.
Run from a cron every midnight the next script:
#!/bin/bash
script="/tmp/script.sh" #insert the path to your script here
min=$(( 24 * 60 ))
rmin=$(( $RANDOM % $min ))
at -f "$script" now+${rmin}min
The above will run the at command every midnight and will execute your script at random time . You should check your crontab how often is the atrun command started. (The atrun runs the commands stored with the at)
The main benefit in comparison with the sleep method: this "survives" the system reboot.
I would simply launch you script at midnight, and sleep for a random time between 0 and 86400 seconds. Since my bash's $RANDOM returns a number between 0 and 32767:
sleep $(( ($RANDOM % 1440)*60 + ($RANDOM % 60) ))
The best alternative to cron is probably at
See at man page
Usually, at reads commands from standard input, but you can give a file of jobs with -f.
Time wise, you can specify many formats. Maybe in your case the most convenient would be
at -f jobs now + xxx minutes
where your scripts gives xxx as a random value from 1 to 1440 (1440 minutes in a day), and jobs contains the commands you want to be executed.
Nothing prevents you from running sed to patch your crontab as the last thing your program does and just changing the next start time. I wouldn't sleep well though.
You can use cron to launch bash script, which generates pseudorandom timestamp and gives it to unix program at
I see you are familiar with bash and cron enough, so at will be a piece of cake for you. Documentation as always "man at" or you can try wiki
http://en.wikipedia.org/wiki/At_(Unix)

How to exit from a command after n seconds? [duplicate]

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

Resources