serv-status 2.52 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2013, 2016  Etersoft
# Copyright (C) 2012, 2013, 2016  Vitaly Lipatov <lav@etersoft.ru>
5
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9 10 11 12 13
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18 19
#

20
# FIXME: sudo ask password, but we do not print command before
21 22
is_service_running()
{
23 24
	local SERVICE="$1"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
25
	case $SERVICETYPE in
26
		service-chkconfig|service-upstart)
27
			if is_anyservice $1 ; then
28 29 30
				$SUDO anyservice $1 status >/dev/null
				return
			fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
31 32
			$SUDO service $1 status >/dev/null
			;;
33
		service-initd|service-update)
34
			$SUDO $INITDIR/$1 status >/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
35 36
			;;
		systemd)
37
			$SUDO systemctl status $1 >/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
38
			;;
39 40 41
		runit)
			$SUDO sv status "$SERVICE" >/dev/null
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
42
		*)
43
			fatal "Have no suitable command for $SERVICETYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
44 45
			;;
	esac
46 47
}

48
# FIXME: sudo ask password, but we do not print command before
49 50
is_service_autostart()
{
51 52
	local SERVICE="$1"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
53
	case $SERVICETYPE in
54
		service-chkconfig|service-upstart)
55 56
			if is_anyservice $SERVICE; then
				$ANYSERVICE $SERVICE isautostarted
Vitaly Lipatov's avatar
Vitaly Lipatov committed
57
				return
58
			fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
59

60
			# FIXME: check for current runlevel
61
			LANG=C $SUDO chkconfig $1 --list | grep -q "[35]:on"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
62
			;;
63
		service-initd|service-update)
64
                        test -L $(echo /etc/rc5.d/S??$1)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
65 66
			;;
		systemd)
67
			$SUDO systemctl is-enabled $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
68
			;;
69 70 71
		runit)
			test -L /var/service/$SERVICE
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
72
		*)
73
			fatal "Have no suitable command for $SERVICETYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
74 75
			;;
	esac
76 77 78 79
}

serv_status()
{
80
	is_service_autostart $1 && echo "Service $1 is scheduled to run on startup" || echo "Service $1 will NOT run on startup"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
81

82 83 84
	local SERVICE="$1"
	shift

Vitaly Lipatov's avatar
Vitaly Lipatov committed
85
	case $SERVICETYPE in
86
		service-chkconfig|service-upstart)
87 88 89 90
			if is_anyservice $SERVICE ; then
				sudocmd anyservice $SERVICE status
				return
			fi
91
			sudocmd service $SERVICE status "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
92 93
			;;
		service-update)
94
			sudocmd $INITDIR/$SERVICE status "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95 96
			;;
		systemd)
97
			sudocmd systemctl status $SERVICE "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
98
			;;
99 100 101
		runit)
			sudocmd sv status "$SERVICE"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
102
		*)
103
			fatal "Have no suitable command for $SERVICETYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
104 105
			;;
	esac
106
}