aboutsummaryrefslogtreecommitdiff
path: root/utils/sext
blob: b120929fe4427bad471f4fde3ff3af19f4c8aaa7 (plain)
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
#!/bin/bash

#Sext (Shutdown Exit)
# List of package managers to check for
package_managers=("emerge" "apt" "dnf" "pacman" "zypper")

# Set how often to check (in seconds)
check_interval=30

# Check if any package manager is running
is_package_manager_running() {
  for pm in "${package_managers[@]}"; do
    if pgrep -x "$pm" >/dev/null; then
      return 0 # Return 0 (true) if any package manager is running
    fi
  done
  return 1 # Return 1 (false) if no package manager is running
}

# Safely shutdown the system
safe_shutdown() {
  # Try using shutdown without sudo first
  if shutdown -h now; then
    echo "Shutdown initiated successfully using 'shutdown'."
    # If shutdown fails, try using poweroff
  elif poweroff; then
    echo "Shutdown initiated successfully using 'poweroff'."
    # If poweroff fails, try using sudo poweroff
  elif sudo poweroff; then
    echo "Shutdown initiated successfully with 'sudo poweroff'."
    # If both shutdown and poweroff fail, try with sudo shutdown
  elif sudo shutdown -h now; then
    echo "Shutdown initiated successfully with 'sudo shutdown'."
  else
    echo "Shutdown command failed. Please check your system configuration."
  fi
}

# Loop until no package manager process is running
while is_package_manager_running; do
  echo "Package manager is still running. Checking again in $check_interval seconds..."
  sleep "$check_interval"
done

# Once the process completes, initiate a safe shutdown
echo "Package manager has finished. Attempting to shutdown..."
safe_shutdown