Problem running a script at startup


17 posts
by endorak » Wed Jun 14, 2017 12:16 pm
Hello,

I'm trying to run a script at startup of my rPi, but nothing happens.

I've written the script to run a puredata patch and a python script and if I try to run the script manually it works well.
The I've added the script's path (/home/pi/bin/script_pd) to the rc.local file and make it executable with:
Code: Select all
sudo chmod 755 script_pd


I rebooted the rPi, but neither puredata nor python scripts seem to work.

Maybe I'm wrong about a trivial thing, but I can not figure it out...

This is my script script_pd:
Code: Select all
#!/bin/bash
echo ".....|| Starting PD ||....."
echo "...|| Starting tshark ||..."
pd /home/pi/Downloads/tsharky-master-raspi/tshark.pd &
sudo python3 /home/pi/Downloads/tsharky-master-raspi/etsharky.py

I've also tried to add a
Code: Select all
sleep 30
above the first command to be sure that all was loaded by the operating system, but nothing.
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by endorak » Wed Jun 14, 2017 4:39 pm
Also tried to run the script like a crontab event, but it doesn't work.

Why it works if I run it manually? :(
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by DirkS » Wed Jun 14, 2017 4:48 pm
Try using full paths to the programs in your script (pd and python3)
Posts: 6941
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK
by endorak » Wed Jun 14, 2017 4:58 pm
What do you mean?
Sorry but I'm not an expert at all
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by DougieLawson » Wed Jun 14, 2017 5:01 pm
Post your whole script and we'll tell you the changes that are needed.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 28159
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by endorak » Wed Jun 14, 2017 5:05 pm
Code: Select all
#!/bin/bash
echo ".....|| Starting PD ||....."
echo "...|| Starting tshark ||..."
pd /home/pi/Downloads/tsharky-master-raspi/tshark.pd &
sudo python3 /home/pi/Downloads/tsharky-master-raspi/etsharky.py

This is my entire script, from your word I think that I'm missing something...

It seems that the problem could be that puredata it doesn't open or maybe it doesn't open the pd patch, because the python script is already running when I manually open the puredata patch...
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by DougieLawson » Thu Jun 15, 2017 8:17 am
What's the pd program doing? What's in /home/pi/Downloads/tsharky-master-raspi/tshark.pd?
What in /home/pi/Downloads/tsharky-master-raspi/etsharky.py? Why does that need sudo? [Note: anything run from rc.local runs as root anyway.]
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 28159
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by Martin Frezman » Thu Jun 15, 2017 8:34 am
You need to give us some idea what "didn't work" means.

This may involve some detective work on your part. It is likely that doing that detective work will lead you to the solution.
If this post appears in the wrong forums category, my apologies.
Posts: 751
Joined: Mon Oct 31, 2016 10:05 am
by KLL » Thu Jun 15, 2017 9:48 am
pls show us your autostart:

sudo cat /etc/rc.local

https://www.raspberrypi.org/documentation/linux/usage/rc-local.md
Posts: 719
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by Martin Frezman » Thu Jun 15, 2017 10:47 am
sudo cat /etc/rc.local

sudo?????
If this post appears in the wrong forums category, my apologies.
Posts: 751
Joined: Mon Oct 31, 2016 10:05 am
by KLL » Thu Jun 15, 2017 11:09 am
sorry, indeed need
sudo nano /etc/rc.local
but only
cat /etc/rc.local

anyhow what i think what needs to be checked is, that the start is prior to the
exit 0
line, like
Code: Select all
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/home/pi/myautostart.sh

exit 0


and to be sure if the problem is in the autostart or in the job,
test with something easy for autostart:

nano /home/pi/myautostart.sh
Code: Select all
#!/bin/bash
touch /run/shm/kilroy_was_here

chmod +x /home/pi/myautostart.sh

after test or reboot pls see if that file is in user ram disk
ls /run/shm/
Posts: 719
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by endorak » Sun Jun 18, 2017 6:31 pm
ok, sorry for the delay and thanks for the support.

@KLL your test works, so the .sh script with touch line of code runs at the startup.

My previous script wasn't after the "exit 0" line, but I double checked to be sure :)

When I add 2 more lines before the one of the test to run other programs (a python script and a puredata patch, both fully working if I try to run from the terminal itself), nothing happen at the starup, even the file in the user ram disk is not there.

So I suppose that the problem are my lines of code...

I post here my actual (not working) content of autostart.sh:
Code: Select all
#!/bin/bash

pd /home/pi/patchtest.pd
sudo python3 /home/pi/patchtest.py
touch /run/shm/testttttt


(I need that sudo for the python script)
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by KLL » Mon Jun 19, 2017 1:05 am
https://www.raspberrypi.org/forums/viewtopic.php?p=1175154#p1175154
you did not read that!

it is strongly recommended that programs
here "pd" and "python3" are called with full path! like /usr/bin/python3
as you installed "pd" you must find where it is.

https://www.raspberrypi.org/forums/viewtopic.php?p=1175391#p1175391
you did not answer this!
i think the point is that IF
"pd" and your python tool need/use GUI... they
will not work because when started from rc.local it is too early ( in boot process ) to call GUI.
ok, other words:
if your "autostart.sh" is called from CLI and opens some windows
it can not work at boot!

"rc.local" is only good for starting service programs.

so a other start method might be better.
take a look here to get more ideas:
http://kll.engineering-news.org/kllfusion01/articles.php?article_id=124

also i noticed that in a earlier version autostart.sh you started the
"pd .. &"
means ?to push it into background?
now NOT use "&"
Posts: 719
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by endorak » Mon Jun 19, 2017 8:44 am
https://www.raspberrypi.org/forums/viewtopic.php?p=1175154#p1175154
you did not read that!


I've read it, tried it, but I had the same problem. Sorry, I haven't told you.

What's the pd program doing? What's in /home/pi/Downloads/tsharky-master-raspi/tshark.pd?
What in /home/pi/Downloads/tsharky-master-raspi/etsharky.py? Why does that need sudo?


pd is filtering some messages from a net sniffing. The first file is the pd patch, the second one the python script lo run the sniffing program and to communicate the infos to pd. py needs sudo because of the sniffing tool that want the root privileges. (I didn't know that rc.local did alla as root).
also i noticed that in a earlier version autostart.sh you started the
"pd .. &"
means ?to push it into background?
now NOT use "&"

I have to use the & because otherwise the script in the next line does't run.

i think the point is that IF
"pd" and your python tool need/use GUI... they
will not work because when started from rc.local it is too early ( in boot process ) to call GUI.
ok, other words:
if your "autostart.sh" is called from CLI and opens some windows
it can not work at boot!
"rc.local" is only good for starting service programs.

I think THIS is the real problem, you're right.
Maybe something with init.d could help me.
(your link is interesting, but I don't want to do nothing to run all my program, after I switch on my Rpi.
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by KLL » Mon Jun 19, 2017 8:57 am
endorak wrote:but I don't want to do nothing to run all my program, after I switch on my Rpi.

???
Posts: 719
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
by endorak » Mon Jun 19, 2017 3:31 pm
I just want to switch on the rpi and let it go with the program open. It will send OSC messages to a Max patch on another device.
Posts: 7
Joined: Wed Jun 14, 2017 11:46 am
by KLL » Mon Jun 19, 2017 3:46 pm
so try a
/home/pi/.config/autostart/myautostart.desktop
starting the myautostart.sh

still i recommend a delaytimer in the myautostart.sh
env sleep 30s
Posts: 719
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand