1

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.

  • 1
    Does it need to be systemd? You normally do this sort of thing at the user-level: you just have the tool run after login. Is systemd an absolute requirement for you? – terdon Oct 30 '20 at 14:23
  • @terdon No it's not at all a requirement. when searching for methods online for having programs run from boot, systemd was described as a new method that is being widely adopted. I also appreciated the learning experience/ getting my head deeper into the architecture. That being said I'm happy with any method that will allow a user to plug the Pi in, log onto the VNC and see the terminal widow printing away.. – Slazl Oct 30 '20 at 14:44
  • Does the program require to write to or interact through a terminal? Can't the usual log file be enough? – A.B Oct 30 '20 at 14:52
  • Hello @A.B Yes it is preferable to be writing to the terminal- The users will not be technically minded and I would prefer them to be able to just VNC on without control and see the current status that will be printed. – Slazl Oct 30 '20 at 15:00
0

I've managed to get the functionality I was after.

I edited my script in /usr/local/bin/ (procamStartup.sh) to now read the following:

#!/bin/bash
DISPLAY=:0 xterm -hold -e /home/pi/Dev/Procam

and have edited my service file in /etc/systemd/system/ (procamStartup.service) to:

[Unit]
 Description=Procam Startup Script
 After=graphical.target

 [Service]
 Environment=XAUTHORITY=/home/pi/.Xauthority
 DISPLAY=:0
 User=pi
 Type=idle
 ExecStart=/usr/local/bin/procamStartup.sh
 User=pi
 Environment=XAUTHORITY=/home/pi/.Xauthority

 [Install]
 WantedBy=graphical.target

After reloading with sudo systemctl daemon-reload and a sudo reboot I now connect to VNC to see the program in its own terminal window already running away.

Great news on a Friday :)

Have a good weekend everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.