2

I have written the following script which is supposed to change directory on startup and then run the script as pi user (on raspberry pi):

#!/bin/bash
echo 'raspberry' | sudo su - pi -c 'cd /home/pi/workspace/counting-objects && python3 script.py

If I run the script, I get the following error:

Traceback (most recent call last):
  File "script.py", line 23, in <module>
    from custom_module...
ImportError: No module named 'custom_module'

If I manually change the folder on login and then run the same script from console, everything works as expected.

Does anyone know how to fix the bash script in order to run correctly the script as pi user after changing directory?

Thanks!

1

Eschew the shell script entirely, throw out sudo because you don't need it, and in the pi user's cron table, add:

@reboot cd /home/pi/workspace/counting-objects && python3 script.py

by logging in as the pi user, and editing the table with crontab -e.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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