0

I don't know where to post this question because it's half a programming question and half a Linux question, so forgive me.

I'm working on a project using Beaglebone Black and I need to execute a very complex python script automatically after the boot, so I followed this tutorial:

(https://stackoverflow.com/questions/28854705/executing-a-script-on-startup-using-beaglebone-black)

which worked, but the script is executed in the / directory and not in the correct directory, which is /root/pyfingerprint/examples/, so when the program tries to execute things in the examples directory it can't find them.

I tried adding a complete path for every line of code which required it and it worked for some parts of the project but it's not enough for cv2 and other libraries.

I tried to solve the problem simply adding os.system("cd /root/pyfingerprint/examples/") at the beginning of the python script but when the program execute on boot it says that the current directory is still /

The program works perfectly when executed manually but not on boot time.

Is there a way to set the working directory of the service to automatically execute it in the right directory?

  • You should `cd` in shell before execute main python script, not `cd` inside the python script. You should show us how you execute your main python script if you need further assist. – 林果皞 Jun 04 '20 at 17:41
  • I'm not an expert on creating services, I just need to edit my .sh script and that's it or I need to do some other operations after that? – Emanuele Sabato Jun 04 '20 at 17:44
  • You need to show us the code inside shell script(.sh) which run the python script(s). Is there any issue if you put `cd /root/pyfingerprint/examples/;` inside shell script before run the python script(s)? – 林果皞 Jun 04 '20 at 17:52
  • Or you may want to do this: https://unix.stackexchange.com/a/200657/64403 – 林果皞 Jun 04 '20 at 17:58
  • 1
    It worked! i added cd /root/pyfingerprint/examples/ before launching the script. Thanks for the help! – Emanuele Sabato Jun 04 '20 at 18:17
  • 1
    Note, `os.system()` has no effect as it creates a new process in which to do the operation. Instead use `os.chdir('/....')` – meuh Jun 04 '20 at 18:31

1 Answers1

0

As suggested by Fruit the solution is to add cd [dir] in the .sh script to launch the program. in my case the script is:

#!/bin/bash
cd /root/pyfingerprint/examples/
python3 main.py