Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

How to: Run once per session

Sun Jul 26, 2015 10:02 am

Hi,

I have a script that I'm launching using /etc/profile.

Reason: this method allows certain privileges that my script needs to run.

Problem: it executes the script 6 times.

Request: I need to make my script exit if it has already been run in this session.
I am trying to make it work with a global variable, but coming from a vb6/.net background, I can't get my head around python variables.

it needs to be something like:

(edited)
check if global variable = 1
if not, run script then set global variable to 1
if so, exit


any hints on how one might achieve this?
Last edited by Dranoweb on Sun Jul 26, 2015 10:09 am, edited 1 time in total.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to: Run once per session

Sun Jul 26, 2015 10:06 am

What do you mean by session?

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:07 am

per boot.

so for instance on a windows machine a "session" would be each time you log into a user's account.


Essentially I need it to run once each time it's booted up, and ignore any further attempts to trigger said script.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to: Run once per session

Sun Jul 26, 2015 10:12 am

Dranoweb wrote:per boot.

so for instance on a windows machine a "session" would be each time you log into a user's account.


Essentially I need it to run once each time it's booted up, and ignore any further attempts to trigger said script.
I'd add it to a cron file and use the @reboot option.

Here is an example in my root crontab.

Code: Select all

@reboot              /usr/local/bin/tmpfs
That runs /usr/local/bin/tmpfs at boot (in my case the script sets up a RAM disc).

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:16 am

originally I had it running @reboot in root cron - Not an option, cron does not launch with the required privileges.

Rc.local is also inactive on my pi for some ungodly reason I can't fix,

and I've tried inittab and that doesn't have the required privileges also.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:19 am

I've also tried LXDE autostart, again not sufficient privelages

I also installed something I can;t remember the name of - that replaced my inittab. it works, but again, not sufficient privileges.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:21 am

essentially I'm launching a python script to detect button presses - and it then runs:

Code: Select all

os.system("mpg321 -g 50 /home/pi/p51.mp3")
os.system("python /home/pi/lightshowpi/py/synchronized_lights.py --file=/home/pi/p51flyby.mp3
")
the second line is ignored in all cases except when launched from /etc/profile

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to: Run once per session

Sun Jul 26, 2015 10:23 am

Dranoweb wrote:originally I had it running @reboot in root cron - Not an option, cron does not launch with the required privileges.

Rc.local is also inactive on my pi for some ungodly reason I can't fix,

and I've tried inittab and that doesn't have the required privileges also.
What privileges does the job need? Since you can run the /etc/profile script from the root crontab you should be able to replicate the permissions.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to: Run once per session

Sun Jul 26, 2015 10:24 am

Dranoweb wrote:essentially I'm launching a python script to detect button presses - and it then runs:

Code: Select all

os.system("mpg321 -g 50 /home/pi/p51.mp3")
os.system("python /home/pi/lightshowpi/py/synchronized_lights.py --file=/home/pi/p51flyby.mp3
")
the second line is ignored in all cases except when launched from /etc/profile
Because you haven't told it where python is located. You need to give the full path, /usr/bin/python.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:26 am

to quote my last post:
essentially I'm launching a python script to detect button presses - and it then runs:

Code: Select all

    os.system("mpg321 -g 50 /home/pi/p51.mp3")
    os.system("python /home/pi/lightshowpi/py/synchronized_lights.py --file=/home/pi/p51flyby.mp3")


the second line is ignored in all cases except when launched from /etc/profile
I'd love to know why, but as i didn't write lightshowpi, I'm at a loss as to why this is.
I'm scheduled to demo my project tomorrow, and this is the last of my problems, and has persisted for weeks.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:28 am

joan wrote: Because you haven't told it where python is located. You need to give the full path, /usr/bin/python.
I'll check that real quick - I have the unit in front of me.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 10:35 am

No go, same issue as I originally had;

works fine from terminal, but when run from any of the above mentioned methods, lightshow pi is ignored.

the button detect script launches this shell script:

Altered according to Joan's suggestion.

Code: Select all

sudo mpg321 -g 50 /home/pi/chimeup.mp3
sudo mpg321 -g 50 /home/pi/p51approaching.mp3
sudo /usr/bin/python /home/pi/lightshowpi/py/synchronized_lights.py --file=/home/pi/p51flyby.mp3

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to: Run once per session

Sun Jul 26, 2015 10:48 am

Isn't this basically the same post as viewtopic.php?f=32&t=116594 ?

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 11:03 am

Yes I guess it has become that way.

However, I had abandoned that approach.

I'm chasing my tail here because everybody wants to work around my solution.

My solution was to prevent my script from re-executing a second time.

every suggestion i get is essentially "just bung it in cron" or "why not use rc.local"

no one has yet to find a way to make rc.local work, or cron.

so, I started a new thread to ask for help writing a python script I hoped would temporarily solve my issues.

so far, no one has even attempted that method, instead suggesting that I follow the "correct coding methods" that i have been circling for weeks.

forgive the tone of this reply, but I'm tearing my hair out at this evasive behavior.

I'm well aware of the proper methods, but in this case they don't work - period.
I would be eternally grateful if someone could suggest some code to the effect of my original post.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: How to: Run once per session

Sun Jul 26, 2015 11:08 am

rpdom wrote:Isn't this basically the same post as viewtopic.php?f=32&t=116594 ?
Yes. And the OP is trying to treat the symptoms at this point rather than find the disease. This is understandable as he is under time pressure to get it working however he can.

Although I don't recommend it, one way to do what is requested here would be to create a flag file in /tmp. The program would check for the file's existence and exit if it is present. Since /tmp is cleared on each boot, this should work.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 11:16 am

I think at this stage, I'll be installing an SSH client onto my phone, and triggering the lightshow from there.

It's now 9pm here, and sleep is required between now at 8am.

But this won't be a permanent solution.

I would like to cure the disease rather than treat the symptoms, but this is a charitable design job for a local museum, and time/money is limited.

I had tried flagging the file, but I don't believe I had the method correct, as I had no results.

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: How to: Run once per session

Sun Jul 26, 2015 11:57 am

For further reference:

Replacing the lightshowpi python script with another python script runs fine.
I suspect there is something weird happening with this exact script.

I have posed similar questions on the developer's google+ page for the script, and have not received much help.

here's what I did to test the theory:
I modified the shell script (launched from the button detect python)
Yes, it's a dodgy way to get to the goal I know - please bear with me.

Code: Select all

sudo mpg321 -g 50 /home/pi/chimeup.mp3
sudo mpg321 -g 50 /home/pi/p51approaching.mp3
sudo /usr/bin/python /home/pi/lightshowpi/py/synchronized_lights.py --file=/home/pi/p51flyby.mp3
sudo python /home/pi/lightson.py

the extra python script is as follows:
(just tests all the relays on the card and resets them)

Code: Select all

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
#
#
#
GPIO.output(18, False)
time.sleep(0.1)
GPIO.output(18, True)
time.sleep(0.1)
#
GPIO.output(23, False)
time.sleep(0.1)
GPIO.output(23, True)
time.sleep(0.1)
#
GPIO.output(24, False)
time.sleep(0.1)
GPIO.output(24, True)
time.sleep(0.1)
#
GPIO.output(25, False)
time.sleep(0.1)
GPIO.output(25, True)
#
time.sleep(0.5)
#
#
GPIO.output(18, True)
GPIO.output(23, True)
GPIO.output(24, True)
GPIO.output(25, True)

Return to “Python”