norpi
Posts: 31
Joined: Wed Apr 08, 2015 4:50 pm

Run bash script after starting X

Wed Apr 08, 2015 5:01 pm

Hi
I would like to start a bash script or preferably a python script in a terminal window after I have booted into X.
The reason is that my project needs to auto start after plugging in power and booting the rpi without any other inputs.
I have tried crontab, rc.local and /etc/xdg/lxsession/LXDE/autostart without success.

Both scripts are located in /home/pi/Python_kode/Kattedetektor. My bash script is called autostart.sh and looks like this:
#!/bin/bash
echo "Running my python sript in another terminal window"
sleep 10
Exec=sudo lxterminal --command "sudo python3 /home/pi/Python_kode/Kattedetektor/kattdet.py"
$SHELL

The actual file I want to run is "kattdet.py" but I thought it would be easier to start it via crontab or rc.local if it was initiated from a bash script.

If I run "/home/pi/Python_kode/Kattedetektor/autostart.sh" from terminal it runs just fine. Also if I run "sudo python3 /home/pi/Python_kode/Kattedetektor/kattdet.py" that starts up directly like it should.

I find it strange that it is so hard to automatically start a script or python file after booting the rpi, one should think that this is a common used feature?

Can anyone help me?

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 5:32 pm

Hello.

Create this file, if do not already exist:

touch /home/pi/.config/lxsession/LXDE-pi/autostart

leafpad /home/pi/.config/lxsession/LXDE-pi/autostart

add the command you want to execute like this:

Code: Select all

@path_to_command
make sure command is executable.

if you want to run in terminal

Code: Select all

@xterm -hold -e path_to_command
-hold make xterm not closing after execution, so you can see if everything went well.

xterm is not installed, so

sudo apt-get install xterm

lxterminal has the -e option, but not the -hold. If don't need to monitor the command
just put @path_to_command to /home/pi/.config/lxsession/LXDE-pi/autostart

Save the file, logout, login.

This has been tested on rasp pi 2 with latest Raspian.
Last edited by troombatzia on Wed Apr 08, 2015 5:49 pm, edited 1 time in total.
English isn’t my first language, so please excuse any mistakes.

norpi
Posts: 31
Joined: Wed Apr 08, 2015 4:50 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 5:49 pm

Hi
Thanks for a quick and detailed reply.
When running sudo apt-get install xterm I get this message:

pi@raspberrypi ~ $ sudo apt-get install xterm
Leser pakkelister ... Ferdig
Skaper oversikt over avhengighetsforhold
Leser tilstandsinformasjon ... Ferdig
Pakken xterm er ikke tilgjengelig, men en annen pakke henviser til den.
Dette kan bety at pakken mangler, er utgått, eller bare finnes
tilgjengelig fra en annen kilde.

E: Pakken «xterm» har ingen installasjonskandidat

Which is Norwegian for "Package xterm is not available, but another package points to it, the package might be missing, has expired or is only available from another source" and finally "The package xterm has no installation candidate"

Do I need to add the source?

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 5:50 pm

no, try apt-get update then try again to install
English isn’t my first language, so please excuse any mistakes.

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 5:56 pm

Found now that there is an lxterm already installed (maybe a customized lx xterm) with same options of xterm. So if don't find xterm, try lxterm (not lxterminal).
English isn’t my first language, so please excuse any mistakes.

norpi
Posts: 31
Joined: Wed Apr 08, 2015 4:50 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 6:24 pm

Great, now it works :)
However I get another issue that I did not have when running the script from the terminal manually.
The bash script has a 30s timer, but upon launching the python script in a new terminal window, the bash script window outputs the following message:
(lxterminal:2554): GLib-GObject-WARNING **: Attempt to add property GtkSettings::gtk-button-images after class was initialised

Do you know what causes this? I could post this as a separate question on the forum but I think it might be related to the launching from LXDE-pi/autostart.

The python script does start but is has issues. It fails to load sound files which shall be played.
Do I need to run the python script in xterm also? I now do Exec=sudo lxterminal --command "sudo python3 /home/pi/Python_kode/Kattedetektor/kattdet.py" in the bash script.

Have a look at the screen dump attached.



The start of my python file:

import RPi.GPIO as GPIO
import time
import picamera
import shutil
import os
from datetime import datetime
from time import sleep
from subprocess import call

GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)

camera = picamera.PiCamera()

print ("Kattedetektor (CTRL+C for aa avslutte)")
time.sleep(2)
print ("Audio:")
audio1 = "omxplayer -o local intro.aac"
call ([audio1], shell=True)

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 7:32 pm

For the error: (lxterminal:2554): GLib-GObject-WARNING **:

I have the same error when I launch leafpad from terminal. You can safely ignore it. If it disturbes you, just replace

Code: Select all

lxterminal --command   
with

Code: Select all

lxterminal 2>/dev/null --command 
but only when your script will work perfectly, because 2>/dev/null hides error messages.

For the sound question: I don't know python, but as a first try, I would feed omxplayer with full path to sound file. As a second try, again xterm ;-)
Last edited by troombatzia on Wed Apr 08, 2015 7:49 pm, edited 1 time in total.
English isn’t my first language, so please excuse any mistakes.

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Wed Apr 08, 2015 7:35 pm

P.S. Too much "sudo" in
Exec=sudo lxterminal --command "sudo python3 /home/pi/Python_kode/Kattedetektor/kattdet.py"

If you run the script in your home and no need to write something in protected directories, you could remove the 2 "sudos":

Code: Select all

Exec=lxterminal --command "python3 /home/pi/Python_kode/Kattedetektor/kattdet.py"
English isn’t my first language, so please excuse any mistakes.

norpi
Posts: 31
Joined: Wed Apr 08, 2015 4:50 pm

Re: Run bash script after starting X

Thu Apr 09, 2015 8:11 pm

Hi again
That actually worked, although I had to use both the sudos for the lxterminal to start up.

troombatzia
Posts: 501
Joined: Fri Jun 29, 2012 4:27 pm

Re: Run bash script after starting X

Thu Apr 09, 2015 8:39 pm

Hi.

Very well.

I understand the second sudo, probably your python script needs super user permission to access the camera (really I don't know python and camera module), but the first sudo to launch lxterminal is strange. Anyway, you know your code better than me :-)
English isn’t my first language, so please excuse any mistakes.

norpi
Posts: 31
Joined: Wed Apr 08, 2015 4:50 pm

Re: Run bash script after starting X

Fri Apr 10, 2015 10:26 am

Hi
I find it a bit strange as well, but will try some more.

Return to “Raspberry Pi OS”