Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'root': 0.05; 'startup': 0.05; 'init': 0.07; 'attributes': 0.09; 'bash': 0.09; 'derived': 0.09; 'exit': 0.09; 'i\xe2\x80\x99ve': 0.09; 'part,': 0.09; 'reinstall': 0.09; 'restart': 0.09; 'skip:/ 10': 0.09; 'todo:': 0.09; 'url:github': 0.09; 'yeah,': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'creates': 0.14; 'dig': 0.16; 'docs.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hard-code': 0.16; 'it;': 0.16; 'itself,': 0.16; 'program?': 0.16; 'reload': 0.16; 'subject:under': 0.16; 'travis': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'unlike': 0.19; 'written': 0.21; 'example': 0.22; 'reset': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'installation': 0.23; 'script.': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'installed': 0.27; 'idea': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'project:': 0.31; 'sep': 0.31; 'anyone': 0.31; 'file': 0.32; 'probably': 0.32; 'figure': 0.32; 'run': 0.32; 'running': 0.33; 'fri,': 0.33; 'skip:d 20': 0.34; "can't": 0.35; 'display': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'subject:service': 0.36; 'method': 0.36; 'subject:?': 0.36; 'should': 0.36; 'project': 0.37; 'starting': 0.37; 'files': 0.38; 'little': 0.38; 'does': 0.39; '12,': 0.39; 'launch': 0.39; 'sure': 0.39; 'changed': 0.39; 'system.': 0.39; 'commands': 0.60; 'is.': 0.60; 'establish': 0.61; 'took': 0.61; 'simply': 0.61; 'become': 0.64; 'provide': 0.64; 'benefit': 0.68; 'default': 0.69; 'user,': 0.69; 'fortunately': 0.84; 'pike': 0.84; 'start.': 0.84; 'to:none': 0.92; 'mount': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=GYeDEzed6lfyebj+oYdCLxKbN7cp4vjpeTb068ghYrc=; b=Qw9EFNf7dr4kWyRuTG2tBmKugEWCnmWu0EQFuB1CqUPVLgCYn00L4L0Qd7fqqrgKcq 0GFoma2P4jo7otbMxoENz9ka8s6GMMbVBXBrh67gaGZ3hQuf6SPPwoVSt4QW7XJzuDhB ssZIk3RvnI4pgn2lDqAux3lPF9dwS97zrswIQH84al2VsuEHLyiiWf++q5MNOwzJNn03 aLf3H0ZgwDU1SqzYR0CcIPugtmYLmu8KHRgSRHm9rtom3qLFVSD8CykV13g8/4I5jjed 89+dQzc6MeRVKUKiFF/MjRSAP5XiTfhBI8sHhOzbPhCIYsXv7pMO8zwW0X6b4EjhrGFV 3zEg== MIME-Version: 1.0 X-Received: by 10.50.30.72 with SMTP id q8mr12509697igh.14.1410480419567; Thu, 11 Sep 2014 17:06:59 -0700 (PDT) In-Reply-To: References: Date: Fri, 12 Sep 2014 10:06:59 +1000 Subject: Re: Example of python service running under systemd? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410480428 news.xs4all.nl 2840 [2001:888:2000:d::a6]:45150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77786 On Fri, Sep 12, 2014 at 3:48 AM, Travis Griggs wro= te: > I=E2=80=99ve been reading lots of systemd docs. And blogs. Etc. At this p= oint, I think I would benefit from learning by example=E2=80=A6 > > Does anyone have an example .service file that they use to launch a long = running service written as a python program? > > If there is any example of what you changed to your python program itself= , that to would be really instructional for me. Yeah, I did that for the Yosemite Project: https://github.com/Rosuav/Yosemite The main program is Python, but all the systemd code comes from a Pike script that creates a bash script that creates the systemd file. Yes, that's a little convoluted... Here's the relevant shell script part, in case you don't want to dig it out of auth.pike: #!/bin/bash [ "$1" =3D "install" ] && [ -d /etc/systemd/system ] && { echo "[Unit] Description=3DYosemite Project [Service] # The user, path, and X display are derived at installation time # from the attributes of the yos script. Reinstall to reset them. Environment=3DDISPLAY=3D$DISPLAY User=3D`stat -c %u $0` ExecStart=3D`readlink -e $0` # If the network isn't available yet, restart until it is. Restart=3Don-failure RestartSec=3D10 [Install] WantedBy=3Dmulti-user.target " >/etc/systemd/system/yos.service # Note that some of this will fail if systemd is installed # but isn't the default init system. In that case, well, you # can't use this method of autostarting. TODO: Provide some # other ways to autostart (eg for Upstart and/or sysvinit). systemctl --system daemon-reload systemctl enable yos.service echo Installed as yos.service. systemctl start yos.service exit } # ... blah blah various setup python Yosemite.py This has some complications that you probably don't need, like that the owner of the script should become the user that runs it (chances are you can hard-code this, or run it as root and have it drop privileges itself, or something), and it needs to restart on failure, as it has to establish an sshfs mount before starting the main program. But it's a start. Note the three commands just before the script exits. Unlike sysvinit and upstart, systemd needs to be told to reload service files, and then you need to enable the service before it'll run on startup (and I like to start it immediately, to see that it's working properly). Took me a few hang-ups to figure that part out. Fortunately systemd can notice and give a warning if you change files and don't daemon-reload it; I'm not sure why it can't simply reload automatically (AIUI Upstart uses inotify on /etc/init - no idea why systemd can't do the same). ChrisA