Launch application after starting display manager with script

I’m trying to make a script which stops the display manager service, starts it again, and then launches an application. The problem is that no matter what I try I can’t launch any applications after starting the display manager service. I believe that the environment variables related to the xsession are not being given to the running script, and so the applications fail to run.

My current script is this:

#!/usr/bin/env bash

if [[ $# -gt 0 ]]; then 
if [[ $stage -eq 0 ]]; then
    # switches to root since user scripts seem to be killed when the display manager service is stopped
    sudo -b windows 1
if [[ $stage -eq 1 ]]; then
    systemctl stop display-manager.service
    systemctl start display-manager.service
    # sleep to give me time to log into sddm
    sleep 25
    # switch back to user
    sudo -u cowsociety -b windows 2
if [[ $stage -eq 2 ]]; then
    firefox &> /home/cowsociety/Documents/log.txt

the log file:

Authorization required, but no authorization protocol specified

Error: cannot open display: :0

I think that you have to launch a new shell instance that has the graphical environment variables updated. I don’t really know much about shell variables, but I do know they normally don’t update in the background and you normally have to source something or start a new shell.

I think you can just do /usr/bin/env bash -c ‘firefox’. This post has good talk about login shells. I think the second answer might also be useful to find out:

If you want to be extra hacky, you could try DISPLAY:=0 before launching ff.

Idk why the link is showing that other answer! Here is the link in parts