0

Using:

  • python 3.9.2
  • AWS greengrass 2
  • Raspberry Pi

I need to use dbm to write a value from command line when commissioning a thing and then read from that database file when a python greengrass script runs. Logged in as user "pi" I use the following from command line:

sudo runuser -l ggc_user -c 'python3'

and then I set the variable in python:

import dbm
with dbm.open('cache', 'c') as db:
    db['topic_prefix'] = 'testing'

I have confirmed that db['topic_prefix'] using:

sudo runuser -l ggc_user -c 'python3'

and then I read the variable in python:

import dbm
with dbm.open('cache', 'c') as db:
     print(db.get('topic_prefix'))

Now I run this python greengrass script as used ggc_user. Here is a snippet:

with dbm.open('cache', 'r') as db:
    topic = db.get('topic_prefix', b'undefined').encode('ASCII')
    print("Topic prefix set as: " + topic)
    topic = topic + "/" + os.getenv("AWS_IOT_THING_NAME")

I get the error:

error[0]("db file doesn't exist; ".

What am I doing wrong? How can I write a database file that can persist through reboots and be accessible by ggc_user?

Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
  • `runuser` defaults to not changing the directory. Have you made sure it has read and write access to file `cache`? – Lutz Prechelt Sep 16 '22 at 17:16
  • I am confused by your comment. When I run python as ggc_user it CAN write to the database. IT CAN read to the database. When the script runs it cannot read the database file. What are you meaning when you say runuser defaults to not changing directory. Does the database file exist in that directory when it is created? When you say to make sure "it" has read and write access" what is "it". The user, group? Python? greengrass??? – Eric Snyder Sep 16 '22 at 21:24
  • I did find the file in the user directory. It has the permissions -rw-r--r-- 1 ggc_user ggc_user 12288 Sep 16 22:41 greengrass.db so I would think the script running as the owning user could read it. – Eric Snyder Sep 16 '22 at 22:03
  • Check if your script is executed in the directory in which it should be running by making it print the current working dir path. – Lutz Prechelt Sep 19 '22 at 09:13

0 Answers0