3

I have created a smart vending machine using my Raspberry Pi. For now, I open the Raspberry Pi using SSH and run the script manually for every transaction.

I want to automate the process and run the script on startup. After execution I want it to run again in a loop till shut down.

If possible I can also map it to a physical button which I connect to the Raspberry Pi and whenever the button is pressed the script should run using Python 3.

How can I possibly do any of the above two things?

4

Your script is a typical use of a service. Usually a service is started once and then it is running in background until it is stopped by the service manager. The service manager can restart a script but it isn't made to be used for loops because it is working on system level with logging and dependency checking and all to manage services.

So first you should program the endless loop within the script. Within this loop you can also check if the button is pressed and do what is needed then.

The default init system and service manager is systemd on Raspbian and it manages services with Unit files. So you should start with a simple Unit file for your service with:

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

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

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

[Service]
ExecStart=/full/path/to/myscript.py

[Install]
WantedBy=multi-user.target

Then enable it to be started on boot up:

rpi ~$ sudo systemctl enable myscript.service

You can look at it's status with:

rpi ~$ systemctl status myscript.service

It may be that it isn't running on the first attempt because your script needs some environment conditions. We will see. For some environment settings you can look at man systemd.exec.

2

We were able to use Supervisor to successfully have a Python script run in the background on boot.

Tutorial I Used to set it up: Monitoring Processes with Supervisord

Supervisor runs as a service, and you have a configuration file where you set up your scripts that you want it to run:

[program:your_script_name]
command=python3 your_script_name.py
directory=/your/file/location/here
autostart=true
autorestart=true

You could either have Supervisor run your vending machine scripts on start up or start a script that is waiting for your button press which would then launch your main vending machine script.

Steps: (Using terminal)

sudo apt-get install -y supervisor

Start the service

sudo service supervisor start

Create your configuration information:

sudo nano /etc/supervisor/conf.d/yourscriptname.conf

Enter the configuration information and save the file:

[program:your_script_name]
command=python3 your_script_name.py
directory=/your/file/location/here
autostart=true
autorestart=true

Update Supervisor to include your new configuration file:

supervisorctl reread
supervisorctl update

See if your service started:

supervisorctl

Start and stop the your script from running:

supervisorctl stop your_script_name
supervisorctl start your_script_name

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.