Page 1 of 1

autostarting a phyton programm

Posted: Fri Dec 28, 2018 2:52 pm
by HombreSolaire
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

Re: autostarting a phyton programm

Posted: Fri Dec 28, 2018 3:18 pm
by pfletch101
Insufficient data!!

Re: autostarting a phyton programm

Posted: Fri Dec 28, 2018 3:49 pm
by n67
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!

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 7:43 am
by HombreSolaire
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

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 11:37 am
by n67
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.

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 2:28 pm
by HombreSolaire
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

[email protected]:~ $ 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

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 2:57 pm
by n67
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.

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 3:11 pm
by bensimmo
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?

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 3:21 pm
by rpiMike
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()

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 4:00 pm
by HombreSolaire
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...

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 4:34 pm
by klricks
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 &

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 5:09 pm
by pfletch101
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.

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 5:58 pm
by HombreSolaire
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

Re: autostarting a phyton programm

Posted: Sat Dec 29, 2018 9:20 pm
by bensimmo
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.

Re: autostarting a phyton programm

Posted: Sun Dec 30, 2018 4:03 pm
by HombreSolaire
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

Re: autostarting a phyton programm

Posted: Sun Dec 30, 2018 4:31 pm
by n67
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.

Re: autostarting a phyton programm

Posted: Sun Dec 30, 2018 4:42 pm
by pfletch101
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.