sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

autorun starts 2nd time when Rasbian Terminal opened

Sat May 12, 2018 10:05 pm

Newbie here. I've got a RasPi Zero W running Rasbian GNU/Linus 9 with LXPanel 0.9.3. I used the following to auto-start a commonly known file, sample.py.

Code: Select all

sudo nano /home/pi/.bashrc
Go to the last line of the script and add:

Code: Select all

echo Running at boot 
sudo python /home/pi/sample.py
When I power the Pi, it launches the program. But, when I open Terminal on LXPanel, it launches the program again. This became a problem when I auto-started my own program which loops in the background. So, when I open Terminal, I get two looping programs competing with each other. Why is this happening and how can I prevent the act of opening Terminal from starting the program a second time?

klricks
Posts: 5664
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Sat May 12, 2018 11:39 pm

sonettguy wrote:
Sat May 12, 2018 10:05 pm
Newbie here. I've got a RasPi Zero W running Rasbian GNU/Linus 9 with LXPanel 0.9.3. I used the following to auto-start a commonly known file, sample.py.

Code: Select all

sudo nano /home/pi/.bashrc
Go to the last line of the script and add:

Code: Select all

echo Running at boot 
sudo python /home/pi/sample.py
When I power the Pi, it launches the program. But, when I open Terminal on LXPanel, it launches the program again. This became a problem when I auto-started my own program which loops in the background. So, when I open Terminal, I get two looping programs competing with each other. Why is this happening and how can I prevent the act of opening Terminal from starting the program a second time?
You should not be using sudo to edit and especially not to create files in the users home directory. If you create a file with sudo then it will be owned by root and may not work as expected.

I would use autostart to start a script on bootup. (GUI desktop login)

Code: Select all

nano /home/pi/.config/lxsession/LXDE-pi/autostart
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Sun May 13, 2018 2:10 am

Thanks for the quick feedback, KL.

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Sun May 13, 2018 2:47 am

I didn't have an autostart file in that folder, so I created one. I cut & pasted the two command lines to start each program, removing the "sudo" from the front.

Code: Select all

python /home/pi/sample.py
but, now they don't start at all.

klricks
Posts: 5664
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Sun May 13, 2018 3:58 am

sonettguy wrote:
Sun May 13, 2018 2:47 am
I didn't have an autostart file in that folder, so I created one. I cut & pasted the two command lines to start each program, removing the "sudo" from the front.

Code: Select all

python /home/pi/sample.py
but, now they don't start at all.
What does the script do? If it exits to the command line then the terminal will immediately close and you won't see anything.
Try:

Code: Select all

@lxterminal -e python /home/pi/sample.py
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Sun May 13, 2018 6:44 pm

Sorry, I don't understand the purpose of your question asking what the script does. It is one of two files. Sample.py speaks a sentence. It is shown in https://www.dexterindustries.com/howto/ ... t-startup/ as an example. The second script displays time on two seven segment displays (ex_7segment_clock in Adafruit). But, my problem was not getting the script to run, it was that it was starting twice when I would open the Terminal in Raspbian. Only now, having moved it to a new "autostart" file, I am having trouble getting it to run.

On first blush, it seems to be much more involved than simply creating an autostart file. Am I missing something? For instance, in another post on getting programs to start automatically, I found this involved discussion:
I want to throw in my two cents, even though this is an old question but commonly asked to do simple thing - autostart. I tried all the suggested solutions in all the answers for this question. NONE of them worked for me. I am using Raspberry PI Model 2 with Raspbian.
The only way I could get my application to autostart successfully is through a script as follows. I say successfully because my application started as expected without having any issue like starting with wrong work path.
1.Create an empty file with extension .sh and name it whatever you want.
2.Copy and Paste the following EXACTLY except change "your application name" to the script name that you just created.

Code: Select all

 #! /bin/sh
 ### BEGIN INIT INFO
 # Provides:          noip
 # Required-Start:    $remote_fs $syslog
 # Required-Stop:     $remote_fs $syslog
 # Default-Start:     2 3 4 5
 # Default-Stop:      0 1 6
 # Short-Description: Simple script to start a program at boot
 ### END INIT INFO

 #change /direct/path/to/your/application to the path your application is in.
 cd /direct/path/to/your/application      # example cd /home/pi/myprogram/

 #change YourProgramExactName to Exact name of your program that you want to auto start
 ./YourProgramExactName

 exit 0 
