1 2 Previous Next 20 Replies Latest reply on Nov 21, 2015 8:51 AM by Frederick Blais

    Python script as background process

    bpryer

      Can anyone shed light on either of these questions?  This is more of a Linux question than it is python.

       

      1. What is the recommended method to launch a python script upon boot?  Since I am new to the Linux OS, my initial investigation led me to go the route of systemd init script utilizing start-stop-damean command to launch my python application in the background.  Can anyone shed some light on what approaches are available? 
      2. I would like to be able to ssh into the Edison and launch a python script and exit the Edison without stopping my program, but I have not been successful thus far.  Are there linux commands that I can utilize to force a script to run in the background?

       

      thanks,

        • 1. Re: Python script as background process
          David_J_Hunt

          1. If you're using ubilinux (Debian), you can add your startup command to /etc/rc.local

           

          2. Maybe use

          nohup <your command> &

          Then you can log out and your process will keep running. The output will go to nohup.out.

          1 of 1 people found this helpful
          • 2. Re: Python script as background process
            bpryer

            David,

             

            Thank you for the insight.  I have tried the nohup, but I ran into the following problems:

            • The when using print within python it creates a nohup.out file in the location of the script itself.
              • How can I turn this functionality off other than removing it from my script?  Can I change the location of the output file to the sd card?
            • When I log back into the Edison after logging out I don't see the python script running when using he "top" command to get all running processes.

             

            Any suggestions?

             

            Brent

            • 3. Re: Python script as background process
              intel_jassowski

              When you use nohup, you need to capture stdout and stderr, otherwise they will be directed to the nohup.out file in the local directory.

              If you don't want output, just redirect to /dev/null:

              $ nohup python mypython.py > /dev/null 2>&1 &

              If you'd like to capture output somewhere other than the local directory, change /dev/null to the file name where you want the output to go...

              • 4. Re: Python script as background process
                Frederick Blais

                Hello bpryer

                 

                I spent some time tonight trying to solve your problem.

                I made a python example "blink.py" which blink the on-board led on the Arduino breakout board

                 

                #!/usr/bin/python
                import mraa
                import time
                
                x = mraa.Gpio(13)
                x.dir(mraa.DIR_OUT)
                
                while True:
                    x.write(1)
                    time.sleep(0.5)
                    x.write(0)
                    time.sleep(0.5)
                
                
                

                 

                I thought at first than the method described here would work:

                http://serverfault.com/questions/34750/is-it-possible-to-detach-a-process-from-its-terminal-or-i-should-have-used-s

                However, "disown" does not exist on Yocto

                 

                I found a solution using "systemctl"

                some info here : Re: Startup scripts on Edison

                 

                What I did is create a blink service "/lib/systemd/system/blink.service" with this content :

                [Unit] Description=Blink! [Service] ExecStart=/home/root/blink.py [Install] WantedBy=multi-user.target

                 

                After that you have to reload your services with

                systemctl daemon-reload

                 

                and then you can start or stop your program with

                systemctl start blink

                systemctl stop blink

                 

                When it is started, it will continue to run even after you terminate you SSH session.

                 

                You can also make it auto-start at boot with

                systemctl enable blink


                To see if your process is running and/or enabled use

                systemctl status blink

                 

                I hope it helps!

                 

                 

                1 of 1 people found this helpful
                • 5. Re: Python script as background process
                  bpryer

                  speccy88,

                   

                  Thank you for the solution.  I have followed your instructions, but for some reason when I start the service, it fails with a status of 203/EXEC.  I'm not sure what might be going on.  I will try starting from scratch and see if I can solve this.  Below is my service file.

                   

                  [Unit]

                  Description=DATA_LOGGER

                   

                  [Service]

                  ExecStart=/code/TEST.py

                   

                  [Install]

                  WantedBy=multi-user.target

                   

                  _________________

                   

                  root@Edison:~# systemctl status custom_app

                  custom_app.service - DATA_LOGGER

                     Loaded: loaded (/lib/systemd/system/custom_app.service; disabled)

                     Active: failed (Result: exit-code) since Fri 2015-02-06 04:52:28 UTC; 4s ago

                    Process: 3660 ExecStart=/home/root/TEST.py (code=exited, status=203/EXEC)

                  Main PID: 3660 (code=exited, status=203/EXEC)

                   

                   

                  Feb 06 04:52:28 Edison systemd[1]: Started DATA_LOGGER.

                  Feb 06 04:52:28 Edison systemd[1]: custom_app.service: main process exited, code=exited, status=203/EXEC

                  Feb 06 04:52:28 Edison systemd[1]: Unit custom_app.service entered failed state.

                  1 of 1 people found this helpful
                  • 6. Re: Python script as background process
                    Frederick Blais

                    Did you do a chmod +x to your python file? Do you have the python shebang line as the first line of your script?

                    • 7. Re: Python script as background process
                      bpryer

                      I am not familiar with the chmod +x.  But I do have the shebang as the first line in every script.

                      • 8. Re: Python script as background process
                        Frederick Blais

                        What is the output of

                        ls -l /code

                        • 9. Re: Python script as background process
                          bpryer

                          root@Edison:/code/DataAcquisition# ls -l

                          -rw-r--r--    1 root     root          6523 Feb  6 05:01 MAX31855.py

                          -rw-r--r--    1 root     root          2864 Feb  6 05:01 application.py

                          -rw-r--r--    1 root     root           273 Feb  6 05:02 constants.py

                          -rw-r--r--    1 root     root          3835 Feb  6 05:02 settings.py

                          -rw-r--r--    1 root     root          3987 Feb  6 05:02 thermocouple.py

                          • 10. Re: Python script as background process
                            Frederick Blais

                            In fact, what I want you to do is

                            chmod +x TEST.py

                            And then check with ls -l if the file has the executable flag. If your script is not executable, systemctl wont work

                            1 of 1 people found this helpful
                            • 11. Re: Python script as background process
                              bpryer

                              Speccy88,

                               

                              Sorry for not getting back sooner.  I have managed to get this working with the example you posted.  But for some reasone I cannot get my py script to work without error.  I ensure every py file has the shebang as the first line of every script.  I ensure each file has executable flag, but nothing so far.  Is there anything specific that a script should not do while running as a service?

                              • 12. Re: Python script as background process
                                Frederick Blais

                                I'm far from my Edison right now so I cannot test anything, but I have an idea what your problem might be.

                                 

                                I don't think the problem is with the shebang line and executable flag. This is only important to start your first script. I don't know exactly how your Python program is structured, but I'm pretty sure there is some trouble finding your modules. What I would do is search in the "/lib/systemd/system/" directory for example service files that has configs that change the environment, like the current directory. If you Python script is not called from the right directory, it could have problem importing other modules.

                                1 of 1 people found this helpful
                                • 13. Re: Python script as background process
                                  bpryer

                                  I will check out the other service files and see if I can find any examples.  One thing I have noticed, the shebang line in my scripts are "#!/usr/bin/python2.7" as that is the path to my python on the edison.  Is this correct?  I noticed your shebang line is:

                                  1. #!/usr/bin/python 

                                  Could this be an issue?

                                   

                                  Brent

                                  • 14. Re: Python script as background process
                                    Frederick Blais

                                    no, take a look at /usr/bin to see if the two links are there

                                     

                                    example for changing working directory : [systemd-devel] WorkingDirectory in service file

                                    1 of 1 people found this helpful
                                    1 2 Previous Next