mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Python Script not starting on Boot

Sat Jul 08, 2017 9:51 am

Hi,

I have a python script in Jessie, which I'm trying to start on boot but it will not. I have tested my script and it works when run manually ./PiTopScreen.sh and it does not require the GUI to be started.

/etc/pitop/PiTopScreen.sh

Code: Select all

#!/bin/sh
# PiTopScreen.sh

(sleep 10; python /etc/pitop/screensaver.py)&
I have tried the following:

/etc/rc.local

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

/etc/pitop/PiTopScreen.sh &

exit 0

/etc/init.d/PiTopScreen.sh

Code: Select all

### BEGIN INIT INFO
# Provides:          PiTopScreen.sh
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

#!/usr/bin/python

(sleep 10; python /etc/pitop/screensaver.py)&
sudo chmod 755 /etc/init.d/PiTopScreen.sh
sudo update-rc.d PiTopScreen.sh defaults


sudo crontab -e

Code: Select all

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
@reboot sh /etc/pitop/PiTopScreen.sh >/home/pi/logs/cronlog 2>&1
It seems like none of the examples online will work, so either I'm doing something wrong or none of these examples work on Jessie.

Thanks in advance

frud0
Posts: 14
Joined: Sat Jul 08, 2017 3:58 am

Re: Python Script not starting on Boot

Sat Jul 08, 2017 10:29 am

https://www.dexterindustries.com/howto/ ... t-startup/

I looking for a similar solution while running a display-less Pi as a standalone system. I attached a small OLED (128x64) and used to make it display the Pi's current WiFi IP address so I could SSH into it and destroy it.

I used /etc/rc.local like so

Code: Select all

sudo python /root/Adafruit_Python_SSD1306/examples/stats.py
Do NOT delete the "

Code: Select all

exit 0
".

pcmanbob
Posts: 6683
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Script not starting on Boot

Sat Jul 08, 2017 11:28 am

Hi.

If you want to run this using cron the your cron line should be

Code: Select all

@reboot /etc/pitop/PiTopScreen.sh >>/home/pi/logs/cronlog.txt 2>&1
no need for sh at the start changed > to >> so error or output from script is appended to file, and added txt extension to add easy reading of file.

also your PiTopScreen.sh file contains

Code: Select all

(sleep 10; python /etc/pitop/screensaver.py)&
if you are just trying to add a delay before running the screensaver.py you should do it like this

Code: Select all

sleep 10 && python /etc/pitop/screensaver.py &
and lastly a common problem when trying to run things using any of your stated attempts is not using full paths to any files in your screensaver.py file.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Re: Python Script not starting on Boot

Sun Jul 09, 2017 9:23 am

Hi pcmanbob,

Thanks for the response, I made the adjustments and found that my scrip is running. However it seems like my script has a different outcome if its autostarted, or if I'm manually starting it. The following script checks if the screen is being blanked on my pitop so that it can turn of the back light. It works when I execute it manually, but when I put it on Autostart I get an error:
2017-07-09 11:10:38 -> Command 'xset q | grep -oP "Monitor is \K.*"' returned non-zero exit status 1
2017-07-09 11:10:39 -xset: unable to open display "
Is there any way to autostart the script as if I manually started it, or is there some way to get the script to perform the terminal command as if I entered it manually.

Thanks in advance,

Code: Select all

import sys, os, subprocess, time
cmdMonitor = "xset q | grep -oP "'"Monitor is \K.*"'""
cmdSetBright = "brightness %d | grep -oP "'"Current brightness = \K.*"'""
cmdOffBright = "brightness off | grep -oP "'"Current brightness = \K.*"'""

sys.argv[0] = "PiTopScrnSave"

#initialization
Monitor_old = "On" 
global Brightness_Old

print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "->", "screensaver.py started"


while True:
    time.sleep(1)
    try:
        subprocess.check_call
        Monitor = subprocess.check_output(cmdMonitor, shell=True)
        Monitor = Monitor.translate(None, chr(10)+chr(5))
    except Exception as e:
        print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "->", str(e)
        Monitor = "On"
 
    if Monitor != Monitor_old:
        if 'Off' in Monitor:
            Brightness_Old = int(subprocess.check_output(cmdOffBright, shell=True))
        if 'On' in Monitor:
            subprocess.check_output(cmdSetBright % Brightness_Old, shell=True)
        Monitor_old = Monitor

pcmanbob
Posts: 6683
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Python Script not starting on Boot

Sun Jul 09, 2017 3:31 pm

I suspect your program actually does not want to be started using any of the methods you have tried, because they all run the script in the background and have no access to the screen you need to run your script when you desktop starts as part of that process.
there are numerous posts on here about how to do that if I can find the last one I will add it to the post.
edit

see here to start in terminal on desktop
viewtopic.php?f=32&t=186575
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Garthy
Posts: 13
Joined: Mon Jul 10, 2017 1:52 am

Re: Python Script not starting on Boot

Mon Jul 10, 2017 2:48 am

xset is a tool that interacts with X, so I believe it's only going to work if you have a running X Session, such as the one you'll have when running a window manager / graphical environment. That is probably why xset is failing.

I'm a little fuzzy on what you're trying to accomplish, but here are a few starting points that might help:

If you're trying to change the screensaving characteristics of a terminal/console, depending on what you are trying to do, you might want to look into the --blank option in setterm.

If you are actually working in a graphical environment, you might have more luck investigating the window manager in use, and using that to launch your script. Start here if you're using LXDE:

https://wiki.lxde.org/en/Autostart

If you want it to run in outside of a graphical session but still do something to a graphical session, you might want to manually set your DISPLAY environment variable to whatever value it would ordinarily be when you are using the graphical environment / window manager. It might look a bit like this in your script:

export DISPLAY=:0.0

If you can elaborate on what you want the end result to be, I might be able to point you in the right direction?

mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Re: Python Script not starting on Boot

Fri Jul 14, 2017 5:29 am

Ok, I completely understand why xset will not work without the GUI.

Long story short I have a pitop running Jessie with pixel because I don't like the PiTop OS. I have figured out everything except for how to turn off the screen when the screen blanks. My script queries xset every second to see if the screen has been blanked and then turns off the display. if the screen blank is no longer blanked it switches the display back on.

Do you maybe have another suggestion for checking if the screen has been blanked. The reason I ask is because if I start the terminal without the GUI the screen will still blank after inactivity right?

Garthy
Posts: 13
Joined: Mon Jul 10, 2017 1:52 am

Re: Python Script not starting on Boot

Sat Jul 15, 2017 2:28 am

Thanks for the extra information.

Now I'm going to make a number of assumptions, please correct any that are wrong:

- When you say GUI, you're referring to the running desktop environment / window manager / X session, basically the graphical desktop that will usually come up with a default Raspbian Jessie install.
- You are probably running LXDE as your desktop environment.
- You are running this environment on the Pi-Top screen as your primary screen.

Now I'll just add that I've never used a Pi-Top so I can't help with that side of things.

First of all, before anything else, dig through your main menu and look for a "Settings" submenu and find either:
- Screensaver options, then look for an "Advanced" tab.
- Power options.

Then, see if there is an option to turn off your display after a period of time. Enable it, lower the time so that you can test it, and see if it solves your problem with no added effort.

Another way to approach things is to experiment with the "dpms" and "blank" options in xset.

Failing that, back to the script option. I would tackle the desktop environment on its own first as one problem, and then the console as another.

Assuming LXDE, start here:

https://wiki.lxde.org/en/Autostart

Start with a simple script, just writing data to a fixed file, something like:

echo test >/home/pi/test

Log out and back in again, and confirm it is running. Then slowly add to the script. You may need to run the important stuff in a loop, with a sleep command each iteration.

Launching via the desktop environment will ensure things are set up correctly to talk to the right display, and commands such as xset should tend to work.

If commands don't seem to run, include the full path to that command.

The next bit will be to solve it when you are just using a console. I'd be experimenting with setterm for that.

There is also vbetool, if you like to live dangerously. Be very careful if you go down this path. I do not recommend it unless you have no other options.

Good luck.

mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Re: Python Script not starting on Boot

Sat Jul 15, 2017 7:11 pm

Hi Garthy,

Thanks the LXDE autostart did the trick! My Script launches successfully and xset works now.

I also looked and couldn't find any power options under the main menu. I experimented with DPMS and trying to get the HDMI on the Pi to switch off and not just blank. The problem with the PiTop Display is that it needs a signal over I2C to the PiTop Hub to dim the backlight. So even if I unplug the HDMI cable the backlight of the PiTop remains on.

The only way I have been able to successfully make this work is with my Python script that sets the brightness to 0 if it is blanked, and return it to the original brightness once the blanking has stopped. I also tried tvservice and setterm to detect if the screen is blanking, but I was unable to detect the screen blanking. So I guess I just have to settle for the screen blanking under LXDE.

Garthy
Posts: 13
Joined: Mon Jul 10, 2017 1:52 am

Re: Python Script not starting on Boot

Sun Jul 16, 2017 8:53 am

mvdm wrote: Thanks the LXDE autostart did the trick! My Script launches successfully and xset works now.
Happy to have been able to help.
mvdm wrote: I also looked and couldn't find any power options under the main menu.
How strange. What does this return:

Code: Select all

dpkg -l | grep screen
If it doesn't include xscreensaver, it might be worth installing it:

Code: Select all

sudo apt-get install xscreensaver
But in any case, with a working solution for LXDE it may no longer be needed.

Now I've just spent a fair bit of time trying to find a general solution re detecting console blanking. No luck. But I might be able to offer a few starting points for further research:

- Look for anything that might let you read DPMS status.

- If you have an active X Session at the time, even if you are in the console, you might have some luck including "export DISPLAY=:0.0" before "xset q" in a script.

- Similarly, if you have an active X Session at the time, just having xscreensaver around and running might help, but you'll need to play around with it.

- There might be an entry in /sys *somewhere* that gives you the right status. No hits for "dpms" for me, but lots of "power". *Maybe* also try in /proc.

That's probably as much as I can offer at this point. :oops:

Best of luck!

mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Re: Python Script not starting on Boot

Sun Jul 16, 2017 11:42 am

Hi Garthy,

Yes I didn't have xscreensaver installed, I'll give it a try and see if that makes things easier.

Thanks for all the added pointers, very useful. I will continue to look into this, to see if there is a better way to implement this and then I will add the updates here.


Thanks again.

Garthy
Posts: 13
Joined: Mon Jul 10, 2017 1:52 am

Re: Python Script not starting on Boot

Sun Jul 16, 2017 12:48 pm

No probs, happy to have been able to help.

Best of luck. :)

Return to “Troubleshooting”