Update src/killsw

This commit is contained in:
2026-03-24 19:59:19 +01:00
parent f9bb3c21a9
commit 4dd188eba6

View File

@@ -1,18 +1,38 @@
#!/bin/bash
VERSION="1.0.1"
VERSION="1.1.0"
cmd="$1"
group="$2"
add_service_to_group() {
service="$1"
group="$2"
# Maak de groep-target aan als die nog niet bestaat
if [[ ! -f "/etc/systemd/system/${group}.target" ]]; then
sudo tee "/etc/systemd/system/${group}.target" > /dev/null <<EOF
[Unit]
Description=Group target $group
PartOf=killswitch.target
EOF
fi
# Zorg dat de .wants map bestaat
sudo mkdir -p "/etc/systemd/system/${group}.target.wants"
# Voeg de service toe aan de groep
sudo ln -sf "/etc/systemd/system/${service}.service" "/etc/systemd/system/${group}.target.wants/${service}.service"
echo "Added service '$service' to group '$group'."
}
case "$cmd" in
stop)
if [[ -n "$group" ]]; then
# Alleen die groep stoppen
systemctl stop "$group".target
echo "Killswitch activated: stopped group '$group'."
else
# Geen groep, alles stoppen
systemctl stop killswitch.target
echo "Killswitch activated: all linked services have been stopped."
fi
@@ -27,11 +47,15 @@ case "$cmd" in
fi
;;
status)
if [[ -n "$group" ]]; then
systemctl status "$group".target
else
systemctl status killswitch.target
fi
;;
add)
if [[ -n "$2" && "$3" == "to" && -n "$4" ]]; then
echo "Adding service '$2' to group '$4'..."
add_service_to_group "$2" "$4"
else
echo "Usage: killsw add [service] to [group]"
fi
@@ -41,35 +65,28 @@ case "$cmd" in
KillSwitch CLI Tool - v$VERSION
USAGE:
killsw <command>
killsw <command> [group]
COMMANDS:
stop Stop all services linked to killswitch.target
start Start all services linked to killswitch.target
status Show the current status of the killswitch target
add [service] to [name] adds services to different groups. (not doing anything at the moment)
stop Stop all services linked to killswitch.target, or a specific group
start Start all services linked to killswitch.target, or a specific group
status Show the current status of killswitch.target or a group
add [service] to [group] Add a service to a group (creates group-target if needed)
-h, --help Show this help message
-v, --version Show version information
ABOUT KILLSWITCH:
What it does:
Stops all services that are linked to 'killswitch.target'. This allows
you to perform an emergency stop or centralized management of multiple
services.
Stops or starts services linked to 'killswitch.target'. Supports groups for partial control.
Why it's needed:
Useful for quickly stopping servers or services in case of maintenance,
emergency, or to prevent unsafe operations.
How to enable it on your services:
1) Create or edit your systemd service to link it to killswitch.target:
Add the following to your [Unit] section:
PartOf=killswitch.target
2) Optionally, enable the service to start at boot:
How to link services:
1) Edit your systemd service and add:
PartOf=<group>.target
2) Enable the service for boot:
sudo systemctl enable <your-service>.service
3) Start/stop via this script:
killsw start
killsw stop
3) Use killsw script to manage:
killsw start [group]
killsw stop [group]
EOF
;;
-v|--version)