3. Then, save the script file within your application folder
4. Then, open /home/pi/.config/autostart folder. It might be different in your case. Just open your home folder and enable view hidden folders. open .config/autostart. If you don't see autostart folder, then create a folder called autostart within .config folder.
5. within autostart folder you will need to create a shortcut to your script file that you created as follows. Create an empty file with extension .desktop.
6. Copy and paste the following in the empty desktop file except you will need to change Comment, Name, Exec, Path and Icon field's value.
[Desktop Entry]

Code: Select all

Comment=
Exec=/path/to/Your/application/Name-of-the-script-file (.sh)
Icon=/Path/to/Your/application/Icon/IconName
Name=YourApplicationEXACTNAME
Path=/Path/to/Your/Application-ONLY
Type=Application 
7. Save and close the file after changing all the necessary fields. You are done. Just test it out.
• note : this makes the script Debian LSBInit compliant:https://wiki.debian.org/LSBInitScripts

User avatar
thagrol
Posts: 712
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Mon May 14, 2018 4:43 pm

sonettguy wrote:
Sat May 12, 2018 10:05 pm
Newbie here. I've got a RasPi Zero W running Rasbian GNU/Linus 9 with LXPanel 0.9.3. I used the following to auto-start a commonly known file, sample.py.

Code: Select all

sudo nano /home/pi/.bashrc
Go to the last line of the script and add:

Code: Select all

echo Running at boot 
sudo python /home/pi/sample.py
When I power the Pi, it launches the program. But, when I open Terminal on LXPanel, it launches the program again. This became a problem when I auto-started my own program which loops in the background. So, when I open Terminal, I get two looping programs competing with each other. Why is this happening and how can I prevent the act of opening Terminal from starting the program a second time?
Just to clarify things, .bashrc doesn't run at boot. It's run when you start a bash shell so at login, every time you open a new terminal window, when you connect via ssh, etc.

I don't know about autostart but I'd guess that runs once every time the GUI desktop is started or a user logins in via the GUI. Again this is not a "run at boot" thing.

.bashrc won't run unless it's owner is logged in, I'd guess autostart is the same.

To run something at boot use cron with a start time of "@reboot".

If you want to launch it from .bashrc, have you script check if a copy is already running and exit if so.
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Mon May 14, 2018 7:43 pm

That is helpful information, thagrol. Thanks.

n67
Posts: 718
Joined: Mon Oct 30, 2017 4:55 pm

Re: autorun starts 2nd time when Rasbian Terminal opened

Mon May 14, 2018 9:16 pm

The reason that the "put stuff in .bashrc" method (seems to) work and, thus, the reason this erroneous advice has found its way into the Googlesphere, is that most Raspbian distros automatically log in as user pi on tty1 as part of the boot/startup code. Thus, the login on tty1 runs the .bashrc and, voila!, it (seems to) work.

However, as various posts on this thread have hinted, there is a lot that can go wrong with this method, and it is, thus, not advised.
Last edited by n67 on Tue May 15, 2018 1:03 am, edited 1 time in total.
"L'enfer, c'est les autres"

klricks
Posts: 5664
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Tue May 15, 2018 12:49 am

sonettguy wrote:
Sun May 13, 2018 2:47 am
I didn't have an autostart file in that folder, so I created one. I cut & pasted the two command lines to start each program, removing the "sudo" from the front.

Code: Select all

python /home/pi/sample.py
but, now they don't start at all.
If you are running the latest Raspbian Stretch with Desktop then the autorun should have already been created and located in
/home/pi/.config/lxsession/LXDE-pi/
Pretty much anything that can be run from the command line will work in autostart though sometimes a delay needs to be added such as (sleep 2).

I copied sample.py from the web site link you gave and put it in autostart as above and installed the espeak and it worked for me.

Note for testing purpose logout and back in is faster than rebooting.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

User avatar
thagrol
Posts: 712
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Tue May 15, 2018 12:41 pm

n67 wrote:
Mon May 14, 2018 9:16 pm
most Raspbian distros automatically log in as user pi on tty1 as part of the boot/startup code
No. At least not that I've seen. That only happens when explicty configured to do so not out of the box.

