-
Notifications
You must be signed in to change notification settings - Fork 0
/
49-docron
39 lines (29 loc) · 1.37 KB
/
49-docron
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
#!/bin/bash
# docron--Runs the daily, weekly, and monthly system cron jobs on a
# system that's likely to be shut down during the usual time of day when
# the system cron jobs would otherwise be scheduled to run.
rootcron="/etc/crontab" # This is going to vary significantly based on
# which version of Unix or Linux you've got.
if [ $# -ne 1 ] ; then
echo "Usage: $0 [daily|weekly|monthly]" >&2
exit 1
fi
# If this script isn't being run by the administrator, fail out. In
# earlier scripts, you saw USER and LOGNAME being tested, but in this
# situation, we'll check the user ID value directly. Root = 0.
if [ "$(id -u)" -ne 0 ] ; then
# Or you can use $(whoami) != "root" here, as needed.
echo "$0: Command must be run as 'root'" >&2
exit 1
fi
# We assume that the root cron has entries for 'daily', 'weekly', and
# 'monthly' jobs. If we can't find a match for the one specified, well,
# that's an error. But first, we'll try to get the command if there is
# a match (which is what we expect).
job="$(awk "NF > 6 && /$1/ { for (i=7;i<=NF;i++) print \$i }" $rootcron)"
if [ -z "$job" ] ; then # No job? Weird. Okay, that's an error.
echo "$0: Error: no $1 job found in $rootcron" >&2
exit 1
fi
SHELL='which sh' # To be consistent with cron's default
eval $job # We’ll exit once the job is finished.