Bare with me here as I am rather new to Linux systems.
I have a Phidgets Debian SBC4 running Stretch 9.x without a GUI interface, it's just the tty terminal.
I also have a program.exe that I have written in C# and run with mono. This program takes stdin input using Console.ReadKey() within a loop and then outputting messages via Console.Writeline(). Depending on what you input, the program toggles some LED lights on/off. Nothing fancy. On program start it just turns all lights on so you can visually see the program is running.
I am able to log into my SBC with root and do mono program.exe and everything works perfectly well. I am able to type input into the console/terminal and then get messages back and the LEDs toggle as expected. No Problem. Now I need this to auto start on boot.
My thinking was, on boot, log in automatically as root and then launch the mono program.exe. To do this I followed the advice in;
And the result was on boot it logs in as root automatically. All good. Now to launch the program.
I have tried using crontab -e and @reboot mono /blahblah/program.exe and this does indeed launch the program but it appears to launch in the background. So I can see the LEDs light up as expected but there is no relation between the tty terminal and the program. Whatever I type into the terminal does not get read by Console.ReadKey(). Likewise, no messages come back to the terminal.
I have also tried creating a service via systemd and using systemctl start/status etc. and this works on reboot but only for a second or two, then it stops. It prints messages to the console as expected so I think this would also allow input from the console.
But when checking systemctl status it shows loaded was success but active "inactive" and code=killed, signal=HUP
After a bunch of Googling I think this is related to my main program loop and Linux basically saying "I can't execute anything else due to this loop so ima kill it".
How can I get this program to run on boot without it being killed? I don't think I can change the code to remove the loop as it is vital to checking what was inputted and then switching the LEDs on/off
I have tried different combinations of settings in the service file under [Unit] and [Service] but nothing seems to help.
Any advice please would be appreciated.
Edit, I moved away from the service approach and tried adding a .sh script to the /etc/profile.d/myscript.sh and then in that script adding;
exec /usr/bin/mono /path/to/my/program.exe
And now when rebooting it logs in as root and runs the program and it works perfectly. Problem is though, now I can't kill the program as CTRL+C does not work. I added a way to exit my program in the C# code by just typing "exit" into the console but soon as the program ends Debian just logs back in as root and re-launches the program again.
I can do CTRL+ALT+F6 to open tty6 but logging in as root just launches the program again so I am unable to edit the program to make any changes should I wish to.
Is there a way to login without running the profile script?