cat << EOF | sudo tee /etc/xdg/autostart/allowed_apps_daemon.desktop
[Desktop Entry]
Type=Application
Exec=/usr/local/sbin/allowed_apps_daemon.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=My Daemon
Comment=Start allowed apps daemon
EOF


cat << EOF | sudo tee /usr/local/sbin/allowed_apps_daemon.sh
#!/bin/bash
TARGET_USER=\$(whoami)
EXCLUDED_PIDS=\$(ps -u "\$TARGET_USER" -o pid= | sed 's/[[:space:]]//g')
EXCLUDED_COMMANDS=("APPLICATION_PROCESS_NAME1" "APPLICATION_PROCESS_NAME2" "APPLICATION_PROCESS_NAME3" )

while true; do
  # Get the list of running processes for the target user
  running_processes=\$(ps -u "\$TARGET_USER" -o pid=,cmd=)

  # Iterate over each process
  while read -r pid cmd; do
    # Check if the process should be excluded
    exclude=false

    # Check if the PID should be excluded
    for excluded_pid in \$EXCLUDED_PIDS; do
      if [[ \$pid == "\$excluded_pid" ]]; then
        exclude=true
        break
      fi
    done

    if [[ \$exclude == false ]]; then
      for excluded_cmd in "\${EXCLUDED_COMMANDS[@]}"; do
        if [[ \$cmd == "\$excluded_cmd" ]]; then
          exclude=true
          break
        fi
      done
    fi

    # Kill the process if it is not excluded
    if [[ \$exclude == false ]]; then
      echo "Killing process \$pid: \$cmd"
      kill "\$pid"
    fi
  done <<< "\$running_processes"

  # Pause for 25 seconds
  sleep 30
done
EOF

chmod 755 /usr/local/sbin/allowed_apps_daemon.sh
visudo_entry="ALL ALL=(ALL) NOPASSWD: /usr/local/sbin/allowed_apps_daemon.sh"
echo "$visudo_entry" >> /etc/sudoers
#Verify the sudoers file syntax
visudo -c

# Please reboot the Device once script is executed