Python systemd service stops soon after started










-1















I tried to run a Python script as a system service, but the service is not starting. Here is my configuration:



pyntp.service:



[Unit]
Description=Python NTP Service
After=multi-user.target

[Service]
Type=forking
ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py

[Install]
WantedBy=multi-user.target


ntpservice.py:



#!/usr/bin/python

import os
import time
import json

pid = os.fork()

if pid == 0:
print 'parent'
else:
print 'child'
while True:
print('123')
time.sleep(1)


The step to start the service is as follows:



cp pyntp.service /etc/systemd/system/
cp ntpservice.py /usr/local/bin/

systemctl daemon-reload
systemctl enable pyntp.service
systemctl start pyntp.service


The thing is, when I try to see the status of pyntp service, it is always like this:



● pyntp.service - Python NTP Service
Loaded: loaded (/usr/lib/systemd/system/pyntp.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2018-11-14 22:27:56 CST; 34min ago
Process: 801 ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py (code=exited, status=0/SUCCESS)
Main PID: 801 (code=exited, status=0/SUCCESS)

Nov 14 22:27:56 HES1 systemd[1]: Started Python NTP Service.
Nov 14 22:27:56 HES1 systemd[1]: Starting Python NTP Service...


Can any one help me resolve this? Thanks.










share|improve this question




























    -1















    I tried to run a Python script as a system service, but the service is not starting. Here is my configuration:



    pyntp.service:



    [Unit]
    Description=Python NTP Service
    After=multi-user.target

    [Service]
    Type=forking
    ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py

    [Install]
    WantedBy=multi-user.target


    ntpservice.py:



    #!/usr/bin/python

    import os
    import time
    import json

    pid = os.fork()

    if pid == 0:
    print 'parent'
    else:
    print 'child'
    while True:
    print('123')
    time.sleep(1)


    The step to start the service is as follows:



    cp pyntp.service /etc/systemd/system/
    cp ntpservice.py /usr/local/bin/

    systemctl daemon-reload
    systemctl enable pyntp.service
    systemctl start pyntp.service


    The thing is, when I try to see the status of pyntp service, it is always like this:



    ● pyntp.service - Python NTP Service
    Loaded: loaded (/usr/lib/systemd/system/pyntp.service; enabled; vendor preset: disabled)
    Active: inactive (dead) since Wed 2018-11-14 22:27:56 CST; 34min ago
    Process: 801 ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py (code=exited, status=0/SUCCESS)
    Main PID: 801 (code=exited, status=0/SUCCESS)

    Nov 14 22:27:56 HES1 systemd[1]: Started Python NTP Service.
    Nov 14 22:27:56 HES1 systemd[1]: Starting Python NTP Service...


    Can any one help me resolve this? Thanks.










    share|improve this question


























      -1












      -1








      -1








      I tried to run a Python script as a system service, but the service is not starting. Here is my configuration:



      pyntp.service:



      [Unit]
      Description=Python NTP Service
      After=multi-user.target

      [Service]
      Type=forking
      ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py

      [Install]
      WantedBy=multi-user.target


      ntpservice.py:



      #!/usr/bin/python

      import os
      import time
      import json

      pid = os.fork()

      if pid == 0:
      print 'parent'
      else:
      print 'child'
      while True:
      print('123')
      time.sleep(1)


      The step to start the service is as follows:



      cp pyntp.service /etc/systemd/system/
      cp ntpservice.py /usr/local/bin/

      systemctl daemon-reload
      systemctl enable pyntp.service
      systemctl start pyntp.service


      The thing is, when I try to see the status of pyntp service, it is always like this:



      ● pyntp.service - Python NTP Service
      Loaded: loaded (/usr/lib/systemd/system/pyntp.service; enabled; vendor preset: disabled)
      Active: inactive (dead) since Wed 2018-11-14 22:27:56 CST; 34min ago
      Process: 801 ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py (code=exited, status=0/SUCCESS)
      Main PID: 801 (code=exited, status=0/SUCCESS)

      Nov 14 22:27:56 HES1 systemd[1]: Started Python NTP Service.
      Nov 14 22:27:56 HES1 systemd[1]: Starting Python NTP Service...


      Can any one help me resolve this? Thanks.










      share|improve this question
















      I tried to run a Python script as a system service, but the service is not starting. Here is my configuration:



      pyntp.service:



      [Unit]
      Description=Python NTP Service
      After=multi-user.target

      [Service]
      Type=forking
      ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py

      [Install]
      WantedBy=multi-user.target


      ntpservice.py:



      #!/usr/bin/python

      import os
      import time
      import json

      pid = os.fork()

      if pid == 0:
      print 'parent'
      else:
      print 'child'
      while True:
      print('123')
      time.sleep(1)


      The step to start the service is as follows:



      cp pyntp.service /etc/systemd/system/
      cp ntpservice.py /usr/local/bin/

      systemctl daemon-reload
      systemctl enable pyntp.service
      systemctl start pyntp.service


      The thing is, when I try to see the status of pyntp service, it is always like this:



      ● pyntp.service - Python NTP Service
      Loaded: loaded (/usr/lib/systemd/system/pyntp.service; enabled; vendor preset: disabled)
      Active: inactive (dead) since Wed 2018-11-14 22:27:56 CST; 34min ago
      Process: 801 ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py (code=exited, status=0/SUCCESS)
      Main PID: 801 (code=exited, status=0/SUCCESS)

      Nov 14 22:27:56 HES1 systemd[1]: Started Python NTP Service.
      Nov 14 22:27:56 HES1 systemd[1]: Starting Python NTP Service...


      Can any one help me resolve this? Thanks.







      python linux systemd systemctl






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 15:31









      pushkin

      4,043112752




      4,043112752










      asked Nov 14 '18 at 15:03









      Tony LinTony Lin

      2851925




      2851925






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Your program is behaving as expected. Just forking isn't enough to make a daemon. What's happening is your code is running as long as its parent process runs, then (both forks are) exiting when the parent process terminates. What you want is to write a daemon (and have that controlled by systemd). You may find this question useful in explaining some easy ways to do that: How do you create a daemon in Python?



          fork is an important part of this process but just doing a fork by itself doesn't completely solve the problem. If you'd like to see a more detailed example of how to daemonize your process by hand using fork you can read this: Python code to Daemonize a process?






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303174%2fpython-systemd-service-stops-soon-after-started%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Your program is behaving as expected. Just forking isn't enough to make a daemon. What's happening is your code is running as long as its parent process runs, then (both forks are) exiting when the parent process terminates. What you want is to write a daemon (and have that controlled by systemd). You may find this question useful in explaining some easy ways to do that: How do you create a daemon in Python?



            fork is an important part of this process but just doing a fork by itself doesn't completely solve the problem. If you'd like to see a more detailed example of how to daemonize your process by hand using fork you can read this: Python code to Daemonize a process?






            share|improve this answer



























              1














              Your program is behaving as expected. Just forking isn't enough to make a daemon. What's happening is your code is running as long as its parent process runs, then (both forks are) exiting when the parent process terminates. What you want is to write a daemon (and have that controlled by systemd). You may find this question useful in explaining some easy ways to do that: How do you create a daemon in Python?



              fork is an important part of this process but just doing a fork by itself doesn't completely solve the problem. If you'd like to see a more detailed example of how to daemonize your process by hand using fork you can read this: Python code to Daemonize a process?






              share|improve this answer

























                1












                1








                1







                Your program is behaving as expected. Just forking isn't enough to make a daemon. What's happening is your code is running as long as its parent process runs, then (both forks are) exiting when the parent process terminates. What you want is to write a daemon (and have that controlled by systemd). You may find this question useful in explaining some easy ways to do that: How do you create a daemon in Python?



                fork is an important part of this process but just doing a fork by itself doesn't completely solve the problem. If you'd like to see a more detailed example of how to daemonize your process by hand using fork you can read this: Python code to Daemonize a process?






                share|improve this answer













                Your program is behaving as expected. Just forking isn't enough to make a daemon. What's happening is your code is running as long as its parent process runs, then (both forks are) exiting when the parent process terminates. What you want is to write a daemon (and have that controlled by systemd). You may find this question useful in explaining some easy ways to do that: How do you create a daemon in Python?



                fork is an important part of this process but just doing a fork by itself doesn't completely solve the problem. If you'd like to see a more detailed example of how to daemonize your process by hand using fork you can read this: Python code to Daemonize a process?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 15:13









                Rob BrichenoRob Bricheno

                2,375318




                2,375318





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303174%2fpython-systemd-service-stops-soon-after-started%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3