av/cmd/vidforward/create_service.sh

57 lines
1.7 KiB
Bash

#!/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
# 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"
# 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]
Type=simple
ExecStart=$gopath$run_script_dir $gopath_user $bin_dir
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