rc.d 1.73 KB
Newer Older
Sébastien Luttringer's avatar
Sébastien Luttringer committed
1
2
#!/bin/bash

3
NEED_ROOT=0 # this script can be run without be root
Sébastien Luttringer's avatar
Sébastien Luttringer committed
4
5
6
7
. /etc/rc.conf
. /etc/rc.d/functions

usage() {
Sébastien Luttringer's avatar
Sébastien Luttringer committed
8
9
	local name=${0##*/}
	cat >&2 << EOF
Sébastien Luttringer's avatar
Sébastien Luttringer committed
10
11
12
13
14
15
16
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
17

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

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

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

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

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