Skip to content

Instantly share code, notes, and snippets.

@ck-on
Last active February 4, 2016 12:31
Show Gist options
  • Save ck-on/b7d5849787c8cdf398b5 to your computer and use it in GitHub Desktop.
Save ck-on/b7d5849787c8cdf398b5 to your computer and use it in GitHub Desktop.
script to enhance SERVICE command in CentOS 7 and avoid systemctl annoyances
#!/bin/bash
#
# script to enhance service command in CentOS 7 and avoid systemctl annoyances
# save to: /usr/local/bin/service then: chmod +x /usr/local/bin/service last: hash -d service
# this is a very unpolished work in progress, wtfpl free for any kind of use
#
service="/usr/sbin/service"
systemctl="/usr/bin/systemctl"
journalctl="/usr/bin/journalctl"
function linebreak { echo '------------------------------'; }
trap linebreak EXIT
linebreak
if [ "$#" -ne 2 ]
then
if [ "$@" == "reload" ]
then
$systemctl daemon-reload
echo -e "\e[0;33mreloaded systemd manager configuration\e[0m"
elif [ "$@" == "log" ]
then
$journalctl -n35 --full --no-pager
elif [ "$@" == "status" ]
then
systemctl list-units --no-pager | grep service | grep -v static | sort -k2 -k1 # unfinished
else
$service $@
fi
exit 0
fi
daemon="$1"
action="$2"
echo -e "\e[0;33m$action $daemon\e[0m"
if [ "$action" == "configtest" ]
then
if [ "$daemon" == "nginx" ]
then
nginx -t
exit 0
elif [ "$daemon" == "mysql" ]
then
help_out=`mysql --help 2>&1`; r=$?
if test "$r" != 0
then
echo "$help_out"
else
echo "MySQL Syntax OK"
fi
exit 0
fi
fi
if [ "$action" == "enable" ] || [ "$action" == "disable" ]
then
$systemctl $action $daemon.service
elif [ "$action" != "status" ]
then
stdbuf -i0 -o0 -e0 $service $daemon $action 2>&1 | sed -r -e "s/(unknown.*)/\x1B\[31;1m\1\x1B\[37;0m/gi"
fi
if [ "$action" != "--status-all" ] && [ "$action" != "configtest" ]
then
sleep 0.5
$systemctl status $daemon.service -n10 2>&1 | uniq -f 2 \
| sed -r -e "s/(failed|inactive|exited|dead\b|stopped|stopping|kill|killed|term)/\x1B\[31;1m\1\x1B\[37;0m/gi" \
-e "s/(active |run|running|starting|started)/\x1B\[32;1m\1\x1B\[37;0m/gi" \
-e "s/(status.*)/\x1B\[33;1m\1\x1B\[37;0m/gi"
linebreak
$journalctl _SYSTEMD_UNIT=$daemon.service -n9
fi
@bas00000009
Copy link

please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment