-1

I have a python programme that works out primes that I have set up with mpi4py, for it to run on my raspberry pi cluster, and I can normally run the command from any directory but when I put it in rc.local it does not work. I can run the rc.local file normally from the command line and then it starts as it should.

I connect to my Raspberry pi via ssh, but I am not with it at the moment, otherwise I would connect a monitor to it, to check for error messages on boot up. I set up port forwarding on our home wifi, and I changed the default port for ssh to make it more secure.

command inside rc.local file:

mpiexec -n 16 -f /home/pi/machinefile python3 /home/pi/clusterprime.py &

code for working out primes:

from mpi4py import MPI

def innit():
        #setting gloabl variables:
        global comm
        global rank
        global size
        global name

        #setting up mpi4py:
        comm = MPI.COMM_WORLD
        rank = comm.rank
        size = comm.size
        name = comm.name

        #checking if thread is master or slave:
        if rank == 0:
                mastersetup()
        main()

def master(thread):
        global n
        global nLock
        global cLock
        global primes
        global c

        print("Started thread: {}".format(thread))
        t = time.time()
        while True:
                with nLock:
                        n+=2
                comm.send(n, dest=thread)
                num, p = comm.recv(source=thread)
                if p:
                        times.append(time.time()-t)
                        t = time.time()
                        print(num)
                        primes.append(num)
                        with cLock:
                                c+=1

def mastersetup():
        #setting global module variables:
        global time
        #master setup:
        import pickle
        import threading
        import time

        #setting global pickle variables:
        global prime
        global n
        global primes

        primes = pickle.load(open("/home/pi/primes/primes.p", "rb"))
        n = primes[len(primes)-1]

        #setting global threading variables:
        global nLock
        global cLock
        global c
        global times

        nLock = threading.Lock()
        cLock = threading.Lock()
        threads = []
        c = 0
        times = []
        t = 0
        for x in range(1,size):
                threads.append(threading.Thread(target=master, args=[x]))
        for x in threads:
                x.daemon = True
               x.start()

        while True:
                if c >= size*5:
                        t = 0
                        with cLock:
                                c = 0
                        pickle.dump(primes,open("/home/pi/primes/primes.p", "wb"))
                        for x in times:
                                t+=x
                        t = t/len(times)/size
                        print(t)
                        t = []

                while len(primes) >= 200:
                        primes.remove(primes[0])

def main():

        while True:
                n = comm.recv(source=0)
                p = isprime(n)
                comm.send((n, p), dest=0)

def isprime(num):
        for n in range(3, round(num/2),2):
                if num%n == 0:
                        return(False)

        return(True)

if __name__ == "__main__":
        innit()

Edit:

I tried running the programme through creating a python file that uses subroccess call function, with a time.sleep before, that sleeps for 30 seconds before I run the command. And I put this in the rc.local file. I rebooted my cluster. Then When I ssh into it I went into htop to see if the programme is running. I can see the programme running, and I know it is on the sleep, but then it stops and nothing happens. I put a “&” character after the python3 command in rc.local.

  • This question doesn't seem to have anything to do with Raspberry Pi. It's a generic programming or Linux question. – Brick Jun 10 at 13:40
  • 1
    Don’t put anything in rc.local! There are hundreds of questions on this site about running a python program on startup – Milliways Jun 10 at 22:18
  • 1
    Why must I not put it in rc.local, I’ve done it manny times before? I even did it when I only had one raspberry pi with the threading module to use all of the cores. – THEAWEGAMER Jun 11 at 5:19
0

I suppose you are using Raspbian. If so then the first step to avoid problems to start programs at boot up is to avoid using rc.local. It is a relict from deprecated distributions that have used the old style SysV init system. But nowadays almost all notable Linux distributions use systemd, including Raspbian/Debian.

For compatibility the systemd developer have made an emulation of rc.local with systemd but it has limitations, in particular it isn't guaranteed that it starts at last service and there is no order in starting other services. So it may be possible that your program starts before other needed services are available. Please look at Compatibility with SysV what can go wrong. We have seen many problems here on this site using rc.local. Following the recommendation of the developers from systemd you should avoid using it. Instead use a systemd Unit file to start your service. You will find many examples here on this site.

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.