8 Replies Latest reply on Apr 10, 2016 9:11 AM by joe-iot

    How to run Python file on startup on Intel Galileo

    BrechtW

      How do I run a python file on startup on the Intel Galileo.

        • 1. Re: How to run Python file on startup on Intel Galileo
          Intel_Peter

          Hello BrechtW,

           

          On the eglibc image for Galileo you can do it with a system service, I'll give you a brief example on how to do it. You will need three files, your script (test.py), a shell script (test.sh) and your service (test.service).

           

          test.py will be the following simple script:

           

          import time
          x = 0
          f = open('/home/root/test.txt','w')
          while x < 10:
                  print x
                  x += 1
                  f.write(str(x)+"\n")
                  time.sleep(1)
          
          

           

          test.sh will simply start test.py (I had some issues trying to make the service and I was able to solve them this way):

           

          #!/bin/sh
          /usr/bin/python /home/root/test.py
          
          

           

          test.service will look like this:

           

          #!/bin/sh
          [Unit]
          Description=test service
          [Service]
          ExecStart=/home/root/test.sh
          Type=idle
          [Install]
          WantedBy=basic.target
          
          

           

          Give test.sh executable right with chmod +x test.sh and store test.service in /lib/systemd/system, test.py and test.sh can be store anywhere but on my example they are stored in /home/root. Once the service file is store in the appropriate directory you can enable the service with systemctl enable test.service. Your script should now start on boot.

           

          Peter.

          1 of 1 people found this helpful
          • 2. Re: How to run Python file on startup on Intel Galileo
            FGT

            I've followed the suggestion at Re: Startup Scripts? of using the iotkit-agent.service to set a specific IP (I know there is a way of doing it with connman, but I wanted to use ifconfig...) so:

             

            # cd /home/root/

            root@galileo:~# cat fixedip

            #!/bin/sh

            /sbin/ifconfig enp0s20f6:avahi <this.is.my.ip> netmask 255.255.0.0 up

             

            # cd /lib/systemd/system

            root@galileo:/lib/systemd/system# cat fixedip.service

            [Unit]

            Description=fixedip

            After=network.target

             

            [Service]

            ExecStart=/home/root/fixedip

            Type=idle

             

            [Install]

            WantedBy=multi-user.target

             

            root@galileo:/lib/systemd/system# systemctl enable fixedip.service

            Created symlink from /etc/systemd/system/multi-user.target.wants/fixedip.service to

            /lib/systemd/system/fixedip.service.

             

            And that's it, the IP is set at startup.

             

            HTH

             

            Fernando.

            • 3. Re: How to run Python file on startup on Intel Galileo
              BrechtW

              @Intel_Peter How do I set the priority and in which file?

               

              My guess is putting this in test.sh.

              nice -n -20 python /my_folder/my_program.py &
              

               

              Is this correct?

              • 4. Re: How to run Python file on startup on Intel Galileo
                Intel_Peter

                What do you mean by priority? On boot? Or would you like to change the process niceness once the OS has finished booting?

                 

                Peter.

                • 5. Re: How to run Python file on startup on Intel Galileo
                  BrechtW

                  My program that runs my robot should have the highest priority. I think this is the niceness.

                  • 6. Re: How to run Python file on startup on Intel Galileo
                    Intel_Peter

                    Indeed you are correct, a process niceness determines its priority. If you wish to change the process' niceness on boot you can do it by changing the test.sh script (which I posted above). The script should now look like this:

                     

                    #!/bin/sh
                    nice -n -20 /usr/bin/python /home/root/test.py
                    

                     

                    That should change your script niceness on boot.

                     

                    Peter.

                    • 7. Re: How to run Python file on startup on Intel Galileo
                      BrechtW

                      @Intel_Peter I tried your answer but without succes:

                      startup.PNG

                       

                      galileo login: root
                      Password:
                      root@galileo:~# cd /home/root/
                      root@galileo:~# ls
                      index.html  miniconda3  test.py  test.sh
                      root@galileo:~# cat test.py
                      import time
                      x = 0
                      f = open('/home/root/test.txt','w')
                      while x < 10:
                              print x
                              x += 1
                      f.write(str(x)+"\n")
                              time.sleep(1)
                      root@galileo:~# cat test.sh
                      #!/bin/sh
                      /usr/bin/python /home/root/test.py
                      root@galileo:~# cat /lib/systemd/system/test.service
                      #! /bin/sh
                      [Unit]
                      Description=test service
                      [Service]
                      ExecStart=/home/root/test.sh
                      Type=idle
                      [Install]
                      WantedBy=basic.target
                      root@galileo:~#
                      
                      
                      
                      
                      

                       

                      I made it executable and enabled the service.

                       

                      Edit: It didn't work because one indent wasn't right.

                       

                      import time   
                      x = 0   
                      f = open('/home/root/test.txt','w')   
                      while x < 10:   
                          print x   
                          x += 1   
                          f.write(str(x)+"\n")   
                          time.sleep(1)
                      
                      
                      1 of 1 people found this helpful
                      • 8. Re: How to run Python file on startup on Intel Galileo
                        joe-iot

                        I have not had to call a shell script to launch a python process on startup. I am able to do it all in python, which seems like a cleaner solution. So here is what I do.

                         

                        Lets say you have a script "door.py" that monitors a switch. You want it to start on boot up. The file is in /root.

                         

                        Create a systemd unit file in /lib/systemd/system called doormon.service.

                        
                        [Unit]
                        Description=Door Monitor Alet App
                        
                        [Service]
                        PIDFile=/var/run/door.pid
                        WorkingDirectory=/home/root
                        ExecStart=/usr/bin/python door.py --pid
                        ExecReload=/bin/kill -s HUP $MAINPID
                        ExecStop=/bin/kill -s TERM $MAINPID
                        
                        [Install]
                        WantedBy=basic.target
                        
                        

                         

                        Now notice I am creating a pid file, this lets systemctl stop and reload work.

                         

                        In your python script use os.getpid() to create a pid file.

                        
                        fh=open("/var/run/door.pid", "w")
                        fh.write(str(os.getpid()))
                        fh.close()
                        
                        

                         

                        Then use systemctl enable doormon.service

                        And start your process with systemctl start doormon

                         

                        Some examples can be seen here:

                        http://www.instructables.com/id/Intel-Galileo-Garage-Monitor/

                        and

                        https://github.com/joemcmanus/galileoGarageMon

                         

                        -Joe

                        1 of 1 people found this helpful