From 413f687c9e5b8e672ec84a948eed865fe7d8fed7 Mon Sep 17 00:00:00 2001 From: Wouter Horlings Date: Thu, 21 Nov 2019 11:37:51 +0100 Subject: [PATCH] added a script that installs the timers in the correct location. And updated the readme in that folder --- systemd_timer_files/README.md | 12 ++++--- systemd_timer_files/settimer.sh | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 systemd_timer_files/settimer.sh diff --git a/systemd_timer_files/README.md b/systemd_timer_files/README.md index 5e6415a..a7149fd 100644 --- a/systemd_timer_files/README.md +++ b/systemd_timer_files/README.md @@ -1,9 +1,13 @@ # Installation of timer files +Easiest way is to run `settimer.sh` from inside the git repo. +The scripts expect that the virtual environment for python is already setup correctly in `.venv` -# Setting the correct path + + +## Setting the correct path In `pythontaskcheck.timer` replace `[[path_to_repo]]` with the absolute path to the repo root. -# Setting up systemd.timer +## Setting up systemd.timer Move `pythontaskcheck.timer` and `pytontaskcheck.service` to the `$XDG_RUNTIME_DIR/systemd/user/` dir. Run the following commands to enable the scheduled execution. ``` @@ -12,7 +16,7 @@ $ systemctl --user enable pythontaskcheck.timer $ systemctl --user start pythontaskcheck.timer ``` -# Check if everything is working +## Check if everything is working ``` $ systemctl --user start pythontaskcheck.service ``` @@ -22,6 +26,6 @@ $ systemctl list-timers --user ``` shows the list with timers. Yours should be listed here. -# updating the time. +## updating the time. Current setup will trigger the script every 5 minutes from 9:00 till 17:55 diff --git a/systemd_timer_files/settimer.sh b/systemd_timer_files/settimer.sh new file mode 100644 index 0000000..85a129f --- /dev/null +++ b/systemd_timer_files/settimer.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# Function to set the path correct in the config files and store them in the correct location. +replace_and_move() { + # The ${GIT_DIR//\//\\\/} replaces '/' with '\/' as '/' is a control char in regex + REPLACE_STR="s/\[\[path_to_repo\\]\\]/${GIT_DIR//\//\\\/}/g" + OUTDIR="$DIRECTORY/$1" + # Parse the file trough `sed` and write output to correct directory + sed $REPLACE_STR $2/$1 > $OUTDIR + echo "$1 script placed at $OUTDIR" +} + +################### +# START OF SCRIPT # +################### +# Check if XDG_CONFIG_HOME is set and select correct config file location. +# See also: systemd.unit manual - section: UNIT FILE LOAD PATH +if [[ -e $XDG_CONFIG_HOME ]]; then + DIRECTORY=$XDG_CONFIG_HOME/systemd/user +else + DIRECTORY=$HOME/.config/systemd/user +fi +echo "The scripts will be put in $DIRECTORY" +# The following git command will give the root of the current repo. +GIT_DIR=`git rev-parse --show-toplevel` +echo "Root of this git repo is: $GIT_DIR" +if [[ -e $GIT_DIR/.venv/bin/python3 ]]; then + echo "Found a virtual environment for Python in the repo" +else + echo "Pythons virtual environment not found at '$GIT_DIT/.venv'. " + #There should be a setup.sh file in the directory that setups the virtual environment for python + if [[ -f $GIT_DIR/setup.sh ]]; then + echo "Have you tried running '$GIT_DIR/setup.sh'?" + fi + echo "I could install config files, but they whould not work. Aborting now" + exit +fi + +FILENAME="pythontaskcheck" +FOLDERNAME="systemd_timer_files" +MAPPATH="$GIT_DIR/$FOLDERNAME/" +TIMERNAME="$FILENAME.timer" +SERVICENAME="$FILENAME.service" +replace_and_move $TIMERNAME $MAPPATH +replace_and_move $SERVICENAME $MAPPATH + +echo "Enable $TIMERNAME for systemd" +`systemctl --user enable $TIMERNAME` +echo "Start $TIMERNAME for systemd" +`systemctl --user start $TIMERNAME` + + + + +