3

I have bash script which loads a virtualenv and run 3 python scripts.

I have tried to init on startup by ini.d and rc.local methods but it doesnt work and I have no idea why is not working but if I start it manually it works

the content of run.sh is

#!/bin/bash
source ~/.profile
workon cv
python socket1.py & python socket2.py & python socket3.py
  • 1
    init.d and rc.local are not aware of your user id, it uses root, so ~/.profile is not where you think it is. Try using the full path. BTW: why do you start the first script with 'python' and the other 2 using the scripts' executable attributes/ shebang settings (check what the ampersand does on the command line) – Dirk Apr 10 '19 at 8:01
3

I have solved it by using

source /home/pi/.virtualenvs/cv/bin/activate

instead of

source ~/.profile & workon cv
| improve this answer | |
1

You can use a systemd Unit file to start your script. With it you are able to exactly set the environment to that one used on the command line. The only difficulty is to find the environment needed for your programs. I suggest to start with this Unit file:

rpi ~$ sudo systemctl --full --force edit myrun.service

In the empty editor insert these statements, save them an quit the editor:

[Unit]
Description=My run script
After=multi-user.target

[Service]
User=pi
WorkingDirectory=/path/to/scripts
Environment="ENV_VAR=important variable"
ExecStart=/path/to/run.sh

[Install]
WantedBy=multi-user.target

You may have a look at man systemd.exec to see what's possible with environment. It is also important, what output your scripts have, if it is text only or graphical. We will see. Enable the service with:

rpi ~$ sudo systemctl enable myrun.service

and reboot.

Here some commands for troubleshooting:

rpi ~$ sudo systemctl --full edit myrun.service   # edit again
rpi ~$ systemctl status myrun.service             # get status of the service
rpi ~$ journalctl -b -e                 # looking at the journal for all logs
| improve this answer | |
  • Thanks for the answer but i have solved it by using "source /home/pi/.virtualenvs/myVirtualEnv/bin/activate" instead of "source ~/.profile & workon cv" – Jandako Apr 12 '19 at 11:03
  • @Jandako Please make an answer with the final solution so other users can benefit of it. It will also finish the question. – Ingo Apr 12 '19 at 11:44

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.