I am trying to run a program on Ubuntu MATE 20.04 that needs to have a graphical output to function. Sometimes I will attach a monitor and sometimes I will not. I would like to configure my computer(a raspberry pi 4) so that it will boot into MATE with or without a monitor.

For background, this program is a radio scanner software written in java and I want the be able to have the program startup at boot. It requires a graphical output in order to function. I did not write the program and trying to tackle launching it outside of X is beyond my capabilities.

Currently I have it set to start as a startup program inside of the MATE desktop and that is working fine when there is a monitor.

I don't care if the desktop is running when there isn't a monitor, system resources are less important to me than getting the program to run.

It is also acceptable to run the program on a "Fake" X session however I need to prevent that from happening when a monitor is attached. This could also be handled by killing the process manually (but preferably automatically) once the desktop has launched. Again, this is less important to me than getting it to work in the first place.

EDIT: the purpose of the exercise is to get the program to run if I plug in the pi and let it boot straight to the program scanning the air for radio(police etc) AND playing it through a speaker.


zach@zach-MATE20:~$ sudo systemctl enable rc-local.service
[sudo] password for zach: 
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

I suggest you to have your Java program always running on a very light virtual desktop environment that can be accessed via VNC from the Raspberry Pi (RPi) itself or from another computer through the network. This way it is irrelevant if there is a monitor plugged in the RPi.

Setting up the RPi to run the Java program

We are going to use Xvfb and Fluxbox to provide us with a virtual desktop and "x11vnc" to provide access to the virtual desktop. From Xvfb's Debian package description:

Xvfb provides an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory.

Install the necessary packages

sudo apt install x11vnc xvfb fluxbox x11-apps

I assume that RPi's default user (pi) will be used to run the Java program.

Script to run the Java program

Log on the RPi via SSH or its physical console (with a real monitor and keyboard).

Create the script file /home/pi/radio_scanner.sh with the following content:

export DISPLAY=:1
Xvfb $DISPLAY -screen 0 1024x768x16 &
fluxbox &
x11vnc -display $DISPLAY -bg -forever -nopw -quiet -xkb
xclock &  # just for testing, may remove this line
java -jar some_file.jar & # you program

Make the script executable:

chmod +x /home/pi/radio_scanner.sh

Run the script as a background process, redirecting its output to files:

~pi/radio_scanner.sh > ~pi/log_radio_scanner.txt 2> ~pi/errors_radio_scanner.txt &

Optional minimum security (password protection)

If you want to protect the VNC access with a password, set it first:

x11vnc -storepasswd

And then, on the script, replace the line executing x11vnc with

x11vnc -display $DISPLAY -bg -forever -usepw -quiet -xkb

Viewing the Java program running

From the RPi console or another computer accessible via network, connect to the RPi using a VNC client, mine is tigervnc-viewer (Linux).

vncviewer RPI_IP_ADDRESS

You should now see the Fluxbox Desktop with the Java program and the xclock app running in it.

Automatizing the Java program launch

For the Java program to always run when the RPi boots up, try one of the following two options to do that.

1) Use crontab

Probably the best option for Ubuntu Mate.

Edit the crontab for your user (pi) and schedule the program to run during boot. In the shell, execute:

crontab -e

A text editor will appear, add the following line at the end

@reboot /home/pi/radio_scanner.sh

2) Use rc.local

Insert the line bellow into the file /etc/rc.local. Be careful to leave the "exit 0" command as the last line on that file!

su - pi --command=/home/pi/radio_scanner.sh > ~pi/log_radio_scanner.txt 2> ~pi/errors_radio_scanner.txt &
  • Very Very close. The scanner program will start and run when I SSH in and run the radio_scanner.sh. However there isn't a /etc/rc.local – Zach Nov 13 '20 at 2:38
  • I tested this solution on my RPi running Raspbian and it has the traditional /etc/rc.local file. What distro are you using on your RPi? – Geraldo Nov 13 '20 at 18:00
  • Try on thing: as root, create /etc/rc.local, put in it the command in my answer and with exit 0 as the last line of the file. Make it executable: chmod +x /etc/rc.local. Reboot and see what happens. – Geraldo Nov 13 '20 at 18:08
  • The distro is Ubuntu MATE. – Zach Nov 13 '20 at 22:25
  • 1
    I have added to the answer another option to run the program. Check it. – Geraldo Nov 14 '20 at 18:47

You can use xvfb-run:

xvfb-run is a wrapper for the Xvfb(1x) command which simplifies the task of running commands (typically an X client, or a script containing a list of clients to be run) within a virtual X server environment.

If your program/script is /usr/local/bin/radio-scanner you can run it directly with:

xvfb-run /usr/local/bin/radio-scanner

If you ever need to access that server, you could use x11vnc and a vnc viewer. It is a bit out of the scope of your question but here it goes for local viewing using x11vnc+xvncviewer . Let's use xclock for example:

# run insided X server number 99, specify the auth file
xvfb-run -n 99 -f /tmp/xvfb-auth -- xclock -digital -update 1

Then, when you need to see the current screen:

# connect x11vnc to it

x11vnc -display :99 -N -auth /tmp/xvfb-auth

# connect a vnc client to display :99

xvncviewer :99

I had the same situation and I resolved it quickly installing a x2go server and a x2go client.




I tried other solutions like vnc but it was too slow ^^'

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.