How to add and run python script by 24/7 using Bolt iot module?

I’m created laser security system using Bolt iot and for code I used pycharm to run the program
It’s has two files 1) config.py (which includes Bolt iot api key and other authentication token)
2) main.py (which includes main system script that runs ) . Anyone done laser security system using LDR(Light dependent resistor)? I have bro make this project to advanced . Anyone have suggestions ?

Hi @rjmail200,

You can use supervisor to run your program 24/7.

  1. So first you have to install the supervisor and your laptop using the below command.
sudo apt-get install supervisor
  1. So before you go ahed you need three information, what is the path of python program, what is the path of the file that you want to run, and current working directly.

a) So to find the path of python program, type

sudo which python3

In my case it return the /usr/bin/python3 for python program.

b) To find the current working directory

type the below command

pwd

In my case it returns /home/ubuntu in your case the pwd value could be different and my main.py file is inside the /home/ubuntu.

c) So the file path will be /home/ubuntu/main.py

  1. Then you have to create a configuration file inside this folder /etc/supervisor/conf.d. Mainly this file contains the information about which file to run, where should output of the program will be stored. etc.
cd /etc/supervisor/conf.d
sudo nano myprogram.conf
  1. and inside the myprogram.conf file, write the below details. (Replace the code based on your path details)
[program:myprogram]
command=/usr/bin/python3 /home/ubuntu/main.py
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=3
stderr_logfile=/home/ubuntu/test.err.log
stdout_logfile=/home/ubuntu/test.out.log
user=ubuntu

and then reread the conf file and update it using the below commands.

sudo supervisorctl reread
sudo supervisorctl update

Then Now open the supervisorctl console by typing the below command and start the program myprogram

sud supervisorctl
start myprogram

And you can check the output of the code in /home/ubuntu/test.out.log.

To check the output, type the below command

sudo tail -f /home/ubuntu/test.out.log

Do let me know in case you need further assitance.

1 Like

Thank you for posting the solution . but i’m using windows 7 low end pc. how can i run this cmds in windows? do you have any other solution? let me know.

Hi @rjmail200,

I am not sure if windows has such feature. You can make your program run in background https://stackoverflow.com/questions/59125493/how-to-constantly-run-python-script-in-the-background-on-windows But i have never tried if before.