Add systemd support

pull/1065/head
Igor Chubin 1 month ago
parent b2832ceaf0
commit 1915a86dd7

@ -0,0 +1,42 @@
To add **wttr.in** to systemd as a service, do the following steps.
1. **Create a systemd service file**: Youll need to create a service file in `~/.config/systemd/user/` (for user-level) or `/etc/systemd/system/` (for system-wide) directory. Lets create it for a user.
Create the directory if it doesnt 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
```

@ -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

@ -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 "$@"
Loading…
Cancel
Save