HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

autostarting a phyton programm

Fri Dec 28, 2018 2:52 pm

Hello,
I actually experience an odd problem.
Since about 3 years I have my raspberry up and running, executing a phyton programm for me.
Even after reboots and updates the programm does his job.
Well... until last saturday. It stopped working and a reboot did not help.
With

Code: Select all

ps -ef | grep python
I can see the programm is running but it don't do anything.
Now the odd thing.
When I start it in the console it works flawlessly.
I would very much appreciate any advice here.
Regards Patrick

pfletch101
Posts: 499
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: autostarting a phyton programm

Fri Dec 28, 2018 3:18 pm

Insufficient data!!

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

Re: autostarting a phyton programm

Fri Dec 28, 2018 3:49 pm

Well, the obvious answer is: If it runs "flawlessly" when run in a console, then run it in a console. Problem solved.

And, yes, there are ways to make things run in a console automatically on startup. Get that working, and, voila!
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

Re: autostarting a phyton programm

Sat Dec 29, 2018 7:43 am

You guys are right. So let me be a little more specific.

My raspberry runs in a so called "headles mode". It is located in an electrical cabinet in my cellar. I get access to this device over ssh.

The task this raspberry has to fulfill is twofold.
First: monitor the state of an electrical switch
Second: at status change post a IFTTT trigger with some jason attached.
So for about three years my raspberry did the job aside from a needed reboot, lets say two times a year, because the programm or something else stopped working. But after the reboot the raspi was up and running again.

To get the job done I worte a little pyton program, starting with the shebang

Code: Select all

#!/usr/bin/python
I made the programm executable with

Code: Select all

sudo chmod +x /usr/local/bin/person_enter_leave_v6.py
and edited the rc.local with

Code: Select all

sudo nano /etc/rc.local
and added the line
python /usr/local/bin/person_enter_leave_v6.py &
just before

Code: Select all

exit 0
So far so good...
now, when I check, after a reboot, if the programm is actually executed with

Code: Select all

ps -ef | grep python
the result is
root 418 1 0 Dec28 ? 00:00:05 python /usr/local/bin/person_enter_leave_v6.py
pi 17927 17857 0 08:35 pts/0 00:00:00 grep --color=auto python
so I be very positive it is loaded

But unfortunately since last saturday the programm won't post the IFTTT trigger, perhaps it don't check the electrcal switch either, or both, or vice versa.

I can start the programm manually with

Code: Select all

python /usr/local/bin/person_enter_leave_v6.py
but this is no solution, because as soon as I exit the ssh terminal, the program is ended to.

So, now I hope that clears things up and someone come up with some advice for this problem.

Regards Patrick

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

Re: autostarting a phyton programm

Sat Dec 29, 2018 11:37 am

I can start the programm manually with

Code: Select all

python /usr/local/bin/person_enter_leave_v6.py
but this is no solution, because as soon as I exit the ssh terminal, the program is ended to.
Well, the first thing is: You're just going to have to debug it. Something changed, and there's not really any "magic bullet" that we can provide. We''ll just be guessing as to what changed to make it stop working where once it did work.

However, since you mention running it via ssh - and that working OK right up until you close the ssh session - there are a couple of things I can suggest that may get you working until you can debug the real problem.

1) You can run the process with setsid - which is like nohup but much better. If you login via ssh and then run:

Code: Select all

$ setsid python /usr/local/bin/person_enter_leave_v6.py
It should run the program "in the background" and when you logout, it will stay running. See if that works for you. man setsid will get more details on this program.

2) You could use either screen or tmux to run the program in a manner that will keep it running after you logout. Both of these programs are very nice and you will find them useful in the long run, but explaining how to use them is beyond the scope of this post. I would do "apt-get install screen" then "man screen" and go from there.

Note: screen is the older and more stable of the two programs. I think it is easier to use than tmux, but tmux is newer and has a lot more features. So, you will have to make that choice.

Again, these are not optimal solutions, since they require to manually run it after each reboot - but it should paint the way towards what the underlying problem is. And, of course, these things can be automated so once you figure it out, you could automate it so that you don't need to manually log in and launch it.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

Re: autostarting a phyton programm

Sat Dec 29, 2018 2:28 pm

Hello n76,
thank you for your reply. I really appeciate the time and effort you put into my questions.
First of all, setsid works for now. Actually I have now two instances (can I write that?) of my program running

Code: Select all

pi@raspberrypi:~ $ ps -ef | grep python
root       418     1  0 Dec28 ?        00:00:05 python /usr/local/bin/person_enter_leave_v6.py
pi       18138     1 31 14:52 ?        00:00:02 python /usr/local/bin/person_enter_leave_v6.py
pi       18145 18101  0 14:52 pts/0    00:00:00 grep --color=auto python
one as root, which was launched at startup and the other one from user pi over your advice with setsid.

Now, how to debug this behavior... I have actually no idea where to start. There are some other options to start a program at startup of the raspberry, I know that. I'm aware of screen which I have already on the pi tmux on the other hand is new to me.
So I do know I have options....
It would be a little more satisfying to know what went wrong with my initial project.
Perhaps you could put me in the right direction here.
Regards
Patrick

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

