rc.d 1.67 KB
Newer Older
Sébastien Luttringer's avatar
Sébastien Luttringer committed
1
2
3
4
5
6
#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

usage() {
Sébastien Luttringer's avatar
Sébastien Luttringer committed
7
8
	local name=${0##*/}
	cat >&2 << EOF
Sébastien Luttringer's avatar
Sébastien Luttringer committed
9
10
11
12
13
14
15
usage: $name <action> <daemon> [daemon] ...
       $name list [started|stopped]
       $name help

<daemon> is the name of a script in /etc/rc.d
<action> can be a start, stop, restart, reload, status, ...
WARNING: initscripts are free to implement or not the above actions.
Sébastien Luttringer's avatar
Sébastien Luttringer committed
16

Sébastien Luttringer's avatar
Sébastien Luttringer committed
17
e.g: $name list
Sébastien Luttringer's avatar
Sébastien Luttringer committed
18
     $name list started
Sébastien Luttringer's avatar
Sébastien Luttringer committed
19
20
     $name help
     $name start sshd gpm
Sébastien Luttringer's avatar
Sébastien Luttringer committed
21
EOF
22
	exit 1
Sébastien Luttringer's avatar
Sébastien Luttringer committed
23
24
}

25
(( $# < 1 )) && usage
Sébastien Luttringer's avatar
Sébastien Luttringer committed
26

27
declare -i ret=0
Sébastien Luttringer's avatar
Sébastien Luttringer committed
28
29
30
31
32
case $1 in
	help)
		usage
		;;
	list)
33
		shift
Sébastien Luttringer's avatar
Sébastien Luttringer committed
34
35
36
37
38
		cd /etc/rc.d/
		for d in *; do
			have_daemon "$d" || continue
			# print running / stopped satus
			if ! ck_daemon "$d"; then
39
				[[ "$1" == stopped ]] && continue
40
				printf "${C_OTHER}[${C_DONE}STARTED${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
41
			else
42
				[[ "$1" == started ]] && continue
43
				printf "${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
44
45
46
			fi
			# print auto / manual status
			if ! ck_autostart "$d"; then
47
				printf "${C_OTHER}[${C_DONE}AUTO${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
48
			else
49
				printf "${C_OTHER}[${C_FAIL}    ${C_OTHER}]"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
50
			fi
Sébastien Luttringer's avatar
Sébastien Luttringer committed
51
			printf " ${C_CLEAR}$d\n"
Sébastien Luttringer's avatar
Sébastien Luttringer committed
52
		done
53
	;;
Sébastien Luttringer's avatar
Sébastien Luttringer committed
54
	*)
55
56
		# check min args count
		(( $# < 2 )) && usage
Sébastien Luttringer's avatar
Sébastien Luttringer committed
57
58
		action=$1
		shift
59
60
		# set same environment variables as init
		runlevel=$(/sbin/runlevel)
Dave Reisner's avatar
Dave Reisner committed
61
62
63
64
65
		ENV=("PATH=/bin:/usr/bin:/sbin:/usr/sbin"
			"PREVLEVEL=${runlevel%% *}"
			"RUNLEVEL=${runlevel##* }"
			"CONSOLE=${CONSOLE:-/dev/console}"
			"TERM=$TERM")
66
		cd /
Sébastien Luttringer's avatar
Sébastien Luttringer committed
67
		for i; do
68
			if [[ -x "/etc/rc.d/$i" ]]; then
69
				env -i "${ENV[@]}" "/etc/rc.d/$i" "$action"
70
			else
71
				printf "${C_OTHER}:: ${C_FAIL}Error: ${C_DONE}Daemon script $i does not exist.\n"
72
			fi
73
			(( ret += !! $? ))  # clamp exit value to 0/1
Sébastien Luttringer's avatar
Sébastien Luttringer committed
74
		done
75
	;;
Sébastien Luttringer's avatar
Sébastien Luttringer committed
76
77
esac

78
exit $ret
Sébastien Luttringer's avatar
Sébastien Luttringer committed
79

80
# vim: set ts=2 sw=2 ft=sh noet: