0

I asked this question in January here on the platform and thought that the reason for my Pi crashing was the raspistill script I used. This turned out to be a false positive.

After some hassle and trying, I decided to give this issue another go and completely re-setup my RPi with the latest RASPBIAN JESSIE LITE released at 2016-03-18 and gave it another try about 20 hours ago.

After about 12 hours of operation my RPi hung up / crashed again and only a hard reboot (power cut) could fix it.

Side info, the RPi is used for weather tracking + timelapsing. It gathers weather data from BMP085, DS18b20 and DHT22 sensors. Images are taken with the Raspberry Pi Camera module rev 1.3.

To my setup:

Raspbery PI model B

Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 8.0 (jessie)
Release:    8.0
Codename:   jessie
Linux carbon 4.1.19+ #858 Tue Mar 15 15:52:03 GMT 2016 armv6l GNU/Linux

root@hostname ~ # python --version
Python 2.7.9
root@hostname ~ # python3 --version
Python 3.4.2

Upon my Pi crashing I get this message in my kern.log file.

As one can easily see, the crash seems to be caused by my weatherPi script which is called every minute via a cronjob. But why?

This is my script:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# This software is used to gather basic environmennt data from sensors
# connected to the Rapsberry Pi Model B. The gathered data is stored into 
# MySQL database and printed to the console.

import os
import sys
import re
import Adafruit_DHT
import Adafruit_BMP.BMP085 as BMP085
import MySQLdb
import time

# make sure script is run as root
euid = os.geteuid()
if euid != 0:
    print('Script not started as root. Running sudo...')
    args = ['sudo', sys.executable] + sys.argv + [os.environ]
    # the next line replaces the currently-running process with the sudo
    os.execlpe('sudo', *args)

# create timestamp
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')

print('Gathering data...')
# grab humidity data from DHT22 sensor
sensor = 22
pin = 4
humidity, temperature_DHT22 = Adafruit_DHT.read_retry(sensor, pin)

# grab barometric data
sensor = BMP085.BMP085()
temperature_bmp9985 = sensor.read_temperature()
pressure_bmp9985  = (sensor.read_pressure()/100)

# grab outside temperature from ds18b20
f = open('/sys/bus/w1/devices/28-00043c9677ff/w1_slave', 'r')
lines = f.read()
match = re.search('t=(-?[0-9]+)', lines)
temperature_18b20 = (float(match.group(1))/1000)

# grab cpu temperature
cpuTempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
cpu_temp = float(cpuTempFile.read())/1000
cpuTempFile.close()

# calculate aproximate dew point
DewPoint = ((humidity / 100) ** 0.125) * (112 + 0.9 * temperature_18b20) + (0.1 * temperature_18b20) - 112

print('Opening database connection')
# connecting to MySQL database and creating cursor
# all queries are executed by the cursor
db = MySQLdb.connect(host="HOST", user="USER", passwd="PASSWORD", db="DATABASE")
cur = db.cursor()

print('Preparing SQL statement')
sql = (('INSERT INTO `weather_data` (`t_out`, `t_out_wall`, `t_case`, `t_cpu`, `humidity`, `pressure`, `dewpoint`) VALUES ({:.2f}, {:.1f}, {:.2f}, {:.2f}, {:.2f}, {:.0f}, {:.2f});').format(temperature_18b20, temperature_DHT22, temperature_bmp9985, cpu_temp, humidity, pressure_bmp9985, DewPoint))

print('Executing SQL statement')
cur.execute(sql)
print('Commiting SQL statement')
db.commit()
print('Closing database connection')
db.close

print('Acquired data:')
print(timestamp)
print('temperature_18b20   =     {:0.2f} °C').format(temperature_18b20)
print('temperature_dht22   =     {:0.1f} °C').format(temperature_DHT22)
print('temperature_bmp9985 =     {:0.2f} °C').format(temperature_bmp9985)
print('temperature_cpu     =     {:0.2f} °C').format(cpu_temp)
print('humidity_dht22      =     {:0.1f}%').format(humidity)
print('pressure_bmp9985    =     {:0.0f} hPa').format(pressure_bmp9985)
print('dew_point           =     {:.2f} °C').format(DewPoint)

This is the crontab line I use (as root):

# gather weather data and commit to database  
* * * * * /usr/local/bin/weatherPi

My questions are:

  • What exactly does take up all the RAM?
  • Why is the RAM not freed up after executing the script?
  • How can I resolve the issue?

Additional info:

I already tried setting a timeout for the weatherPi script I use in cron, so if the script ran longer than 10 seconds, it would be killed with signal 9. This made no difference, whatsoever.

The Pi runs overclocked at Turbo, but this makes no difference to the crashes (tried several settings).

If any other information is needed, please let me know. I don't know any further on how to debug this issue.


EDIT:

After updating my code like the following the script seems to not close anymore. First tests (with same conditions above) crashed the Pi within an hour.

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

# This software is used to gather basic environmennt data
# with the Rapsberry Pi Model B. This software was sepecifically
# designed for one single system

import os
import sys
import re
import Adafruit_DHT
import Adafruit_BMP.BMP085 as BMP085
import MySQLdb
import time

# make sure script is run as root
euid = os.geteuid()
if euid != 0:
    print('Script not started as root. Running sudo...')
    args = ['sudo', sys.executable] + sys.argv + [os.environ]
    # the next line replaces the currently-running process with the sudo
    os.execlpe('sudo', *args)

# create timestamp
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')

print('Gathering data...')
# grab humidity data from DHT22 sensor
sensor = 22
pin = 4
humidity, temperature_DHT22 = Adafruit_DHT.read_retry(sensor, pin)

# grab barometric data
sensor = BMP085.BMP085()
temperature_bmp9985 = sensor.read_temperature()
pressure_bmp9985  = (sensor.read_pressure()/100)

# grab outside temperature from ds18b20
f = open('/sys/bus/w1/devices/28-00043c9677ff/w1_slave', 'r')
lines = f.read()
f.close()
match = re.search('t=(-?[0-9]+)', lines)
temperature_18b20 = (float(match.group(1))/1000)

# grab cpu temperature
cpuTempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
cpu_temp = float(cpuTempFile.read())/1000
cpuTempFile.close()

# calculate aproximate dew point
DewPoint = ((humidity / 100) ** 0.125) * (112 + 0.9 * temperature_18b20) + (0.1 * temperature_18b20) - 112

print('Opening database connection')
# connecting to MySQL database and creating cursor
# all queries are executed by the cursor
db = MySQLdb.connect(host="HOST", user="USER", passwd="PASSWORD", db="DATABASE")
cur = db.cursor()

print('Preparing SQL statement')
sql = (('INSERT INTO `weather_data` (`t_out`, `t_out_wall`, `t_case`, `t_cpu`, `humidity`, `pressure`, `dewpoint`) VALUES ({:.2f}, {:.1f}, {:.2f}, {:.2f}, {:.2f}, {:.0f}, {:.2f});').format(temperature_18b20, temperature_DHT22, temperature_bmp9985, cpu_temp, humidity, pressure_bmp9985, DewPoint))

print('Executing SQL statement')
cur.execute(sql)
print('Commiting SQL statement')
db.commit()
print('Closing database connection')
cur.close()
db.close()

print('Acquired data:')
print(timestamp)
print('temperature_18b20   =     {:0.2f} °C').format(temperature_18b20)
print('temperature_dht22   =     {:0.1f} °C').format(temperature_DHT22)
print('temperature_bmp9985 =     {:0.2f} °C').format(temperature_bmp9985)
print('temperature_cpu     =     {:0.2f} °C').format(cpu_temp)
print('humidity_dht22      =     {:0.1f}%').format(humidity)
print('pressure_bmp9985    =     {:0.0f} hPa').format(pressure_bmp9985)
print('dew_point           =     {:.2f} °C').format(DewPoint)

Asked by tac:

Output of cat /sys/bus/w1/devices/28-00043c9677ff/w1_slave

56 00 55 00 7f ff 0c 10 7b : crc=7b YES
56 00 55 00 7f ff 0c 10 7b t=5375

This is just the temperature reading of the ds18b20 I2C temperature sensor on the bus.

Update 24.03.2016:

After using the suggested fixes from @tac all seemed to work fine for about 24 hours. But suddenly the RPi crashed again with the same kern.log which indicated that there was a mass of my weatherPi scripts running.

My guess is that the script starts hanging if the database connection cannot be established. From the Python MySQL docs I found that you can set a timeout for MySQLdb.connect. I added a 15 second timeout for testing purposes.

MySQLdb.connect(host="HOST", user="USER", passwd="PASSWORD", db="DATABASE", connect_timeout=15)

Another change that I made was to add a killall to the crontab, to assure that all remaining scripts are stopped before running a new one:

# gather weather data and commit to database
* * * * * killall weatherPi && /usr/local/bin/weatherPi

Of course the connection timeout is currently redundant because of the prefixed killall. I currently want to see if this helps at all. After some testing of about 48 hours I will remove this command again and try out my connection timeout solution solely

12
  • 1
    @PandaLion98 I typed my whole answer on a phone! :D
    – cat
    Mar 20, 2016 at 13:21
  • 1
    @tac because anything higher than python2.7 is not supported by the libraries used for the BMP085 and DHT22
    – Flatron
    Mar 20, 2016 at 13:28
  • 1
    What are the contents of /sys/bus/w1/devices/28-00043c9677ff/w1_slave? Just a few numbers?
    – cat
    Mar 20, 2016 at 18:32
  • 1
    Oh, I didn't even read the message log at the link earlier. Now I see.
    – cat
    Mar 20, 2016 at 18:37
  • 1
    I've updated my answer.
    – cat
    Mar 20, 2016 at 19:20

2 Answers 2

3
  1. You're not closing your database properly. Check line 66 of your file.
    • After you're done with your database, issue a db.close().
  2. You're also not closing your f file. Check line 39.
    • Same with #1. Issue a f.close() after you're done using your f variable.
1
  • Also thanks to you for pointing this out as @tac did!
    – Flatron
    Mar 20, 2016 at 14:41
2

First time I read this script, I completely overlooked the following.

euid = os.geteuid()
if euid != 0:
    print('Script not started as root. Running sudo...')
    args = ['sudo', sys.executable] + sys.argv + [os.environ]
    # the next line replaces the currently-running process with the sudo
    os.execlpe('sudo', *args)

Notice anything?

the next line replaces the currently-running process with the sudo

Oh.

Let's take a look at that message log.

[ pid ]   uid  tgid total_vm      rss nr_ptes nr_pmds swapents oom_score_adj name
[   86]     0    86     2026      611       8       0       29             0 systemd-journal
[   88]     0    88     2826        0       8       0       98         -1000 systemd-udevd
[  401]     0   401     1265       27       6       0       22             0 cron
[  407]     0   407      847       21       6       0       30             0 systemd-logind
[  417]   105   417      994       34       5       0       37             0 avahi-daemon
[  420]   104   420     1370       44       6       0       41          -900 dbus-daemon
[  440]   105   440      966        5       5       0       50             0 avahi-daemon
[  458] 65534   458      570       26       5       0       26             0 thd
[  465]     0   465     1892      544       7       0      356             0 tmux
[  472]     0   472      475        1       5       0       17             0 sh
[  475]     0   475     1173       55       5       0        7             0 pilapse
[  482]     0   482     7843      314      10       0      128             0 rsyslogd
[  484]     0   484     1784       28       6       0       74             0 wpa_supplicant
[  742]     0   742      638       22       5       0       48             0 dhcpcd
[  743]     0   743     1963       28       7       0       86         -1000 sshd
[  761]   106   761     1439       79       8       0       68             0 ntpd
[  763]     0   763     1010        0       6       0       31             0 agetty
[  764]     0   764      965        0       4       0       31             0 agetty
[21469]     0 21469     1472       16       6       0       55             0 cron
[21482]     0 21482      475        0       4       0       19             0 sh
[21486]     0 21486     3865        0      10       0      964             0 weatherPi
[21506]     0 21506     1472       16       6       0       55             0 cron
[21522]     0 21522      475        0       5       0       19             0 sh
[21523]     0 21523     3865        0      10       0      964             0 weatherPi
[21540]     0 21540     1472       16       6       0       55             0 cron
[21556]     0 21556      475        0       5       0       19             0 sh
[21557]     0 21557     3865        0      11       0      964             0 weatherPi
[21577]     0 21577     1472       16       6       0       55             0 cron
[21590]     0 21590      475        0       4       0       19             0 sh
[21592]     0 21592     3865        0      11       0      964             0 weatherPi
[21611]     0 21611     1472       16       6       0       55             0 cron
[21624]     0 21624      475        0       4       0       19             0 sh
[21626]     0 21626     3865        0      11       0      964             0 weatherPi
[21650]     0 21650     1472       16       6       0       55             0 cron
[21663]     0 21663      475        0       5       0       19             0 sh
[21667]     0 21667     3865        0      11       0      964             0 weatherPi
[21769]     0 21769     1472       16       6       0       55             0 cron
[21782]     0 21782      475        0       5       0       19             0 sh
[21784]     0 21784     3865        0      10       0      964             0 weatherPi
[21801]     0 21801     1472       16       6       0       55             0 cron
[21817]     0 21817      475        0       5       0       19             0 sh
[21821]     0 21821     3865        0      11       0      964             0 weatherPi
[21840]     0 21840     1472       16       6       0       55             0 cron
[21853]     0 21853      475        0       5       0       19             0 sh
[21855]     0 21855     3865        0      11       0      964             0 weatherPi
[21877]     0 21877     1472       16       6       0       55             0 cron
[21890]     0 21890      475        0       4       0       19             0 sh
[21892]     0 21892     3865        0       9       0      964             0 weatherPi
[21911]     0 21911     1472       16       6       0       55             0 cron
[21924]     0 21924      475        0       4       0       19             0 sh
[21926]     0 21926     3865        0      10       0      964             0 weatherPi
[21948]     0 21948     1472       16       6       0       55             0 cron
[21961]     0 21961      475        0       5       0       19             0 sh
[21963]     0 21963     3865        0      10       0      964             0 weatherPi
[21982]     0 21982     1472       16       6       0       55             0 cron
[21994]     0 21994      475        0       5       0       19             0 sh
[21995]     0 21995     3865        0      10       0      964             0 weatherPi
[22014]     0 22014     1472       16       6       0       55             0 cron
[22029]     0 22029      475        0       4       0       19             0 sh
[22030]     0 22030     3865        0      11       0      964             0 weatherPi
[22053]     0 22053     1472       16       6       0       55             0 cron
[22066]     0 22066      475        0       5       0       19             0 sh
[22068]     0 22068     3865        0      11       0      964             0 weatherPi
[22085]     0 22085     1472       16       6       0       55             0 cron
[22098]     0 22098      475        0       5       0       19             0 sh
[22102]     0 22102     3865        0      11       0      964             0 weatherPi
[22138]     0 22138     1472       16       6       0       55             0 cron
[22156]     0 22156      475        0       4       0       19             0 sh
[22159]     0 22159     3865        0       9       0      964             0 weatherPi
[22696]     0 22696     1472       16       6       0       55             0 cron
[22710]     0 22710      475        0       4       0       19             0 sh
[22712]     0 22712     3865        0       9       0      964             0 weatherPi
[22945]     0 22945     1472       16       6       0       55             0 cron
[22961]     0 22961      475        0       4       0       19             0 sh
[22962]     0 22962     3865        0      11       0      964             0 weatherPi
[22977]     0 22977     1472       16       6       0       55             0 cron
[22990]     0 22990      475        0       4       0       19             0 sh
[22992]     0 22992     3865      225      10       0      739             0 weatherPi
[23016]     0 23016     1472       64       6       0        7             0 cron
[23029]     0 23029      475       19       4       0        0             0 sh
[23031]     0 23031     3865      964      11       0        0             0 weatherPi
[23048]     0 23048     1472       66       6       0        5             0 cron
[23061]     0 23061      475       19       5       0        0             0 sh
[23063]     0 23063     3865      964      10       0        0             0 weatherPi
[23087]     0 23087     1472       66       6       0        5             0 cron
[23103]     0 23103      475       19       5       0        0             0 sh
[23104]     0 23104     3865      964      11       0        0             0 weatherPi
[23119]     0 23119     1472       66       6       0        5             0 cron
[23132]     0 23132      475       19       4       0        0             0 sh
[23134]     0 23134     3865      964      11       0        0             0 weatherPi
[23158]     0 23158     1472       66       6       0        5             0 cron
[23171]     0 23171      475       19       5       0        0             0 sh
[23174]     0 23174     3865      964      10       0        0             0 weatherPi
[23190]     0 23190     1472       66       6       0        5             0 cron
[23203]     0 23203      475       19       5       0        0             0 sh
[23205]     0 23205     3865      964      10       0        0             0 weatherPi
[23312]     0 23312     1472       66       6       0        5             0 cron
[23325]     0 23325      475       19       5       0        0             0 sh
[23327]     0 23327     3865      964      10       0        0             0 weatherPi
[23344]     0 23344     1472       66       6       0        5             0 cron
[23360]     0 23360      475       19       5       0        0             0 sh
[23362]     0 23362     3865      964      11       0        0             0 weatherPi
[23383]     0 23383     1472       66       6       0        5             0 cron
[23396]     0 23396      475       19       4       0        0             0 sh
[23400]     0 23400     3865      964      10       0        0             0 weatherPi
[23415]     0 23415     1472       66       6       0        5             0 cron
[23430]     0 23430      475       19       5       0        0             0 sh
[23431]     0 23431     3865      964      10       0        0             0 weatherPi
[23454]     0 23454     1472       66       6       0        5             0 cron
[23469]     0 23469      475       19       5       0        0             0 sh
[23470]     0 23470     3865      964      10       0        0             0 weatherPi
[23486]     0 23486     1472       66       6       0        5             0 cron
[23501]     0 23501      475       19       5       0        0             0 sh
[23503]     0 23503     3865      964      11       0        0             0 weatherPi
[23526]     0 23526     1472       66       6       0        5             0 cron
[23542]     0 23542      475       19       4       0        0             0 sh
[23543]     0 23543     3865      964      10       0        0             0 weatherPi
[23558]     0 23558     1472       66       6       0        5             0 cron
[23571]     0 23571      475       19       5       0        0             0 sh
[23573]     0 23573     3865      964      10       0        0             0 weatherPi
[23597]     0 23597     1472       66       6       0        5             0 cron
[23610]     0 23610      475       19       5       0        0             0 sh
[23613]     0 23613     3865      964      10       0        0             0 weatherPi
[23629]     0 23629     1472       66       6       0        5             0 cron
[23642]     0 23642      475       19       5       0        0             0 sh
[23644]     0 23644     3865      964      11       0        0             0 weatherPi
[23711]     0 23711     1472       66       6       0        5             0 cron
[23724]     0 23724      475       19       5       0        0             0 sh
[23727]     0 23727     3865      964       9       0        0             0 weatherPi
[23743]     0 23743     1472       66       6       0        5             0 cron
[23757]     0 23757      475       19       4       0        0             0 sh
[23758]     0 23758     3865      964      10       0        0             0 weatherPi
[23782]     0 23782     1472       66       6       0        5             0 cron
[23795]     0 23795      475       19       5       0        0             0 sh
[23798]     0 23798     3865      964      10       0        0             0 weatherPi
[23815]     0 23815     1472       66       6       0        5             0 cron
[23828]     0 23828      475       19       6       0        0             0 sh
[23830]     0 23830     3865      964      11       0        0             0 weatherPi
[23854]     0 23854     1472       66       6       0        5             0 cron
[23867]     0 23867      475       19       5       0        0             0 sh
[23871]     0 23871     3865      964      11       0        0             0 weatherPi
[23886]     0 23886     1472       66       6       0        5             0 cron
[23899]     0 23899      475       19       5       0        0             0 sh
[23901]     0 23901     3865      964      10       0        0             0 weatherPi
[24010]     0 24010     1472       66       6       0        5             0 cron
[24022]     0 24022      475       19       5       0        0             0 sh
[24023]     0 24023     3865      964      10       0        0             0 weatherPi
[24042]     0 24042     1472       66       6       0        5             0 cron
[24055]     0 24055      475       19       5       0        0             0 sh
... 100 more lines ...
Out of memory: Kill process 24097 (weatherPi) score 7 or sacrifice child
Killed process 24097 (weatherPi) total-vm:15460kB, anon-rss:3856kB, file-rss:0kB

Specifically, the name column. sh cron weatherPi sh cron weatherPi ....

For whatever reason (perhaps cron or sh weren't started with sudo, os.geteuid is wrong for a number of reasons, etc), this process just spawns itself over and over again, eventually invoking the OOM Killer. It's what's known as a fork bomb.

It's (sort of) equivalent to while 1: os.fork().

What else? Well,

args = ['sudo', sys.executable] + sys.argv + [os.environ]

???
args now probably resembles:

["sudo", "/usr/bin/python", "", {'HISTTIMEFORMAT': '%d-%m-%y %H:%M ', 'MDMSESSION': 'cinnamon', 'MDM_LANG': 'en_US.UTF-8', 'SHELL': '/bin/bash', 'XDG_DATA_DIRS': '/usr/share/cinnamon:/usr/share/gnome:/usr/local/share/:/usr/share/:/usr/share/mdm/', 'MANDATORY_PATH': '/usr/share/gconf/cinnamon.mandatory.path', 'HISTSIZE': '', 'WINDOWPATH': '8', 'MANPATH': ':/usr/man:/usr/local/man:/usr/local/share/man:/usr/share/man', 'XSCH_PARAM_NAME': '/usr/etc/xsch.par', 'LIBVIRT_DEFAULT_URI': 'qemu:///system', 'XDG_RUNTIME_DIR': '/run/user/1000', 'gvm_go_name': 'go1.6', 'XDG_SESSION_ID': 'c1', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-xFITmI638B,guid=0f94f5b7d8be3b5d2dd8d09d56edb8f9', 'DESKTOP_SESSION': 'cinnamon', 'MBK_VSS': 'vss', 'XFSM_PARAM_NAME': '/usr/etc/xfsm.par', 'MBK_OUT_PH': 'ap', 'DREAL_TECHNO_NAME': '/usr/etc/cmos.dreal', 'MBK_CATA_LIB': '.:/usr/cells/sxlib:/usr/cells/dp_sxlib:/usr/cells/rflib:/usr/cells/rf2lib:/usr/cells/ramlib:/usr/cells/romlib:/usr/cells/pxlib:/usr/cells/padlib', 'MBK_WORK_LIB': '.', 'MBK_SPI_MODEL': '/usr/etc/spimodel.cfg', 'gvm_pkgset_name': 'global', 'XDG_CURRENT_DESKTOP': 'X-Cinnamon', 'USER': 'cat', 'XDG_VTNR': '8', 'PS1': '\\[\\e[1;35m\\]`nonzero_return`\\[\\e[1;34m\\]\\u @ \\[\\e[1;32m\\]\\h : \\[\\e[1;31m\\]\\w\\[\\e[1;32m\\]`git_prompt` \\[\\e[1;35m\\]$ \\[\\e[m\\]', 'GRAAL_TECHNO_NAME': '/usr/etc/cmos.graal', 'SESSION_MANAGER': 'local/mint-kitty:@/tmp/.ICE-unix/2257,unix/mint-kitty:/tmp/.ICE-unix/2257', 'SHLVL': '1', 'DISPLAY': ':0', 'QT_ACCESSIBILITY': '1', 'WINDOWID': '88080390', 'rc': '/home/cat/.bashrc', 'CLUTTER_BACKEND': 'x11', 'XDG_SESSION_DESKTOP': 'cinnamon', 'ED': 'vim', 'GVM_VERSION': '1.0.22', 'GDMSESSION': 'cinnamon', 'PKG_CONFIG_PATH': '/home/cat/.gvm/pkgsets/go1.6/global/overlay/lib/pkgconfig:', '_': '/usr/bin/python', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-cinnamon:/etc/xdg', 'MBK_VDD': 'vdd', 'GOPATH': '/home/cat/.gvm/pkgsets/go1.6/global', 'GVM_PATH_BACKUP': '/home/cat/.gvm/bin:/home/cat/.rakudobrew/bin:/home/cat/.rakudobrew/moar-nom/install/share/perl6/site/bin:/home/cat/.rakudobrew/bin:/home/cat/.rakudobrew/moar-nom/install/share/perl6/site/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games', 'ALLIANCE_TOP': '/usr', 'HOME': '/home/cat', 'MBK_IN_LO': 'vst', 'LD_LIBRARY_PATH': '/home/cat/.gvm/pkgsets/go1.6/global/overlay/lib:', 'LANG': 'en_US.UTF-8', 'RDS_IN': 'cif', 'MBK_SCALE_X': '100', 'VH_DLYSFX': 'dly', 'MBK_CATAL_NAME': 'CATAL', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'GVM_ROOT': '/home/cat/.gvm', 'USERNAME': 'cat', 'GDM_XSERVER_LOCATION': 'local', 'DESKTOP_AUTOSTART_ID': '106c5170b96fb63af14584199627359200000022570002', 'VTE_VERSION': '4002', 'DYLD_LIBRARY_PATH': '/home/cat/.gvm/pkgsets/go1.6/global/overlay/lib:', 'GENVIEW_TECHNO_NAME': '/usr/etc/cmos.genview', 'MBK_OUT_LO': 'vst', 'DEFAULTS_PATH': '/usr/share/gconf/cinnamon.default.path', 'MONKEYSPHERE_VALIDATION_AGENT_SOCKET': 'http://127.0.0.1:40789', 'MDM_XSERVER_LOCATION': 'local', 'LOGNAME': 'cat', 'XDG_SEAT': 'seat0', 'GOROOT': '/home/cat/.gvm/gos/go1.6', 'PATH': '/home/cat/.gvm/pkgsets/go1.6/global/bin:/home/cat/.gvm/gos/go1.6/bin:/home/cat/.gvm/pkgsets/go1.6/global/overlay/bin:/home/cat/.gvm/bin:/home/cat/.gvm/bin:/home/cat/.rakudobrew/bin:/home/cat/.rakudobrew/moar-nom/install/share/perl6/site/bin:/home/cat/.rakudobrew/bin:/home/cat/.rakudobrew/moar-nom/install/share/perl6/site/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games', 'SSH_AGENT_PID': '2340', 'TERM': 'xterm-256color', 'RDS_OUT': 'cif', 'VH_MAXERR': '10', 'MBK_C4_LIB': './cellsC4', 'HISTFILESIZE': '', 'RDS_TECHNO_NAME': '/usr/etc/cmos.rds', 'VH_PATSFX': 'pat', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'VH_BEHSFX': 'vbe', 'MBK_TARGET_LIB': '/usr/cells/sxlib', 'ELP_TECHNO_NAME': '/usr/etc/prol.elp', 'XAUTHORITY': '/tmp/.mdmVE82DY', 'OLDPWD': '/home/cat', 'XPAT_PARAM_NAME': '/usr/etc/xpat.par', 'HISTCONTROL': 'ignoredups', 'PWD': '/home/cat/projects/py', 'GVM_OVERLAY_PREFIX': '/home/cat/.gvm/pkgsets/go1.6/global/overlay', 'MBK_IN_PH': 'ap'}

At least, that's what it does for me. I'm not sure why you're adding os.environ to the executable args.

'sudo', *args

This is equivalent to:

"sudo", "sudo", "/usr/bin/python", "", # the rest of the environ from above

Well, that's pretty clearly wrong.

From help(os.execlpe):

execlpe(file, *args, env)

Execute the executable file (which is searched for along $PATH) with argument list args and environment env, replacing the current process.

The arguments to os.execlpe should probably be:

os.execlpe("sudo", args, os.environ())

Where args is not unpacked.

This will probably be subject to the same bug as before, perhaps due to escalation issues on Raspbian.

So scrap that, and do the following instead:

  1. in a script called start_weatherPi.sh:

    #!/bin/sh
    if [ $(id -u) -eq 0 ]; then 
        exec "python weatherPi.py"
    else 
        echo 'Need to be root!'
        exit
    fi
    
  2. In weatherPi.py:

    import blah, blah, blah
    
    assert os.geteuid() == 0, "You need to be root! (call to geteuid didn't return 0!)"
    
    # rest of script as root ....
    

Then, put /path/to/start_weatherPi.sh in cron.

Spawning processes unchecked is, as we've seen, dangerous. Please: use a helper script that replaces itself with python instead, and don't autospawn new processes, because a fork bomb is what you get. :P

7
  • 1
    Thanks for this answer, yes this seems obvious, I fixed the issues, I'll see how this goes within the next 24 hours.
    – Flatron
    Mar 20, 2016 at 14:41
  • 1
    as requested, I added the updated code to the question itself.
    – Flatron
    Mar 20, 2016 at 18:26
  • 1
    Really thanks for this extended answer :) As I only run the script as root the line is redundant. I removed it and my tests are now running. I will keep you updated.
    – Flatron
    Mar 20, 2016 at 19:34
  • 1
    Okay, to keep you updated: As stated in my question I updated the code as you suggested. So far it looked good and worked for about 24 hours. After that the RPi crashed with the same kern.log. My guess is that the script hangs on the connection part, when the connection to the main server cannot be established for whatever reason. To avoid this I added connect_timeout=15 to the MySQLdb.connect statement in order to avoid this. The other thing is that I updated the crontab entry and added a killall weatherPi && ... to assure that all remaining running scripts are killed.
    – Flatron
    Mar 24, 2016 at 9:02
  • 1
    @Flatron What about adding a simple locking mechanism in the script itself to avoid multiple running instances (or at least the risk of it)? E.g. check for the existence of a pid file at start up. If not found, write one and run, otherwise quit.
    – jDo
    Mar 24, 2016 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.