2

Hello i am trying to open two shell scripts on reboot.

Crontab My crontab is the following

@reboot sh /home/pi/Desktop/autoStart.sh

autoStart.sh in this shell script is the following

#!/bin/sh
cd /home/pi/Desktop
sudo xterm -e ./startNodeServer.sh &
sudo xterm -e ./startPythonScanner.sh

Error The error i get is

xterm: Xt error: Can't open display: %s
xterm: DISPLAY is not set

Thanks for the help

2

The answer is in the error message. DISPLAY is not set. Cron is not a part of your desktop environment, so it does not know where to display the xterm.

If you open an xterm normally, and ask echo $DISPLAY, it will tell you soemthing like

:0.0

That is the display identifier where windows would be displayed. Unfortunately, you cannot just put

DISLPLAY=':0.0'

in your script because

  • there is a form of security that prevents opening windows on the display of another login (xauth)
  • your X server may not be running yet when the cronjob is executed.

The solution depends on what kind of window manager you are running. If default raspian, it would probably be something like:

  • auto-login pi (via raspi-config)
  • put @/home/pi/Desktop/autoStart.sh in /home/pi/.config/lxsession/LXDE-pi/autostart
| improve this answer | |
0

You CAN NOT start GUI scripts with crontab - just boot to the GUI and use .config/lxpanel/LXDE-pi/autostart to start your scripts.

To run an application you simply add it on a line starting with a '@' character.

@lxterminal
| improve this answer | |
  • Well, not completely true. If you are logged-in in the GUI, do an xhost + and put * * * * * /usr/bin/xterm -display :0.0 -e sleep 5 in your crontab, you'll see that you can start GUI stuff from crontab. But xhost + is of course bad practice. – Ljm Dullaart Oct 26 '19 at 11:09
  • @LjmDullart You are right, that the categorical "CAN NOT" is not correct. But in this special case: Can you be sure that the crontab @reboot is executed after the XServer has started. If not, there is no XServer that can display the xterm. And because @reboot is sometimes used to initialize e.g. special network drivers, I doubt that the X-Server is running when the script is performed. Your answer is helpful (uv), but the comment might confuse the OP. – Peter Paul Kiefer Oct 26 '19 at 11:47
0

Starting programs and scripts are controlled by systemd with Unit files. With it you can define the needed environment to run the program. But because we know nothing about your scripts and what they are doing I can only suggest a general template to start a script with GUI. Create a new service with:

rpi ~$ sudo systemctl --force --full edit node-server.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Node Server
Wants=graphical.target
After=graphical.target

[Service]
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/pi/.Xauthority
WorkingDirectory=/home/pi/Desktop
ExecStart=/usr/bin/xterm -c /home/pi/Desktop/startNodeServer.sh

[Install]
WantedBy=graphical.target

Enable and start the new service with:

rpi ~$ sudo systemctl enable --now node-server.service

Check with:

rpi ~$ systemctl status node-server.service

Create and start just a second python-scanner.service for startPythonScanner.sh in the same way. Maybe you have to define Wants=node-server.service and After=node-server.service in its [Unit] section. If it doesn't work you have to look what additional environment settings are needed or you may elaborate your question.

| improve this answer | |

Your Answer

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