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
Jandako
  • 61
  • 1
  • 5

2 Answers2

3

I have solved it by using

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

instead of

source ~/.profile & workon cv
Greenonline
  • 2,969
  • 5
  • 27
  • 38
Jandako
  • 61
  • 1
  • 5
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
Ingo
  • 42,961
  • 20
  • 87
  • 207