abducate

abduco wrapper to mimic rc-style user services
git clone git://git.girlpoison.org/abducate
Log | Files | Refs | README

commit 6c89d9b7fd63e42b112bcb9687ca2dc2ffac9296
Author: Giygas <mvk@girlpoison.org>
Date:   Sun,  2 Jun 2024 03:18:09 +0200

Initial commit

Diffstat:
AREADME | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Aabducate | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -0,0 +1,52 @@ +Intro +------ +abducate is a small wrapper for abduco designed to run commands in the style of background services. +When starting a service through abducate, the given command is run in a new abduco session. abducate will not attach to it. It will exit +with an error if the given session is already running. Sessions are maintained in a separate directory, $HOME/.abducate by default. + +Contrived example +------------------ +$ abducate start picom +$ abducate start sxhkd +$ abducate start sxhkd +abducate: already running +$ abducate toggle sxhkd +$ abducate list +Active sessions (on host girlpoison) + Tue 2024-05-28 13:58:58 9500 picom + +Usage +------ +abducate [-n] [-l label] [start | stop | toggle | list | attach | status] command [args...] + +OPTIONS + -l label + Specify which session to interact with. If omitted, abducate will assume the name + of the given command as the label. To aid in readability, this applies for actions + that don't require a command argument, too. + -n + Trigger a desktop notification after creating or terminating a session. + +ACTIONS + start + Create a new abduco session, if one with the given label is not already running. + stop + Terminate the session with the given label, if it is running. + toggle + Create a new session if it is not currently running. Otherwise, terminate the + session with the given label. + list + Display the list of currently running sessions. + attach + Attach to the specified session. + status + Print the status of the specified session. + +ENVIRONMENT VARIABLES + ABDUCATE_DIR + Specifies the directory in which abducate stores session information. It defaults + to $HOME/.abducate + +Notes +------ +abducate depends on the most recent version of abduco, which displays the PID of the running process in the session listing. Notably, the abduco package from the official Arch Linux repositories is an earlier version. Install abduco-git from the AUR. diff --git a/abducate b/abducate @@ -0,0 +1,58 @@ +#!/bin/sh +PROGNAME="$(basename $0)" +[ "$ABDUCATE_DIR" ] || ABDUCATE_DIR="$HOME/.abducate" +export ABDUCO_SOCKET_DIR="$ABDUCATE_DIR" + +err() { + echo "$PROGNAME: $*" >&2 +} + +usage() { + err "usage: $PROGNAME [-n] [-l label] [start | stop | toggle | list | attach | status] command [args...]" +} + + +while getopts "nl:" opt; do + case ${opt} in + n) + NOTIFY=1 + ;; + l) + LABEL="${OPTARG}" + ;; + *) + usage + exit 1;; + esac +done + +shift $((OPTIND-1)) + +[ "$1" ] && WHAT=$1 && shift; +[ "$1" ] && SERVE=$1 && shift; +[ ! "$LABEL" ] && LABEL=$SERVE; + +read _ _ _ PID _ <<<$(abduco | sed -n '2,$p' | awk -v label=$LABEL '$NF==label && $1!="+"') +if [ "$WHAT" = "toggle" ]; then [ $PID ] && WHAT="stop" || WHAT="start"; fi + +case $WHAT in +start) + if [ $PID ]; then err "already running" && exit 1; fi + + abduco -frn $LABEL $SHELL -c "ABDUCO_SOCKET_DIR='' $SERVE $*" + [ $NOTIFY ] && notify-send "$PROGNAME: enabled $LABEL";; +stop) + if [ ! $PID ]; then err "not running" && exit 1; fi + kill $PID + [ $NOTIFY ] && notify-send "$PROGNAME: disabled $LABEL";; +list) + abduco;; +attach) + abduco -a $LABEL;; +status) + [ $PID ] && echo "enabled" || echo "disabled";; +*) + usage && exit 1;; +esac + +exit 0;