-
Notifications
You must be signed in to change notification settings - Fork 0
/
journalctl
executable file
·161 lines (143 loc) · 2.7 KB
/
journalctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
SYSTEMCTL_PY="systemctl3.py"
get_cmd(){
local cmd="$1"
command -v "$cmd" || printf "${0%/*}/$cmd"
}
SYSTEMCTL=$(get_cmd $SYSTEMCTL_PY)
unit=""
follow=false
lines=""
no_pager=false
system=false
user=false
root=""
x=false
out() { printf "$1 $2\n" "${@:3}"; }
error() { out "${0##*/}: error:" "$@"; } >&2
die() { error "$@"; exit 1; }
ignore_error() {
"$@" 2>/dev/null
return 0
}
usage() {
cat << EOF
usage: ${0##*/} [-h] -u unit [-f] [-n num] [--no-pager]
[--system] [--user] [--root path] [-x]
EOF
}
usage_verbose(){
usage
cat << EOF
options:
-h, --help show this help message and exit
-u unit, --unit unit Systemd unit to display
-f, --follow Follows the log
-n num, --lines num Num of lines to display
--no-pager Do not pipe through a pager
--system Show system units
--user Show user units
--root path Use subdirectory path
-x Switch on verbose mode
EOF
}
die_arg(){
local arg=$1
shift
if [ -z "$@" ] ;then
local die0="$arg"
else
local die0="$@"
fi
case $arg in
-*|"")
usage
die "${die1}${die0}${die2}"
;;
*)
;;
esac
}
expected_arg(){
die1="argument "
die2=": expected one argument"
die_arg $@
}
required_arg(){
die1="the following arguments are required: "
die2=""
die_arg $@
}
prepare(){
[ ! -L "/tmp/run" ] && ln -sfr /tmp /tmp/run
[ ! -x $SYSTEMCTL ] && die "${0##*/}: command not found: $SYSTEMCTL_PY"
[ ! -x /usr/bin/python3 ] && die "${0##*/}: command not found: python3"
}
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
usage_verbose
exit 0
;;
-u|--unit)
shift
expected_arg $1 "-u/--unit"
unit=$1
;;
-f|--follow)
follow=true
;;
-n|--lines)
shift
lines=$1
;;
--no-pager)
no_pager=true
;;
--system)
system=true
;;
--user)
user=true
;;
--root)
shift
root=$1
;;
-x)
x=true
;;
*)
error "argument $1: ignored explicit argument\'$1\'"
shift
;;
esac
shift
done
required_arg "$unit" "-u/--unit"
cmd=("$SYSTEMCTL" "log" "$unit")
if [ "$follow" = true ]; then
cmd+=("-f")
fi
if [ -n "$lines" ]; then
cmd+=("-n" "$lines")
fi
if [ "$no_pager" = true ]; then
cmd+=("--no-pager")
fi
if [ "$system" = true ]; then
cmd+=("--system")
elif [ "$user" = true ]; then
cmd+=("--user")
fi
if [ -n "$root" ]; then
cmd+=("--root" "$root")
fi
if [ "$x" = true ]; then
cmd+=("-vvv")
fi
prepare
[ -r "/var/log/journal/$unit.log" ] || \
[ -r "/var/log/journal/$unit.service.log" ] && \
ignore_error exec "${cmd[@]}"
exec "${cmd[@]}"