The misunderstanding is far more likely to have been caused by people moving from windows (essentially and historically a single user system) to a true multi-user system (linux).
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

n67
Posts: 718
Joined: Mon Oct 30, 2017 4:55 pm

Re: autorun starts 2nd time when Rasbian Terminal opened

Tue May 15, 2018 9:20 pm

I find your experiences and observations interesting.

Strange... But interesting.
"L'enfer, c'est les autres"

User avatar
thagrol
Posts: 712
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Wed May 16, 2018 4:44 pm

n67 wrote:
Tue May 15, 2018 9:20 pm
I find your experiences and observations interesting.

Strange... But interesting.
I find yours mis-informed and/or trolling. None of the dozen plus linux boxes I have here automatically login in as any user on any device. All were installed with the default login settings. That box count includes several raspberry pi and debian boxes.

I'm not going to discuss this further as we've had similar differences before...
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Wed May 16, 2018 9:10 pm

My intent is to boot headless, so I've dropped the autostart file method. I've abandoned the .bashrc as well, since it was obviously the culprit starting multiple versions. That wouldn't be a problem except that I am also starting an infinitely-looping program. Multiple copies of it compete to display things. Now, I am trying the following "cron" method.
To use crontab with your Raspberry Pi to automate your programs, follow these steps:
1. Write your program and note down its location. We’ll be using a program called sample.py and save it at /home/pi/Desktop/.
2. Now open crontab. You may need to open crontab in root (add sudo before the command!).

Code: Select all

crontab -e
3. Add a new entry at the very bottom with @reboot to specify that you want to run the command at boot, followed by the command. Here we want to run the python program, so our entry is

Code: Select all

@reboot sudo python /home/pi/Desktop/sample.py
 /home/pi/Desktop/pyprog/log.txt 
4. Now save the file and exit.
5. When you restart the pi, the command will be run. Be a bit careful with the permissions and making sure that your program runs properly before you put it on boot: you can waste a lot of time trying to figure out what went wrong!
So, I'm nearly back to where I started, which was having a program boot and run properly (at least until I opened Terminal in Raspbian). Almost. Now, it seems, one of my programs no longer gets the time from the Internet. In fact, my Pi seems to have a new IP address. I wonder if doing all this caused me to lose my configuration along the way. I must leave that for another day...

User avatar
DougieLawson
Posts: 33308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Thu May 17, 2018 6:55 am

What's stopping you creating a systemd service and enabling that at boot time?

sudo nano /etc/systemd/system/stuff_at_boot.service

Code: Select all

[Unit]
Description=stuff @ boot service

[Service]
ExecStart=/home/pi/python_to_run_at_boot.py
Restart=always
User=pi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=stuff_at_boot

[Install]
WantedBy=multi-user.target
Enable that with
sudo systemctl enable stuff_at_boot.service
sudo systemctl start stuff_at_boot.service
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
RaTTuS
Posts: 9992
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: autorun starts 2nd time when Rasbian Terminal opened

Thu May 17, 2018 7:34 am

re .bashrc
viewtopic.php?t=7192#p307237 may help
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

sonettguy
Posts: 65
Joined: Wed Jan 10, 2018 7:29 pm
Location: texas, USA

Re: autorun starts 2nd time when Rasbian Terminal opened

Thu May 17, 2018 8:20 pm

What's stopping you
@Dougie, ignorance. Honestly, I can't even tell in your sample code what I am supposed to change to apply to my case.
@ RaT, thanks, but I don't click on .php links.

User avatar
DougieLawson
Posts: 33308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: autorun starts 2nd time when Rasbian Terminal opened

Thu May 17, 2018 8:49 pm

Feed some of the words from my sample service file into your favourite search engine and the documentation will pop out. Doing that is part of learning. Learning is the primary mission of the Raspberry Pi Foundation. If I hand it to you on a plate you learn nothing.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

n67
Posts: 718
Joined: Mon Oct 30, 2017 4:55 pm

Re: autorun starts 2nd time when Rasbian Terminal opened

Thu May 17, 2018 10:29 pm

I know you like it - and are proud of having learned it yourself - but systemd is really not worth learning. It's a mess.
"L'enfer, c'est les autres"

Return to “Troubleshooting”

Who is online

Users browsing this forum: Insurmountable, kondaveetiarungopal, winwan33 and 47 guests