-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-cron-job.sh
executable file
·128 lines (113 loc) · 3.89 KB
/
add-cron-job.sh
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
set -e
#set -x
scriptdir=$(cd $(dirname $0); pwd -P)
. $scriptdir/os.sh
name=eprint-iacr-notifier.py
bin=/usr/local/bin/$name
conf=/etc/eprint-iacr-notifier.conf
logdir=/var/log/eprint
logfile=$logdir/out+err.log
if [ "$OS" = "OSX" ]; then
user_group=staff
root_group=wheel
cronfile=$HOME/Library/LaunchAgents/org.iacr.eprint.notifier.plist
else
user_group=`whoami`
root_group=root
crondir=/etc/cron.d
cronfile=$crondir/eprint-iacr-notifier
fi
# symlink script in /usr/local/bin
sudo ln -sf $scriptdir/$name $bin
sudo chmod +x $bin
if [ ! -f "$conf" ]; then
# write year and latest paper ID to conf file
conf_tmp=`mktemp`
year=`date +%Y`
read -p "What is the last paper ID you have read? " ID
echo $year >$conf_tmp
echo $ID >>$conf_tmp
sudo mv $conf_tmp $conf
else
echo "WARNING: Already detected config file at: $conf. Leaving intact."
fi
# create log file and dir
if [ ! -d $logdir ]; then
sudo mkdir $logdir
fi
if [ ! -f "$logfile" ]; then
sudo touch "$logfile"
else
echo "WARNING: Already detected log file at: $logfile. Leaving intact."
fi
sudo chown `whoami`:$user_group $logdir
sudo chown `whoami`:$user_group "$logfile"
if [ ! -f "$cronfile" ]; then
# get details about the Gmail account used to send emails
read -p "What is the email address that should receive new paper notifications? " NOTIF_EMAIL
read -p "Gmail account username that should send out the emails: " GMAIL_USER
read -p "Gmail account password: " GMAIL_PASSWD
GMAIL_USER=${GMAIL_USER%"@gmail.com"} # in case user gives extra @gmail.com
GMAIL_ADDR="[email protected]"
fi
if [ "$OS" = "OSX" ]; then
if [ ! -f "$cronfile" ]; then
# copy .plist file to ~/Library/LaunchAgents
cat >$cronfile << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.iacr.eprint.notifier</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/python3</string>
<string>/usr/local/bin/eprint-iacr-notifier.py</string>
<string>$NOTIF_EMAIL</string>
<string>$GMAIL_ADDR</string>
<string>$GMAIL_PASSWD</string>
<string>/etc/eprint-iacr-notifier.conf</string>
<!--<string>1</string>
<string>1</string>-->
</array>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>StandardErrorPath</key>
<string>$logfile</string>
<key>StandardOutPath</key>
<string>$logfile</string>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>
EOF
else
echo "WARNING: Already detected plist file at: $cronfile. Leaving intact."
fi
launchctl unload $cronfile || :
launchctl load -w $cronfile
echo "launchd job status: "
launchctl list | grep org.iacr.eprint.notifier
elif [ "$OS" = "Linux" ]; then
if [ ! -f "$cronfile" ]; then
# configure cron job in /etc/cron.d/
# For help, see: https://crontab.guru/#0_5_*_*_1-7
time="5" # the hour of the day to run the script: from 0 to 23 (military format)
days="1-7" # the days of the week to run the script: any range from 1 to 7
cron_tmp=`mktemp`
echo "0 $time * * $days `whoami` $bin $NOTIF_EMAIL $GMAIL_ADDR $GMAIL_PASSWD $conf >>$logfile 2>&1" >$cron_tmp
# if you want to test the cron job is installing correctly, use this 'every 1-minute' template instead
#echo "* * * * * `whoami` $bin $NOTIF_EMAIL $GMAIL_ADDR $GMAIL_PASSWD $conf >>$logfile 2>&1" >$cron_tmp
sudo mkdir -p $crondir
sudo mv $cron_tmp $cronfile
sudo chmod 0644 $cronfile
sudo chown root:$root_group $cronfile
else
echo "WARNING: Already detected cron file at: $cronfile. Leaving intact."
fi
fi