How to run a script as a service in Raspberry Pi - Raspbian Jessie

The Raspberry Pi is an incredible popular credit card size mini computer with awesome capabilities.  Despite of is mini size it behaves pretty much like a regular computer (it has an OS with a graphical interface, an ethernet port so you can surf the web, an HDMI output, etc.) but also it exposes several inputs/outputs (GPIO) to interact with the real world (sensors and cool tech gadgets).

A pretty common task when using this device, is to run some script (for example a python script) as a service in the operating system so it can start on boot, stop and restart using systemctl and more. In this post I'm going to explain how to set a little script as a service using Raspbian Jessie in a Raspberry Pi.

First of all, we are going to write a small python script which print "Hello World" every 60 seconds. This is going to be our service script (

You can execute it by python If you get boring reading so many hello worlds, press Ctrl+C (or Cmd+C on OSX) to stop it. Save this file as in your home folder (home/pi/). Now we're going to define the service to run this script:

The service definition must be on the /lib/systemd/system folder. Our service is going to be called "hello.service":

Here we are creating a very simple service that runs our hello_world script and if by any means is aborted is going to be restarted automatically. You can check more on service's options in the next wiki:

Now that we have our service we need to activate it:

For every change that we do on the /lib/systemd/system folder we need to execute a daemon-reload (third line of previous code). If we want to check the status of our service, you can execute:

In general:


Diego Acuña

Diego Acuña

I'm a Software Engineering and Web Developer from Chile. My main interests are web technologies, software development methodologies and data mining (mainly on financial area). If you want to get in touch with me, use the contact page from the top menu.

  • Klayton Curran

    This needs to be more popular! I'm sure most of the kiddies that want their pi to run a program automatically are just setting it as a login command and are SOL if it happens to crash. I knew this could be done but it was not easy finding this article! Well written and the steps worked perfectly for me! Thank you.

  • igauravsehrawat

    The CoreOS link is outdated. This may be the one:

    • Diego Acuña

      Thanks, link updated!

  • Cyrus Nikko Pante

    Will the .py run as sudo too? And does it rerun if it crashes?

  • Brian Smith

    Quick comment: you say "press Ctrl+C (or Cmd+C on OSX) to stop it"

    Assuming you're in a unix terminal session (and where else would you type commands?) Control-C is Control-C, the OS doesn't matter. Command-C is "Cut".

    Perhaps you're thinking of Windows, where "Cut" is Control-C, which causes some problems.

    • Jake Vande Walle

      Actually, just so others don't get confused, on windows cut is CTRL-X and copy is CTRL-C

      • Brian Smith

        Oops, I should have been saying "Copy" for both MacOS and Windows. Command-C is Copy on MacOS just like Control-C is Copy on Windows, and Command-X/Control-X is Cut on each, respectively.

        But inside a terminal of some sort, control-C will still be control-C, and will be the default interrupt character.

  • Abdi Pranoto

    Is this service run on boot? if its not, how to make it run at startup? thx

    • Diego Acuña

      actually this runs on boot

  • Daniel Stonek

    sudo service hello start/stop/status