[RndTbl] "sc" - helper script for systemctl

Brian Lowe muug-mail at groupbcl.ca
Wed Feb 13 13:57:10 CST 2019


Here's a helper script I wrote for systemctl to reduce the amount of typing 
you need to do to invoke common systemctl function. Put this script into
/usr/bin or /usr/local/bin (in which case, you may need to add /usr/local/bin 
to root's PATH), then run 'sc --setup' to make the needed symlinks.


#!/bin/bash
##---------------------------------------------------------------------------
##
#
#	Script:	'sc' - systemctl shortener
#	Author:	Brian Lowe
#	Date:	December 2015
#
#	'sc' implements a series of shortcuts for the 'systemctl' command (and
#	one for 'journalctl') via a series of symlinks in /usr/local/bin:
#
#	Symlink	Runs
#	 sc 	systemctl
#	 scs	systemclt start
#	 scr	systemclt restart
#	 sck	systemctl stop ('k' = 'kill')
#	 scl	systemctl reload ('l' = reLoad)
#	 scq	systemctl status ('q' = 'query')
#	 sct	systemctl status ('t' = 'sTaTus')
#	 scj	journalctl ('scj NAME' runs 'journalctl --catalog --unit=NAME')
#	 sch	(provides help for the 'sc' command)
#
##---------------------------------------------------------------------------
##
#
#	Setup:
#	 1. Put this script into /usr/local/bin
#	 2. Run the 'sc --setup' to set up the symlinks
#
##---------------------------------------------------------------------------
##
#	BUUS: This script is part of Brian's Useful Utilities Set
##---------------------------------------------------------------------------
##
PROGRAM_TO_RUN='systemctl'
COMMAND=''

BASENAME="$(basename $0)"
if [ $BASENAME = 'sc' -a "$1" = '--setup' ]
then
	DIRNAME=$(dirname $0)
	[ "$DIRNAME" ] && cd $DIRNAME
	echo -n "Setting up:"
	awk '/^#[[:space:]]*sc[a-z][[:space:]]/{print $2}' sc | while read X
	do
		[ -L $X ] || (echo -n " $X,"; ln -s sc $X)
	done
	echo " done"
	exit
fi

case $BASENAME in
  scs)	COMMAND='start';;
  sck)	COMMAND='stop';;
  scr)	COMMAND='restart';;
  scl)	COMMAND='reload';;
  scq)	COMMAND='status';;
  sct)	COMMAND='status';;
  scj)	PROGRAM_TO_RUN='journalctl';;
  sch)	COMMAND='sc-show-help';;
esac

#--- Handle a 'help' command (-h, --help)
if [ "$1" = '-h' -o "$1" = "--help" ]
then
	if [ ! "$COMMAND" ]
	then
		echo "Running 'systemctl --help' -- run 'sch' for help on 'sc' 
itself"
		sleep 1
	fi
fi

#--- Handle 'scj UNIT-NAME', where the user wants the journal for a given unit
if [ "$PROGRAM_TO_RUN" = 'journalctl' -a "$1" -a "${1:0:1}" != '-' ]
then
	COMMAND="--catalog --unit=$1"
	shift
fi

#--- Handle 'sc-show-help' (help for the 'sc command'
if [ "$COMMAND" = 'sc-show-help' ]
then
	awk '
		/#[[:space:]]*sch/ { exit }
		/#[[:space:]]*[[:alnum:]]+:/ { sw = 1 }
		sw == 1 && /^#[[:space:]]*$/ { sw = 2 }
		sw == 2 { match($0, /^#\t(.*)/, a); print a[1]  }
	' $0
	echo
	exit
fi

echo -n " >> $PROGRAM_TO_RUN $COMMAND"; [ "$1" ] && echo -n " $@"; echo
exec $PROGRAM_TO_RUN $COMMAND "$@"



More information about the Roundtable mailing list