1
votes

I am trying to launch a python script automatically after boot. I want to launch it in a terminal window because the program gives important feedback in the terminal.

I've researched many ways to do this including crontab, init.d, rc.local, and /etc/xdg/autostart/myscript.desktop.

When I've tested my script manually, calling rc.local from the terminal works, however none of these work after booting.

I've tried many variations, the latest in crontab:

@reboot sleep 60 && xterm -hold -e sudo python /home/pi/newcode/newcode/boot-test.py

Other variations I tried include (calling my python script from a shell script):

@reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh

and

@reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh

Update:

foo1.desktop (saved at /usr/local/bin/foo1):

[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;

foo1 (saved at /etc/xdg/autostart/foo1.desktop):

#!/bin/bash
/usr/bin/x-terminal-emulator --command="/home/pi/newcode/newcode/boot-test.py" --title="My foo1"

python script, very simple for now (saved at /home/pi/newcode/newcode/boot-test.py)

sensortype=raw_input("press enter to continue")
2
Do you really need to print to the console? Can't you just output to a log file and then tail the log file for the output if/when necessary?Labrys Knossos
maybe I don't, maybe I really want to do this because its giving me a hard time and I want to see it work. I didn't think it would be this hard. I've also read many posts of others trying to do this, some with success...user3808752
Remove the..., these are only placeholder. Compare with a full blown example from your /use/share/application directory. Double-check your saved location, your given one are exchanged to my Answer. Add python in front of the path to your *.py. Compare with my Answer. Edit your Question accordingly.stovfl

2 Answers

0
votes

Comment:
1. is named foo1.desktop ?
2. is named foo1.sh ?
3. does foo1.sh need to be made executable?

  1. Yes, must be named *.desktop
  2. If you use *.sh, the entry in *.desktop have to be equal
    Exec=gksu /usr/local/bin/foo1.sh
  3. Yes, a Bash script have to made executable.

Question: I've test my script (rc.local) manually

Don't use etc/rc.local, it's executed to early for X11.


My /etc/xdg/autostart example:

  1. Create a foo1.desktop with at least the following content:

[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;

  1. Copy foo1.desktop or create link to foo1.desktop in /etc/xdg/autostart.

  2. Create /usr/local/bin/foo1 with the following content:

#!/bin/bash
/usr/bin/x-terminal-emulator --command="python path_to_your_*.py" --title="My foo1"

  1. Make /usr/local/bin/foo1, executable.
0
votes

After trying multiple variation the script that worked contained the following:

#!/bin/bash
/usr/bin/x-terminal-emulator -hold -e sudo python /home/pi/newcode/newcode/hms2-v2.6.py

making the script executable by do the following was also necessary:

sudo chmod 755 /etc/xdg/autostart/foo1.desktop

I never got cron or init.d to work