0

I have made a python application for raspberry and it needs to run into a terminal. That's totally necessary.

To do that I wrote two bash files, run_.sh and launcher.sh.

run_.sh execute the python application, while launcher.sh invoke a lxterminal and execute run_.sh

When I manually run launcher.sh with the command:

sh launcher.sh

It works well. The terminal opens, the application runs and everything is ok. But when launcher.sh have to be executed in raspberry's startup, it doesn't work.

I'm using a raspberry pi 3 Raspbian: kernel version 4.14

Method of execute on startup:

sudo crontab -e
@reboot sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1

The startup's method is working. That's no the problem. The problem is that the commando that open the xlterminal doesn't work on startup. It works, when I excute manually, but not automatically.

This is what is write into my bash files.

launcher.sh:

lxterminal --command="/bin/bash -c '/home/pi/run_.sh; /bin/bash'"

run_.sh

python3 application.py

I expect that a xlterminal appears and execute the commands into run_.sh. But It show me this error:

(lxterminal:1005): Gtk-WARNING **: cannot open display:
1

You can't run something at startup that should open a window, because the X Server is not yet available.

Additionally, cron jobs don't normally have access to the X Server.

However, it is possible to connect to the X Server if you prepare the correct environment.

Normally, what you need are the DISPLAY, XAUTHORITY and XAUTHLOCALHOSTNAME variables. To verify that, run

env -i \
    DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY XAUTHLOCALHOSTNAME=$XAUTHLOCALHOSTNAME \
    /home/pi/bbt/launcher.sh

The line breaks are for better readability. The command env -i will run the command that follows with a clear environment, just with the values explicitly set on the command line.

If that works, you need to find out the correct values for those variables for your crontab, and add a sufficient delay for the X Server to start:

@reboot sleep 30; DISPLAY=:0 XAUTHORITY=... XAUTHLOCALHOSTNAME=... /home/pi/bbt/launcher.sh

As you probably don't use /home/pi/bbt/launcher.sh for something else, you can also add the commands to that file instead of the crontab line. This assumes you made the script executable.

  • Thanks for answer, Ralf. Your test commando works well, but I'm not sure what is XAUTHORITY and XAUTHLOCALHOSTNAME. Please, can you explain me? How can I find out these values? I tried with: reboot sleep 120; DISPLAY=:0 XAUTHORITY=$XAUTHORITY XAUTHLOCALHOSTNAME=$XAUTHLOCALHOSTNAME /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1 But it doesn't work. It show me the same error: (lxterminal:1005): Gtk-WARNING **: cannot open display: :0 Thanks for your support. – marlon valerio Jun 7 at 19:42
  • You can't use XAUTHORITY=$XAUTHORITY in the crontab, because that assumes that XAUTHORITY is already set, which is true in the shell, but not in the crontab. Use echo DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY XAUTHLOCALHOSTNAME=$XAUTHLOCALHOSTNAME in the shell to show the current values. – RalfFriedl Jun 7 at 20:41
  • Thanks a lot, Ralf. It works very well. – marlon valerio Jun 7 at 22:38
0

You can put your commands in ~/.bashrc and they'll be executed when you open a terminal.

  • Thanks for answer, ben. Yes that's an option, but I wan to automatically open a xlterminal and excute commands into it automatically. Do you know how to do that? – marlon valerio Jun 7 at 16:50
  • This is very bad advice. .bashrc is for configuring the shell and should not be used to run programs – Milliways Jun 7 at 23:37
0

My guess is the init level may not be set high enough yet for an X session. You might add a sleep 120 to the cron and possibly a nohup:

@reboot sleep 120;nohup /bin/sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1

(edit: I also fully qualified the /bin/sh but I doubt that's the issue)

  • Hi,Steve. Thanks for your support. I thought tha same, but that's not the solution. It still doesn't work. – marlon valerio Jun 7 at 19:45

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.