systemd: Why is this service inactive (dead)?

12,861

Solution 1

It's dead because the script reaches its endpoint and terminates. Why would a single print statement blow up the terminal? A daemon usually runs in a loop that checks whether it's quitting time and terminates if so (or if it catches a signal from the kernel).

Solution 2

Your script is normally started as service, ends quite fast (as there is only one command and no while loop or similar in it re-executing it) and so the service has ended more quickly then you can ask for its status. Try to add some loop (with timeout to not spam your logs) to be able to see it active:

while True:
    print('hello')
    time.sleep(2)

and kill it through systemctl as soon as you have seen (with journalctl -u test.service) enough .

Solution 3

Your python script prints 'hello world', then python realizes it has reached the end of your script and terminates execution.

What do you want systemd to do ?

try a simple sh script like this:

#!/bin/sh touch /tmp/myfile$$.txt echo /tmp/myfile$$.txt tail -f /tmp/myfile$$.txt

You get the name of <file> in the service status.

Then, in your terminal, just pipe some text to it: echo "Hello World" >> <file> and inspect the service.

A service that loops and prints repeatably will fill up your terminal .... here, you read a file and are free to feed it some text when you want. Each service start creates a new file, $$ is the PID, next, as an exercise, delete the /tmp/myfile$$.txt file when you stop the service ;-)

Keyword for the exercise: trap is one way ;-)

Keep things simple, learn, then add to it. Note that python can also react to signals. I chose sh because it is easier ... for starters ...

Share:
12,861

Related videos on Youtube

JobHunter69
Author by

JobHunter69

Updated on September 18, 2022

Comments

  • JobHunter69
    JobHunter69 over 1 year

    I'm just trying to daemonize this simple python script:

    #!/usr/bin/python3
    print('hello')
    

    Here are my commands and the status:

    root@gw-0317STLG0067 /lib/systemd/system # systemctl enable test.service
    root@gw-0317STLG0067 /lib/systemd/system # systemctl daemon-reload
    root@gw-0317STLG0067 /lib/systemd/system # systemctl start test.service
    root@gw-0317STLG0067 /lib/systemd/system # systemctl status test.service
    * test.service - My Script Service
       Loaded: loaded (/usr/lib/systemd/system/test.service; enabled; vendor preset:
       Active: inactive (dead) since Sun 2016-07-31 10:06:29 UTC; 4s ago
      Process: 20620 ExecStart=/root/test.py (code=exited, status=0/SUCCESS)
     Main PID: 20620 (code=exited, status=0/SUCCESS)
    
    Jul 31 10:06:29 gw-0317STLG0067 systemd[1]: Started My Script Service.
    Jul 31 10:06:29 gw-0317STLG0067 test.py[20620]: hello
    

    And here my service script:

    [Unit]
    Description=My Script Service
    After=multi-user.target
    
    [Service]
    Type=simple
    ExecStart = /root/test.py
    
    [Install]
    WantedBy=multi-user.target
    

    Why is it dead, and also what exactly should happen when you daemonize a print statement? Should it blow up the terminal?

    • user996142
      user996142 almost 7 years
      Are you sure you should have Type=simple? Looks like Type=oneshot for me.
  • JobHunter69
    JobHunter69 almost 7 years
    I thought daemonizing the script made it run in the background constantly? So in that case the script should keep running and reiterating, blowing up the terminal right?
  • Pankaj Goyal
    Pankaj Goyal almost 7 years
    It will keep running in the background until it completes. No loop, nothing to be in the background.
  • thecarpy
    thecarpy almost 7 years
    @Goldname try a simple sh script like this: #!/bin/sh touch /tmp/myfile$$.txt echo /tmp/myfile$$.txt tail -f /tmp/myfile$$.txt Then just pipe some text to it: echo "Hello World" >> <file> You get <file> name in the service status.
  • JobHunter69
    JobHunter69 almost 7 years
    Ok, but when I do print() where does the output go?
  • JobHunter69
    JobHunter69 almost 7 years
    @thecarpy what is that for?
  • thecarpy
    thecarpy almost 7 years
    You want to experiment with systemd, have tail read a file (which is blocking), then put some text in that file. A service that prints something without loop will terminate. A service that loops and prints repeatably will fill up your terminal .... here, you read a file and are free to feed it some text when you want. Each service start creates a new file, $$ is the PID, next, as an exercise, delete the /tmp/myfile$$.txt file when you stop the service ;-)
  • JdeBP
    JdeBP almost 7 years
    You might want to more explicitly explain, given the question, that there's no terminal involved and that the output is going to a log.
  • Jaleks
    Jaleks almost 7 years
    thought the hint to not spam the logs and to use journalctl would be good enough for a starter :-)
  • Jaleks
    Jaleks almost 7 years
    the print output goes to the systemd log and can be viewed with journalctl, see my answer below
  • JdeBP
    JdeBP almost 7 years
    The questioner, in the question and in comments, has explicitly asked for an exact description rather than hints. Xe does not know, and wants to know.