Re: autostarting a phyton programm

Sat Dec 29, 2018 2:57 pm

You should remove (or just comment out) the line from /etc/rc.local - of course - since you are now running it manually. And, of course, you should kill the one running as root now (if you don't want to reboot).

But now that I think about it, maybe that's the problem. When run from rc.local, it runs as root, whereas when you run it manually, it runs as pi. Maybe that's the problem.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: autostarting a phyton programm

Sat Dec 29, 2018 3:11 pm

In RC.local

If you use

Code: Select all

su pi -c "python myprog.py" &
It should run it as the pi use and not root (incase that is causing a problem)


Did you update the system?
You could add print statements to display actions on screen to see what the program is doing, to help debug.

You could move to systemd services, but what OS versions and kernel are you using?
Last edited by bensimmo on Sat Dec 29, 2018 9:17 pm, edited 2 times in total.

User avatar
rpiMike
Posts: 850
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: autostarting a phyton programm

Sat Dec 29, 2018 3:21 pm

My suggestions for debugging:

Redirect output and errors to a file:

Code: Select all

python /usr/local/bin/person_enter_leave_v6.py >> /home/pi/templog.txt 2>&1 &
In your python code flush stdout after all print statements:

Code: Select all

import sys
print('test')
sys.stdout.flush()

HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

Re: autostarting a phyton programm

Sat Dec 29, 2018 4:00 pm

Hello,
thank you at all.
I'm a little puzzled at the moment.
I try going with your advice, bensimmo, editing the rc.local like

Code: Select all

sudo nano //etc/rc.local
edit the line

Code: Select all

sure pi -c python /usr/local/bin/person_enter_leave_v6.py &
Exit with control x
Save modified buffer with y
File Name to Write: //etc/rc.local Enter

check the changes again with

Code: Select all

sudo nano //etc/rc.local
where I can see my changes.

But after the reboot //etc/rc.local is back to the state before my editing.... now, that gives me something to chew on...

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

Re: autostarting a phyton programm

Sat Dec 29, 2018 4:34 pm

HombreSolaire wrote:
Sat Dec 29, 2018 4:00 pm
.......

Code: Select all

sure pi -c python /usr/local/bin/person_enter_leave_v6.py &
.....
That does not look right to me.........
To start and force user pi...... should be like this:

Code: Select all

sudo -u pi python /usr/local/bin/person_enter_leave_v6.py &
Last edited by klricks on Sat Dec 29, 2018 9:51 pm, edited 1 time in total.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

pfletch101
Posts: 499
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: autostarting a phyton programm

Sat Dec 29, 2018 5:09 pm

HombreSolaire wrote:
Sat Dec 29, 2018 4:00 pm
Hello,
thank you at all.
I'm a little puzzled at the moment.
I try going with your advice, bensimmo, editing the rc.local like

But after the reboot //etc/rc.local is back to the state before my editing.... now, that gives me something to chew on...
You may have found your problem! It sounds as if your SD card may be on the way out and have become read only. It may be worth making and booting a copy of it.

HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

Re: autostarting a phyton programm

Sat Dec 29, 2018 5:58 pm

pfletch101
that would be a very plausible explanation for this recent behavior... I never thought about that.
Despite I worked already on a solution involving a arduino, just because I never trusted this SD card.
So, I'm going to check my stuff with a new SD card.
Regards
Patrick

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: autostarting a phyton programm

Sat Dec 29, 2018 9:20 pm

Yep try a new SD, you can always see if the old repairs after.

Look at a new clean setup, and using cron or services to start. You may have more control.


I correct the startup line 'sure' should be just su
An auto correct crepe in.

That's how I used to do it with SenseHAT dataloggers.
I also used to cd to the folder first.

It has been a while since it tried it again though.

HombreSolaire
Posts: 9
Joined: Tue Dec 06, 2016 5:35 pm

Re: autostarting a phyton programm

Sun Dec 30, 2018 4:03 pm

Hi @ all,
The problem is solved! After I put all the stuff on a new SD card eveverything is back to normal.
I woul'd like to thank you all for your support.
Regards
Patrick

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

Re: autostarting a phyton programm

Sun Dec 30, 2018 4:31 pm

Yeah, I'm beginning to think that "Your SD card is worn out - replace it" should be our first response to any "It was working right up until it stopped working - and I didn't do anything" type problem report.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

pfletch101
Posts: 499
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: autostarting a phyton programm

Sun Dec 30, 2018 4:42 pm

n67 wrote:
Sun Dec 30, 2018 4:31 pm
Yeah, I'm beginning to think that "Your SD card is worn out - replace it" should be our first response to any "It was working right up until it stopped working - and I didn't do anything" type problem report.
It's certainly a possibility that should always be borne in mind. The problem I have encountered in software support (mainly) for Applications running on Windows computers, however, is that "I didn't do anything" has often turned out to be untrue, and the 'minor change' the user had forgotten not infrequently turns out to have caused or to be related to the problem (s)he is encountering.

Return to “Troubleshooting”