3

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?

1
  • 3
    Are you sure you should have Type=simple? Looks like Type=oneshot for me.
    – user996142
    Jul 7, 2017 at 17:19

3 Answers 3

5

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 .

3
  • 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.
    – JdeBP
    Jul 7, 2017 at 17:30
  • thought the hint to not spam the logs and to use journalctl would be good enough for a starter :-)
    – Jaleks
    Jul 7, 2017 at 17:37
  • 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.
    – JdeBP
    Jul 8, 2017 at 9:35
3

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).

7
  • 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? Jul 7, 2017 at 17:10
  • 1
    It will keep running in the background until it completes. No loop, nothing to be in the background.
    – DopeGhoti
    Jul 7, 2017 at 17:14
  • @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.
    – thecarpy
    Jul 7, 2017 at 17:15
  • Ok, but when I do print() where does the output go? Jul 7, 2017 at 17:17
  • @thecarpy what is that for? Jul 7, 2017 at 17:17
1

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 ...

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.