Results 1 to 7 of 7

Thread: Issue while launching python programm using systemd service

  1. #1

    Default Issue while launching python programm using systemd service

    Hello everyone;

    I'm trying to launch a python program using a systemd service and I'm already stuck.
    My systemd service (v4m_agent.service) is:
    Code:
    [Unit]
    Description=Service v4m_agent
    Wants=network.target
    After=network.target syslog.target
    
    [Service]
    Type=oneshot
    RemainAfterExit=no
    ExecStart=/usr/local/psa/v4m_agent/v4m_agentd.py &
    ExecStop=/usr/bin/kill -TERM $MAINPID
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    If I type systemd start v4m_agent the program is launched but it never returns the command line control to me, unless I type Ctrl + C (this don't stops the python program but it returns the command line control to me). After that, if I type systemd status v4m_agent, the status is always "activating" and never "active".

    How could I fix it? My python program is like a daemon to be executed "forever" in backgroud after launched it. And I want/need that its state will be "active" (for other managements reasons).
    Are there any issue in my systemd service?

    Thanks a lot!

  2. #2

    Default Re: Issue while launching python programm using systemd service

    Quote Originally Posted by dpatino82 View Post
    Are there any issue in my systemd service?
    Yes, from the first glance I'd say you should _not_ use '&' in your ExecStart line, in particular as you also set "RemainAfterExit=no".

    And as your "daemon" doesn't fork and never returns, you should probably set the "Type" to "simple" instead of "oneshot".

    See also "man systemd.service":
    If set to simple (the default value if neither Type= nor BusName= are specified), it is expected that the process configured with

    ExecStart= is the main process of the service.

    ...

    Behavior of oneshot is similar to simple; however, it is expected

    that the process has to exit before systemd starts follow-up units.

  3. #3

    Default Re: Issue while launching python programm using systemd service

    A lot of thanks, wolfi323.

    I've deleted '&' in my ExecStart line and changed "Type" to "simple" and now it seems to be working properly.


    Thank you very much!

  4. #4

    Default Re: Issue while launching python programm using systemd service

    OMG! Now, I've got a new issue.

    Currently when I stop the service (systemctl stop v4m_agent) it doesn't do all the tasks programmed to be done before finish, instead of that it finish hardly.
    My main program is waiting for a SIGTERM to start the finish tasks and then stop, SIGTERM is received but nothing else happend, the program died suddenly. No final tasks are executed.
    Is this due to changed "Type" to "simple"? I told this because before that change final tasks where executed.
    How could I fix it?

    Thanks!

  5. #5

    Default AW: Re: Issue while launching python programm using systemd service

    Quote Originally Posted by dpatino82 View Post
    Is this due to changed "Type" to "simple"?
    Might be. Probably systemd is killing it.

    I told this because before that change final tasks where executed.
    How could I fix it?
    Well, I suppose you could switch back to "oneshot", start it with '&' to make it fork again, but then set "RemainAfterExit=yes".
    This way systemd should treat it as active even if it returns immediately.

    Probably not the nicest way, but if it works for you...

  6. #6

    Default Re: Issue while launching python programm using systemd service

    Thanks wolfi323.

    I've made a 'mix' of your suggestions. I've kept "Type=simple" and set "RemainAfterExit=yes".
    Thus, typing start the service goes to active (running) state and typing stop it goes to inactive (dead) state finishing all my code before exit.
    Currently it seems OK, however, what would you think is the nicest way to configure it?

    Many thanks!

  7. #7

    Default Re: Issue while launching python programm using systemd service

    Quote Originally Posted by dpatino82 View Post
    I've made a 'mix' of your suggestions. I've kept "Type=simple" and set "RemainAfterExit=yes".
    Thus, typing start the service goes to active (running) state and typing stop it goes to inactive (dead) state finishing all my code before exit.
    Hm. I wouldn't have thought that RemainAfterExit would make a difference in that case...
    The man page says:
    Code:
           RemainAfterExit=           Takes a boolean value that specifies whether the service shall be
               considered active even when all its processes exited. Defaults to
               no.
    But in your current case, no process should exit anyway.

    Currently it seems OK, however, what would you think is the nicest way to configure it?
    Well, I cannot really answer that.
    But the whole "starting it with &, and stopping it with kill" sounds a bit hackish to me.

    The nicest way would of course be to write a proper daemon that forks itself and probably even interfaces with systemd (i.e. "that the daemon sends a notification message via sd_notify(3) or an equivalent call when it has finished starting up", and using Type=notify), I suppose...

    Again, if it works now like you expect it, I would leave it as it is.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •