0

I have four python scripts which runs at the boot time. I have written a shell script where in it includes all the four python scripts

My shell script is as follows: sudo nano launcher.sh

#!/bin/sh    
cd / 
cd /home/pi/myfile  
python3 script1.py &    
python3 script2.py &    
python3 script3.py &    
python3 script4.py &    
cd /

i have scheduled the shell script using a crontab to run the scripts at boot time.

sudo crontab -e

@reboot  sh /home/pi/myfile/launcher.sh &

sudo reboot

All the scripts are running at the backgroud except the script 3 where in the script3 accepts the input from the server and executes the script accordingly.(basically switches the lights on and off by recieving the input from the server)

I want to run all the four scripts parallelly. Since the script 3 takes the input from the server, it is not running at the background. Kindly provide me the solution so that all the scripts runs at the boot time.

Note: All the four scripts runs when i run the launcher manually using the shell script. i run it using the command sh launcher.sh

script3.py

#!/usr/bin/env python3
import RPi.GPIO as GPIO
from requests import ReadTimeout, ConnectTimeout, HTTPError, Timeout, ConnectionError
import os,time,datetime,urllib3,json
import urllib.request
import requests
from time import sleep
import ast
import json

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

from config import *  #pin configuration file

global date_time,API_KEY,API,poleID,streetlightID

now=datetime.datetime.now()

API_ENDPOINT = API + "light/log"
GPIO.setup(light, GPIO.OUT)  

current_light_on_off_state = ""

def getInfo():
    r = requests.get(url = API +"light/state/" + poleID + "/" + API_KEY)
    data = r.json()
    return data

def postLightLog(data):
    r = requests.post(url = API_ENDPOINT, data=data)
    data = r.json()

def on_or_off_light(input_data, state):
    global current_light_on_off_state
    if(state != current_light_on_off_state):
        current_light_on_off_state = state
        print("light state= ",state)
        if(state == True):
            GPIO.output(light, GPIO.HIGH) # on
        else:
            GPIO.output(light, GPIO.LOW)  #off      
        postLightLog(input_data)

try:
    while True:
        data = getInfo()
        if(data['status'] == 'success'):
            res = data['result']
            input_data = {
                "logTime":str(now) ,
                "remarks": "on or off status",
                "state": res["state"],
                "authKey": API_KEY
                }
            on_or_off_light(input_data, res["state"])
        else:
            print('No data from server')
        time.sleep(2)
except KeyboardInterrupt:
    pass

1 Answer 1

0

It is not very clear to me what your problems are and what you want to achieve, however:

  • cron is for scheduled tasks, if you want to launch some programs when you start you should setup a service Here you have an example on how to do it: https://www.suse.com/support/kb/doc/?id=000019672
  • For the Python GPIO LEDS on Raspberry you need superuser permissions. Make sure appropriate user/permissions are set

Cheers

1
  • 1
    Legit answer from Carles. It could be that the script is already running but the network services isn't up yet, hence using it as a service (set to start in the correct order), should avoid such possible problems. Maybe consider using "yourscript > logfile" for testing purposes.
    – s1mmel
    Aug 2, 2022 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .