-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisable_extra_env_for_service_linux.sh
More file actions
executable file
·116 lines (98 loc) · 2.5 KB
/
Copy pathdisable_extra_env_for_service_linux.sh
File metadata and controls
executable file
·116 lines (98 loc) · 2.5 KB
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
#!/bin/sh
#####################################################################################
## The script for disable extra env for specified systemd service.
## Service name specified as script argument (without any suffix, only service name).
## Linux version.
##
## Version 1.0
## Written by Y.Voinov (C) 2026
#####################################################################################
# Variables
# Drop-in directory
DROP_IN_DIR="/usr/lib/systemd/system"
# Drop-in extra env
CONF_EXTRA_ENV_FILE="mt_extra_env.conf"
# Subroutines
usage_note()
{
echo "The script for disable extra env for specified systemd service."
echo "Must be run as root."
echo "Usage: `basename $0` <service_name> [options]"
echo "Options:"
echo " -h, -H, --help show this help"
echo "Example (completely disable extra env): `basename $0` apache2"
exit 0
}
check_os()
{
if [ "`uname`" != "Linux" ]; then
echo "ERROR: Unsupported OS."
exit 1
fi
}
check_root()
{
if [ -z "`id | grep 'uid=0(root)'`" ]; then
echo "ERROR: Must be run as root."
exit 2
fi
}
check_service()
{
if [ ! -z "`systemctl status $SERVICE_NAME | grep 'could not be found.'`" ]; then
echo "ERROR: Service $SERVICE_NAME could not be found."
exit 3
fi
}
disable_drop_in()
{
drop_in_file_name=$1
if [ -d $DROP_IN_DIR/$SERVICE_NAME.service.d ]; then
echo "Directory $DROP_IN_DIR/$SERVICE_NAME.service.d found."
if [ -f $DROP_IN_DIR/$SERVICE_NAME.service.d/$drop_in_file_name ]; then
rm -f $DROP_IN_DIR/$SERVICE_NAME.service.d/$drop_in_file_name
echo "File $DROP_IN_DIR/$SERVICE_NAME.service.d/$drop_in_file_name removed."
rmdir $DROP_IN_DIR/$SERVICE_NAME.service.d
if [ ! -d $DROP_IN_DIR/$SERVICE_NAME.service.d ]; then
echo "Directory $DROP_IN_DIR/$SERVICE_NAME.service.d removed."
fi
fi
else
echo "ERROR: Directory $DROP_IN_DIR/$SERVICE_NAME.service.d does not exists."
exit 4
fi
}
# Main
if [ -z $1 ]; then
usage_note
fi
SERVICE_NAME=""
while [ $# -gt 0 ]; do
case "$1" in
-h|-H|--help)
usage_note
;;
*)
# Accumulate to one string
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=$1
else
SERVICE_NAME="$SERVICE_NAME $1"
fi
;;
esac
shift
done
if [ -z $SERVICE_NAME ]; then
usage_note
fi
check_os
check_root
check_service
if [ ! -z $SERVICE_NAME ]; then
disable_drop_in $CONF_EXTRA_ENV_FILE
fi
systemctl daemon-reload
systemctl restart $SERVICE_NAME
echo "Completed for $SERVICE_NAME."
exit 0