4

I created a Django project which communicate another Python process (called gateway). All works fine. Both projects are using virtualenv, so, it's necessary activate virtualenv to execute the projects

To execute gateway, I open a terminal:

#activate virtual env:
pi@raspberrypi: source venv/bin/activate

#go to archive folder
(venv) pi@raspberry cd PFCProject/PFC

#executes
(venv) pi@raspberry python arduinoserver.py

After, I have to initialize django server:

#activate virtual env:
pi@raspberrypi: source venv/bin/activate

#go to archive folder
(venv) pi@raspberry cd PFCProject/PFC

#executes
(venv) pi@raspberry python manage.py runserver 0.0.0.0:8000

I have to open 2 terminals and executes the commands in each one.

The question is: how can I create commands, which execute the above tasks when Raspberry starts? It's necessary to execute gateway before Django project.

  • What version of Raspbian you are using? I'm looking for a solution with systemd. Have you made two bash scripts? Does they run on the command line? Can you put all commands in one script? – Ingo May 3 '18 at 17:58
1

You can always use rc.local. It's run on startup.

You need to use absolute paths, though. And I'd recommend moving your arduinoserver.py script to your virtualenv's bin directory as per this documentation, or this won't work.

It looks like you don't actually have to "activate" virtualenv (see above link), as long as things are in the right directory.

So to do this, I'd recommend making your python scripts executable, and then running them as the default user. (See below rc.local commands.) Note that you'd want your #!/path/to/python to point to your virtualenv's python interpreter, and not your system's default.

sudo nano /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# *** Your scripts/commands go here.
su -s /bin/bash -c /path/to/your/script/arduinoserver.py your_username&
su -s /bin/bash -c /path/to/your/script/manage.py runserver 0.0.0.0:8000 your_username&

exit 0
0

install nano if havent already done so:

sudo apt-get install nano

then create a new shell file:

cd /home/pi

nano onstartup.sh

should be a blank file. add your scripts. save the file.

open cron tab:

crontab -e

if prompted for text editor press enter for nano add:

@reboot /home/pi/onstartup.sh

hope it helped!

  • Thanks! last question: actually , i have to open 2 terminals and executes the commands in each one. In this case, i have to write 2 shell files? it's possible to add 2 files in crontab and make one shell executes after other? – FelipeFonsecabh Nov 3 '17 at 1:01
  • yes just do @reboot then the 2 files the first to exicute needs to be put in first! – User98764431 Nov 3 '17 at 1:28
  • @FelipeFonsecabh if that does not work in cron tab, use ths: @reboot /home/pi/onstartup2.sh | at now + 2 minutes this will garintee that the first starts the after 2 minutes the second shell script will run. Hope it helps! Can you accept this answer? – User98764431 Nov 3 '17 at 1:37
  • Hi! No one shell file works When i use the crontab -e command, it creates a crontab file in tmp folder. After restart, the file does'nt not executed and it disappear. I try to edit the crontab file in /etc folder, but not works to. Any idea? – FelipeFonsecabh Nov 3 '17 at 2:58
  • are you on raspberry pi 3? newest version? – User98764431 Nov 3 '17 at 12:02
0

As with User98764431's answer, you can create a shell executable and run it in cron, but this doesn't seem to work, you say. Just create 2 shell files containing your separate scripts and add this crontab entry: @reboot sh firstfile.sh && sh secondfile.sh. This runs the first shell file then the other.

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.