If you want the file to run upon GUI login (and autologin can be set), then the answer will be different than if you want to replace the GUI login with a particular program. The least invasive will be for the program to run upon GUI login. Can you give more detail on how you want this to run versus what kind of login you expect will need to be manual versus automatic?
NOTE: Prior code may have failed because you didn’t give it a DISPLAY environment variable first…which requires a login first…and the script to run from the same user as the login.
I am only guessing, but I think you need to be sure the graphical desktop is already up and running. Then the environment variable “DISPLAY” needs to be set, e.g., for operating locally on that computer, “export DISPLAY=:0” and run by the same user as the desktop login runs as (so for example your script may need to run sudo).
To summarize, you cannot use the GPU unless there is an environment set up for the user who runs the program (an actively logged in desktop). Part of this is needing a context for the GPU (the login), the other part is security (a user is only allowed to execute a program on a context they own…running CUDA or GPU type code from one user while the GPU is owned by someone else is not allowed…consider using sudo). The DISPLAY environment variable tells the program which login session to associate with (a way to get a context).
Consider autologin as a particular user, and having that login be the trigger to running the program. Then, if you need this to run without actually having a monitor, consider a virtual desktop (which pretends to have a monitor but is purely in software…this allows a context which is associated with a user, thus DISPLAY can be set). For an example of virtual desktops see:
[url]http://ubuntuhandbook.org/index.php/2016/07/remote-access-ubuntu-16-04/[/url]