av/cmd/vidforward/create_service.sh

62 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/bash
# This script is utilised by Makefile for creation of a systemd service. The
# service lines are stored in a string allowing us to substitute the GOPATH into
# the ExecStart path of the service file.
if [ $# -ne 2 ]; then
echo "incorrect number of arguments, expected run script and binary directories"
exit 1
fi
# This corresponds to the path, relative to the GOPATH, of the run script that
# will be used by the service e.g. "/src/bitbucket.org/ausocean/av/cmd/vidforward/run.sh"
run_script_dir=$1
# This corresponds to the binary dir. e.g. /src/bitbucket.org/ausocean/av/cmd/vidforward.
bin_dir=$2
2023-02-08 08:49:05 +03:00
# This is the IP we'll run the host on.
host=$(hostname -I | awk '{print $1}')
# Get the bin name (assuming this is at the end of the bin_dir).
bin_name=$(basename $bin_dir)
# First find the user that corresponds to this path (which is assumed to be at the
# base of the current working directory).
in=$(pwd)
arr_in=(${in//// })
gopath_user=${arr_in[1]}
# We can now form the gopath from the obtained user.
gopath="/home/$gopath_user/go"
2023-02-08 08:49:05 +03:00
echo "$gopath$run_script_dir $gopath_user $bin_dir $host"
# Here are the lines that will go into the rv.service file. We'll set the
# ExecStart field as the GOPATH we've obtained + the passed run script dir.
service="
[Unit]
Description=vidforward service for forwarding video to youtube
[Service]
2023-02-08 08:49:05 +03:00
Type=notify
ExecStart=$gopath$bin_dir/vidforward -host $host
WatchdogSec=30s
Restart=on-failure
[Install]
WantedBy=multi-user.target
"
# The service name will just use the bin name.
service_name="$bin_name.service"
# Now overwrite the service if it exists, or create the service then write.
service_dir=/etc/systemd/system/$service_name
if [ -f $service_dir ]; then
echo "$service" > $service_dir
else
touch $service_dir
echo "$service" > $service_dir
fi