-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd_install.sh
executable file
·118 lines (101 loc) · 2.75 KB
/
systemd_install.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
#!/bin/bash
host_name=$(hostnamectl hostname)
usage() {
echo "Usage: ./systemd_install.sh -e executable_name
Example: ./systemd_install.sh -e ${host_name^}"
}
confirm() {
read -rp " > ${1} [y/n] " yn
case "$yn" in
[yYsS])
true ;;
*)
false ;;
esac
}
while getopts 'e:' flag
do
case "${flag}" in
e) executable=${OPTARG} ;;
*) usage
exit 1 ;;
esac
done
if [[ $# -le 1 ]]
then
usage
exit 1
fi
# Check whether the script does exist
if ! command -v ${executable} > /dev/null
then
echo "Can't find executable ${executable} in path"
usage
exit 1
fi
# Check whether the config file does exist in the usual place, if it doesn't, run init
if [ ! -f ${HOME}/.config/pybliotecario/pybliotecario.ini ]
then
echo "User config file not found in ${HOME}/.config/pybliotecario/pybliotecario.ini"
echo "Running --init to configure ${executable} for the first time"
${executable} --init || exit 1
fi
# Small bash script to create a systemd unit with to start the
# bot on start as a daemon
# TODO: make it into part of the --install command?
unitname=pybliotecario
username=$USER
python_exe=$(which python3)
pybliotecario_exe=$(which ${executable})
systemd_file=/usr/lib/systemd/system/${unitname}.service
# Check whether the service already exists
if [[ -f "${systemd_file}" ]]
then
echo "Unit ${unitname} already exists"
if ! confirm "Overwrite previous unit?"
then
exit 1
fi
fi
# Now select the different options
extra_options=""
echo "Sometimes badly formed commands or network errors can make the bot fail
The bot can just exit on failure or just ignore the error"
if confirm "Do you want to ignore errors?"
then
extra_options="${extra_options} --clear_incoming"
limit_interval="
StartLimitInterval=3600
StartLimitBurst=5"
restart_on="
Restart=on-failure
RestartSec=60s"
fi
echo "You can receive the IP of the host computer every time the unit starts"
if confirm "Do you want to recieve a msg with the IP of the computer?"
then
extra_options="${extra_options} --my_ip Estoy despierto, ip: "
fi
tmpfile=.pybliounit.unit
echo "[Unit]
Description=My Script Service
After=multi-user.target
[Service]
User=${username}
Type=simple
ExecStart=${python_exe} ${pybliotecario_exe} -d ${extra_options}
${restart_on}
[Install]
WantedBy=multi-user.target" > ${tmpfile}
# From now on we need sudo
sudo mv ${tmpfile} ${systemd_file}
# Kill it in case it was already there
if systemctl is-active --quiet ${unitname} > /dev/null
then
echo "${unitname} was already active, restarting"
sudo systemctl stop ${unitname}
sudo systemctl disable ${unitname}
fi
sudo systemctl daemon-reload
sudo systemctl enable ${unitname}
sudo systemctl start ${unitname}