summaryrefslogtreecommitdiff
path: root/src/randsleep.1
blob: ece722a7de41c80c7a2588c11d260aaeb830a980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
.Dd July 06, 2011
.Dt RANDSLEEP 1
.Os
.
.
.Sh NAME
.
.Nm randsleep
.Nd Execute program after sleeping for a random time
.
.
.Sh SYNOPSIS
.
.Nm
.Ar time
.Op Ar command args ...
.
.
.Sh VERSION
.
This manual documents
.Nm
version 0.0
.
.
.Sh DESCRIPTION
.
.Nm
execs into
.Ar command
after sleeping for up to
.Ar time
seconds.
It is intended for use in cronjobs and similar.
.
.Pp
.
If
.Ar command
is not specified,
.Nm
will simply return after sleeping.
.
.
.Sh RATIONALE
.
When executing commands via cron, you can only select the point at which
they're run in minute granularity. Also, you rarely know how many other
commands may be started at the same point.
To avoid load spikes by too many cronjobs running at the same time, it has
become a rather widespread practice to sleep for a random amount of time
.Pq usually something between 0 and 30 minutes at most
before actually doing anything.
.
.Pp
.
Since the snippets responsible for this may, especially if the shell does not
support
.Ev $RANDOM ,
become quite long, I decided to write this utility.
It is an extremely simple C program with no additional dependencies, so using
it should not be a problem.
.
.
.Sh EXAMPLE
.
.Dl #!/bin/sh
.Dl if [\& -z \&"$RANDOM\&" ]; then
.Dl   RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c\&"1-5\&")
.Dl fi
.Dl sleep $(( RANDOM % 1800 ))
.Dl # actually do something
.
.Pp
.
With
.Nm :
.
.Dl #!/bin/sh
.Dl randsleep 1800
.Dl # actually do something
.
.Pp
.
Or
.Pq as crontab entry :
.
.Dl 10 * * * * root \& \& randsleep 1800 /path/to/script with arguments
.
.
.Sh AUTHOR
.
Copyright (C) 2011 Daniel Friesel
.Aq derf@finalrewind.org .
.
.
.Sh LICENSE
.
You just DO WHAT THE FUCK YOU WANT TO.