First off apologies for the simple question- there is more to it that I will explain below.
I am new to Linux style systems and evidently I have A LOT to learn.
With that said...
My System
I am running a program on a Raspberry Pi 4 Model B. My operating system is Raspbian GNU/Linux 10 (buster).
What I Have
I have produced a working program in C++ that is stored in home/pi/Dev
. The program has no GUI but prints information into the terminal.
The program infinitely loops (using an empty for
loop).
When launching the program through Geany (an editor on the pi), I can make and run the program which opens a new terminal window and prints the output there.
When clicking on the executable file in the folder I'm asked to choose between 'Execute' and 'Execute in terminal'.
Selecting to 'Execute' seemingly has no effect (I have looked for the process in the task manager?)
Selecting to 'Execute in Terminal' brings up an error:
Failed to execute child process “xterm” (No such file or directory)
When using the terminal and running ./myprogram
The program runs, printing the output in the already open terminal window.
What I Want
I would like the program to run automatically on startup.
The Pi will be used 'headless' but with the graphical interface enabled so I can see the desktop using VNC; when I open the VNC connection I would like a terminal to be running and displaying the program output.
What I Have Tried
I have been attempting to use systemd to launch the program.
At first I tried launching the program directly but got absolutely nowhere; I couldn't find any good info or examples online and most examples were using bash or Python scripts... If you can't beat them join them...
So next I created (copied) a simple bash script in usr/local/bin/ called procamStartup.sh
:
#!/bin/bash
echo test
cd /home/pi/Dev/
./Procam
and when running this script using ./procamStartup.sh
, "test" was printed in the terminal and then the program started printing below that.. so the script in my eyes is functional.
Next I created a service
file in /etc/systemd/system/
called procamStartup.service
:
[Unit]
Description=Procam Startup Script
After=graphical.target
[Service]
Type=idle
ExecStart=/usr/local/bin/procamStartup.sh
[Install]
WantedBy=graphical.target
I chose Type = idle
so the system would wait for all processes to finish before launching.
I chose WantedBy=graphical.target
so the program would only launch after the graphical interface has started.
I set execute permissions for both script and service folders using:
sudo chmod +x /usr/local/bin/procamStartup.sh
sudo chmod +x /etc/systemd/system/procamStartup.service
I next told systemd to reload the files:
sudo systemctl daemon-reload
and then enabled the script to be ran at boot:
sudo systemctl enable procamStartup.service
after that I ran sudo reboot
.
Continued
So.. before sending this post I followed my own instruction and at the end ran:
sudo systemctl status procamStartup.service
Which reported the service was successfully running and printing the program output into a log.
This is semi-ok: I can use the Pi as intended as the program is running- just with no output showing by default.
So to redo my question:
Is it possible to have systemd open a program running in a clean terminal automatically after startup?
I also think I need to change the service file; since its running a script that runs the program I might be launching several instances of the same program?
Thank you for reading.
Continued continued
I have now installed xterm
I have changed my script to the following:
#!/bin/bash
xterm -hold -e /home/pi/Dev/Procam
When running this script through the Pi GUI terminal, I get exactly what I want: a new window outputting correctly.
When running this from startup or through SSH I get the following errors:
xterm: Xt error: Can't open display: %s
xterm: DISPLAY is not set
I'm currently looking into resolving this.