diff --git a/share/systemd/README.md b/share/systemd/README.md new file mode 100644 index 0000000..59ea96a --- /dev/null +++ b/share/systemd/README.md @@ -0,0 +1,42 @@ +To add **wttr.in** to systemd as a service, do the following steps. + + +1. **Create a systemd service file**: You’ll need to create a service file in `~/.config/systemd/user/` (for user-level) or `/etc/systemd/system/` (for system-wide) directory. Let’s create it for a user. + +Create the directory if it doesn’t exist: + +```bash +mkdir -p ~/.config/systemd/user/ +``` + +Then, create the service file called `myscript.service`: + +```bash +cp share/systemd/wttrin.service ~/.config/systemd/user/myscript.service +``` + +2. **Reload the systemd daemon**: This will ensure systemd recognizes the new service. + +```bash +systemctl --user daemon-reload +``` + +4. **Enable and start your service**: + +To start the service immediately, run: + +```bash +systemctl --user start wttrin.service +``` + +To enable it to start automatically at boot, run: + +```bash +systemctl --user enable wttrin.service +``` + +5. **Check the status**: To verify that your service is running correctly, you can check its status: + +```bash +systemctl --user status wttrin.service +``` diff --git a/share/systemd/wttrin.service b/share/systemd/wttrin.service new file mode 100644 index 0000000..f5f7956 --- /dev/null +++ b/share/systemd/wttrin.service @@ -0,0 +1,11 @@ +[Unit] +Description=wttr.in services + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/home/igor/src/wttr.in/share/systemd/wttrin.sh session1 +ExecStop=/usr/bin/tmux kill-session -t session1 + +[Install] +WantedBy=default.target diff --git a/share/systemd/wttrin.sh b/share/systemd/wttrin.sh new file mode 100755 index 0000000..7e9b0b8 --- /dev/null +++ b/share/systemd/wttrin.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +SESSION_NAME="" +SRC_DIR=/home/igor/src/wttr.in +SERVICES_FILE=config/services/services.yaml + +start_service() { + local name="$1" + local workdir="$2" + local cmd="$3" + + if [[ -z "$SESSION_NAME" ]]; then + echo Unknown SESSION_NAME. Exiting >&2 + exit 1 + fi + + local WINDOW_NAME="$name" + local TEXT_TO_ENTER="cd $workdir && $cmd" + + if ! tmux has-session -t "$SESSION_NAME" >& /dev/null; then + tmux new-session -d -s "$SESSION_NAME" + fi + + # Create a new window if it doesn't exist + tmux list-windows -t "$SESSION_NAME" | grep -q "^.\+ $WINDOW_NAME " || tmux new-window -t "$SESSION_NAME" -n "$WINDOW_NAME" + + sleep 0.05 + + # Send text to the new window and press Enter + tmux send-keys -t "$SESSION_NAME:$WINDOW_NAME" "$TEXT_TO_ENTER" C-m +} + + +main() { + local name + local cmd + + if [[ -n $1 ]]; then + SESSION_NAME=$1 + else + echo Usage: $0 SESSION_NAME >&2 + exit 1 + fi + + cd "$SRC_DIR" || exit 1 + + set -x + + while read -r line; do + name=$(jq -r .name <<< "$line") + workdir=$(jq -r .workdir <<< "$line") + cmd=$(jq -r .command <<< "$line") + + start_service "$name" "$workdir" "$cmd" + done <<< "$(yq -c .services[] < "$SERVICES_FILE")" +} + +main "$@"