make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / cronrun
blob668f1ff7324b6a68644b76bb361bbc2d76c63d12
1 #!/bin/bash
5 true <<'EOF'
6 =pod
8 =head1 NAME
10 cronrun - convenience features to run commands in task scheduler environment
12 =head1 SYNOPSIS
14 cronrun [I<OPTIONS>] <I<COMMAND>> [I<ARGS>]
16 Run I<COMMAND> in a way most scheduled jobs are intended to run, ie:
18 =over 4
20 =item Set computing priority (nice(1), ionice(1)) to low
22 =item Delay start for random amount of time, thus avoiding load-burst when multiple jobs start at the same time
24 =item Allow only one instance at a time (by locking)
26 =back
28 =head1 OPTIONS
30 =over 4
32 =item --random-delay, -d I<SEC>
34 Delay program execution at most I<SEC> seconds.
35 Default is to wait nothing.
37 =item --wait-lock, -W
39 Wait for the lock to release.
40 By default cronrun(1) fails immediately if locked.
42 =back
44 =head1 DESCRIPTION
46 Lock is based on C<CRONJOBID> environment, or I<COMMAND> if C<CRONJOBID> is not set.
48 If C<CRONJOBID> is set, STDIO goes to syslog too, in the "cron" facility, stdout at info level, stderr at error level.
49 If not set, STDIO is not redirected.
51 =head1 FILES
53 =over 4
55 =item F<~/.cache/cronrun>
57 Lock files stored in this directory.
59 =back
61 =head1 ENVIRONMENT
63 =over 4
65 =item CRONJOBID
67 Recommended practice is to set C<CRONJOBID=something> in your crontab before each C<cronrun ...> job definition.
69 =back
71 =head1 LIMITATIONS
73 =head1 SEE ALSO
75 =cut
77 EOF
82 scriptname=${0##*/}
83 random_delay_sec=0
84 wait_lock=''
86 while [ $# != 0 ]
88 case "$1" in
89 --help)
90 exit;;
91 -d|--random-delay)
92 shift
93 random_delay_sec=$1
95 -W|--wait-lock)
96 wait_lock=1
98 --) shift; break;;
99 -*) echo "$scriptname: unknown option: $1" >&2
100 exit -1;;
101 *) break;;
102 esac
103 shift
104 done
106 if [ `id -u` = 0 ]
107 then
108 lockdir=/var/run/lock/cronrun
109 else
110 lockdir=~/.cache/cronrun/locks
112 cmd_hash=`echo -n "${CRONJOBID:-$1}" | md5sum | cut -c1-32`
115 # delay random amount of seconds
116 sleep $[RANDOM % ($random_delay_sec + 1)]
119 # set up logging
120 if [ -n "$CRONJOBID" ]
121 then
122 syslog_ident=$LOGNAME.${CRONJOBID//\//.}
123 exec 1> >(exec logger -p cron.info -t "$syslog_ident" -s 2>&1) 2> >(exec logger -p cron.error -t "$syslog_ident" -s 1>&2)
127 # scheduled jobs are likely good to run at low priority
128 renice -n 19 -p $$ >/dev/null
129 ionice -c 3 -p $$
132 # check the lock
133 mkdir -p "$lockdir"
134 lockfile=$lockdir/$cmd_hash
135 exec {lock_fd}>>"$lockfile"
136 flock --exclusive --nonblock $lock_fd
137 errno=$?
138 if [ $errno != 0 ]
139 then
140 if [ $wait_lock ]
141 then
142 echo "cronrun: waiting on lock..." >&2
143 flock --exclusive $lock_fd
144 errno=$?
145 if [ $errno != 0 ]
146 then
147 exit $errno
149 echo "cronrun: lock acquired" >&2
150 else
151 exit $errno
155 # ---- lock acquired ----
157 # run the job
158 exec "$@"
160 exit $?