-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinstall
More file actions
executable file
·102 lines (89 loc) · 2.1 KB
/
install
File metadata and controls
executable file
·102 lines (89 loc) · 2.1 KB
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
#!/usr/bin/env bash
# verify permission
if [ "${EUID:-$(id -u)}" != 0 ]; then
echo >&2 "error: permission denied"
echo >&2 'installation requires root privileges'
exit 1
fi
# go to script dir
cd "$(dirname "$(readlink -f "$0")")"
# add cron scheduled job
add_cron(){
local name="${1}"
local cmd="${2}"
local motd="${3}"
cron_file="/etc/cron.hourly/${name}"
echo "[+] add cron in ${cron_file}"
cat << EOF > "${cron_file}"
#!/usr/bin/env sh
${cmd} > "${motd}"
EOF
chmod +x "${cron_file}"
}
# add systemd scheduled job
add_systemd(){
local name="${1}"
local cmd="${2}"
local motd="${3}"
local service_file="/etc/systemd/system/${name}.service"
echo "[+] add service in ${service_file}"
cat << EOF > "${service_file}"
[Unit]
Description=${name} executor
After=network.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=${cmd}
StandardOutput=truncate:${motd}
StandardError=truncate:${motd}
[Install]
WantedBy=multi-user.target
EOF
local timer_file="/etc/systemd/system/${name}.timer"
echo "[+] add timer in ${timer_file}"
cat << EOF > "${timer_file}"
[Unit]
Description=${name} timer
[Timer]
OnCalendar=hourly
AccuracySec=5s
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now ${name}.timer
}
# parse args
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
case "$1" in
-u|--user) shift; user="$1";;
esac
shift
done
if [[ -z "${user}" ]]; then
echo >&2 "error: no user specified"
echo >&2 "usage: ./$(basename "${0}") --user <USER>"
exit 1
fi
# get homedir
home="$(getent passwd "${user}" | cut -d: -f6)"
# search for job scheduler
motd="$(pwd)/data/motd"
if [[ -d /run/systemd/system ]]; then
add_systemd 'tinymotd' "$(pwd)/generate" "${motd}"
elif [[ -d /etc/cron.hourly ]]; then
add_cron 'tinymotd' "$(pwd)/generate" "${motd}"
else
echo >&2 "error: no job scheduler method found"
exit 1
fi
# add print
bashrc="${home}/.bashrc"
echo "[+] add motd load in ${bashrc}"
cat << EOF >> "${bashrc}"
# motd supervision
if [[ -f ${motd} ]]; then
cat ${motd}
fi
EOF