Skip to content

Commit 714b9a2

Browse files
henrybarretogustavosbarreto
authored andcommitted
fix(install): uninstall systemd service and remove files on error
1 parent 3ad8b0d commit 714b9a2

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

install.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,29 @@ standalone_install() {
134134
echo "🚀 Starting ShellHub system service..."
135135

136136
$SUDO cp $TMP_DIR/shellhub-agent.service /etc/systemd/system/shellhub-agent.service
137-
$SUDO systemctl enable --now shellhub-agent || { rm -rf $TMP_DIR && echo "❌ Failed to enable systemd service."; exit 1; }
137+
138+
# NOTE: As we need to check if the service is running to indicate it was installed correctly, we need to copy the
139+
# values to install directory before enable it, to a correctly check the status.
140+
$SUDO rm -rf $INSTALL_DIR
141+
$SUDO mv $TMP_DIR $INSTALL_DIR
142+
143+
uninstall() {
144+
echo "Please check the logs with the command:"
145+
echo "journalctl -f -u shellhub-agent"
146+
echo ""
147+
echo "❗ Uninstalling ShellHub agent..."
148+
$SUDO rm -rf $TMP_DIR
149+
$SUDO rm -rf $INSTALL_DIR
150+
$SUDO rm /etc/systemd/system/shellhub-agent.service
151+
}
152+
153+
$SUDO systemctl enable --now shellhub-agent || { uninstall && echo "❌ Failed to enable systemd service."; exit 1; }
138154

139155
trap 'echo "❗ Interrupted. Disabling shellhub-agent..."; $SUDO systemctl disable --now shellhub-agent; exit 1' INT
140156

141157
echo "🔍 Checking service status..."
142158
echo "Please wait for the service to start. This may take a few seconds."
143159
echo "Press Ctrl+C to cancel the installation."
144-
echo ""
145-
echo "If it takes too long, please check the logs with the command:"
146-
echo "journalctl -f -u shellhub-agent"
147160

148161
timeout 15s sh -c '
149162
journalctl -f -u shellhub-agent --since "$(systemctl show -p ActiveEnterTimestamp shellhub-agent | cut -d= -f2)" | while read -r line; do
@@ -163,15 +176,15 @@ standalone_install() {
163176
echo "❌ Timeout: Service took too long to start."
164177
echo "Disabling shellhub-agent service..."
165178
$SUDO systemctl disable --now shellhub-agent
179+
uninstall
166180
exit 1
167181
elif [ $exit_code -eq 2 ]; then
168182
echo "Disabling shellhub-agent service..."
169183
$SUDO systemctl disable --now shellhub-agent
184+
uninstall
170185
exit 1
171186
fi
172187

173-
$SUDO rm -rf $INSTALL_DIR
174-
$SUDO mv $TMP_DIR $INSTALL_DIR
175188
$SUDO rm -rf $TMP_DIR
176189
}
177190

0 commit comments

Comments
 (0)