Files
killswitch-cli/src/killsw
2026-03-24 20:05:50 +01:00

95 lines
2.8 KiB
Bash

#!/bin/bash
VERSION="1.1.0"
cmd="$1"
group="$2"
add_service_to_group() {
service="$1"
group="$2"
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
sudo mkdir -p "/etc/systemd/system/${group}.target.wants"
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
systemctl stop "$group".target
echo "Killswitch activated: stopped group '$group'."
else
systemctl stop killswitch.target
echo "Killswitch activated: all linked services have been stopped."
fi
;;
start)
if [[ -n "$group" ]]; then
systemctl start "$group".target
echo "Killswitch deactivated: started group '$group'."
else
systemctl start killswitch.target
echo "Killswitch deactivated: all linked services have been started."
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
add_service_to_group "$2" "$4"
else
echo "Usage: killsw add [service] to [group]"
fi
;;
help|-h|--help)
cat << EOF
KillSwitch CLI Tool - v$VERSION
USAGE:
killsw <command> [group]
COMMANDS:
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 or starts services linked to 'killswitch.target'. Supports groups for partial control.
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) Use killsw script to manage:
killsw start [group]
killsw stop [group]
EOF
;;
-v|--version)
echo "KillSwitch CLI Tool version $VERSION"
;;
*)
echo "Ahoy, matey! No command provided. Check --help for guidance."
;;
esac