1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/docs/linux-service.md

176 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2020-04-29 22:35:27 +02:00
## Run BGPalerter as a Linux Service
If you are interested in running this application as a service on a Linux server here is a basic guide covering how to do that. This process works for RHEL 7 based Linux installations. It will likely work very similiarly on other systemctl enabled installations.
### Create directory for the application to reside
2020-05-18 18:05:52 +02:00
Create a user for BGPalerter
```shell
2020-05-18 18:05:52 +02:00
adduser bgpalerter
```
**If this is a new installation:**
2020-04-30 00:34:40 +02:00
* download the BGPalerter binary in the home of the newly created user:
```shell
sudo su bgpalerter
cd /home/bgpalerter
wget https://github.com/nttgin/BGPalerter/releases/latest/download/bgpalerter-linux-x64
chmod +x bgpalerter-linux-x64
```
* execute it and proceed with the auto-configuration, at the end of which all the needed files will be created:
```shell
./bgpalerter-linux-x64
```
**If this is an existing installation:**
2020-04-30 00:34:40 +02:00
* simply move the files of your existing installation into this directory and assign the correct permissions
2020-04-30 00:34:40 +02:00
```shell
mv /your/old/bgpalerter /home/bgpalerter
chown -R bgpalerter:bgpalerter /home/bgpalerter
chmod -x /home/bgpalerter/bgpalerter-linux-x64
```
### Create systemd service file
Next you need to create the systemd service file.
2020-05-18 18:05:52 +02:00
`sudo vi /etc/systemd/system/bgpalerter.service`
The contents of this file should be as follows:
```
[Unit]
2020-04-29 22:35:27 +02:00
Description=BGPalerter
After=network.target
[Service]
Type=simple
2020-05-18 18:05:52 +02:00
Restart=on-failure
RestartSec=30s
2020-05-18 18:05:52 +02:00
User=bgpalerter
WorkingDirectory=/home/bgpalerter
ExecStart=/home/bgpalerter/bgpalerter-linux-x64
[Install]
WantedBy=multi-user.target
```
### Reload systemd
Reload systemd to register the new configuration.
`systemctl daemon-reload`
### Enable and start the service
2020-04-29 22:35:27 +02:00
Enable BGPalerter to start at boot and then start the service.
`systemctl enable bgpalerter`
`systemctl start bgpalerter`
2020-08-02 11:54:10 -04:00
### Automatic Updates
You can enable automatic updates by following the instructions below.
2020-08-02 11:54:10 -04:00
Enter as bgpalerter user in its home directory:
2020-08-02 11:54:10 -04:00
```shell
sudo su bgpalerter
cd /home/bgpalerter
2020-08-02 11:54:10 -04:00
```
Create a file `upgrade.sh` with the following content:
```shell
2020-08-02 11:54:10 -04:00
#!/usr/bin/env bash
# Change the directories if needed
DIR=/home/bgpalerter
LOGS=$DIR/logs
# If log file does not exist, create it
if [ ! -f $LOGS/upgrade.log ]; then
touch $LOGS/upgrade.log
chown bgpalerter:bgpalerter $LOGS/upgrade.log
2020-08-02 11:54:10 -04:00
fi
# Delete log file if larger than 5MB
find $LOGS -type f -name "upgrade.log" -size +5M -delete
2023-10-12 00:02:42 +02:00
exec 1>> $LOGS/upgrade.log 2>&1
2020-08-02 11:54:10 -04:00
cd $DIR
2020-08-02 11:54:10 -04:00
# Download the latest version and save it to a temp file
wget --no-verbose -O bgpalerter-linux-x64.tmp https://github.com/nttgin/BGPalerter/releases/latest/download/bgpalerter-linux-x64
# Set permissions to execute the file
2020-08-02 11:54:10 -04:00
chmod +x bgpalerter-linux-x64.tmp
# Set variables to compare versions
2020-08-02 11:54:10 -04:00
if [ -f bgpalerter-linux-x64 ]; then
# If a file exists already
2020-08-02 11:54:10 -04:00
v1=$(./bgpalerter-linux-x64 -v)
v2=$(./bgpalerter-linux-x64.tmp -v)
2020-08-02 11:54:10 -04:00
else
v1=$"0"
v2=$(./bgpalerter-linux-x64.tmp -v)
fi
# If there is no old version
2020-08-02 11:54:10 -04:00
if [ "$v1" == "0" ];then
#Remove the file
rm bgpalerter-linux-x64.tmp
2020-08-02 11:54:10 -04:00
echo "This script upgrades BGPalerter; however, $DIR/bgpalerter-linux-x64 cannot be found. Please, install it first https://github.com/nttgin/BGPalerter/blob/main/docs/linux-service.md"
exit 1
2020-08-02 11:54:10 -04:00
# The versions are different
2020-08-02 11:54:10 -04:00
elif [ "$v1" != "$v2" ];then
# Remove the old version
rm bgpalerter-linux-x64
2020-08-02 11:54:10 -04:00
# Rename the temp file
2020-08-02 11:54:10 -04:00
mv bgpalerter-linux-x64.tmp bgpalerter-linux-x64
# Kill the process
# We use kill because "systemctl restart bgpalerter" works only
# if you run this script as root. Systemctl will restart the process.
kill -9 $(cat $DIR/bgpalerter.pid) || true
echo "A new version of BGPalerter has been installed"
2020-08-02 11:54:10 -04:00
else
# If the versions are the same, delete the temp file
2020-08-02 11:54:10 -04:00
rm bgpalerter-linux-x64.tmp
echo "BGPalerter is up to date"
2020-08-02 11:54:10 -04:00
fi
```
2020-08-02 11:54:10 -04:00
The `upgrade.sh` file needs to be executable
2020-08-02 11:54:10 -04:00
```shell
chmod +x upgrade.sh
2020-08-02 11:54:10 -04:00
```
Now we need to configure a cron job to periodically execute `upgrade.sh`.
2020-08-02 11:54:10 -04:00
As the `bgpalerter` user, do
2020-08-02 11:54:10 -04:00
```shell
crontab -e
```
and append the following line to perform the check weekly
2020-08-02 11:54:10 -04:00
```
0 0 * * 0 /home/bgpalerter/upgrade.sh
2020-08-02 11:54:10 -04:00
```