Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ngtrthanh/45ee744b99a9dfd40d77751041d77e28 to your computer and use it in GitHub Desktop.

Select an option

Save ngtrthanh/45ee744b99a9dfd40d77751041d77e28 to your computer and use it in GitHub Desktop.

Revisions

  1. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ except KeyboardInterrupt, e:

    You can execute it by python hello_world.py. If you get boring reading so many hello worlds, press Ctrl+C (or Cmd+C on OSX) to stop it. Save this file as hello_world.py in your home folder (home/pi/).

    ## Service to Run the Script
    ## Service for the Script

    Now we're going to define the service to run this script:

  2. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,6 @@

    _Original Article: http://www.diegoacuna.me/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.
  3. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    Original Article: http://www.diegoacuna.me/how-to-run-a-script-as-a-service-in-raspberry-pi-raspbian-jessie/
    # How to Run a Script as a Service in Raspberry Pi - Raspbian Jessie
    **By: Diego Acuña**

    By: Diego Acuña
    _Original Article: http://www.diegoacuna.me/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
  4. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ You can execute it by python hello_world.py. If you get boring reading so many h

    Now we're going to define the service to run this script:

    ```ShellSession
    ```Shell
    cd /lib/systemd/system/
    sudo nano hello.service
    ```
    @@ -60,7 +60,7 @@ Here we are creating a very simple service that runs our hello_world script and

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

    ```ShellSession
    ```Shell
    sudo chmod 644 /lib/systemd/system/hello.service
    chmod +x /home/pi/hello_world.py
    sudo systemctl daemon-reload
  5. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -35,14 +35,14 @@ You can execute it by python hello_world.py. If you get boring reading so many h

    Now we're going to define the service to run this script:

    ```bash
    ```ShellSession
    cd /lib/systemd/system/
    sudo nano hello.service
    ```

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

    ```
    ```text
    [Unit]
    Description=Hello World
    After=multi-user.target
    @@ -60,7 +60,7 @@ Here we are creating a very simple service that runs our hello_world script and

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

    ```sh
    ```ShellSession
    sudo chmod 644 /lib/systemd/system/hello.service
    chmod +x /home/pi/hello_world.py
    sudo systemctl daemon-reload
  6. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -60,7 +60,7 @@ Here we are creating a very simple service that runs our hello_world script and

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

    ```bash
    ```sh
    sudo chmod 644 /lib/systemd/system/hello.service
    chmod +x /home/pi/hello_world.py
    sudo systemctl daemon-reload
  7. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ A pretty common task when using this device, is to run some script (for example
    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.

    ## Create Python Script
    ## Example Python Script

    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 (hello_world.py):
    @@ -31,7 +31,7 @@ except KeyboardInterrupt, e:

    You can execute it by python hello_world.py. If you get boring reading so many hello worlds, press Ctrl+C (or Cmd+C on OSX) to stop it. Save this file as hello_world.py in your home folder (home/pi/).

    ## Create a Service to Run Script
    ## Service to Run the Script

    Now we're going to define the service to run this script:

    @@ -68,7 +68,7 @@ sudo systemctl enable hello.service
    sudo systemctl start hello.service
    ```

    ## Service Maintenance
    ## Service Tasks
    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:

    `sudo systemctl status hello.service`
  8. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    Original Article: http://www.diegoacuna.me/how-to-run-a-script-as-a-service-in-raspberry-pi-raspbian-jessie/

    By: Diego Acuña

    The Raspberry Pi is an incredible popular credit card size mini computer with awesome capabilities.
  9. @emxsys emxsys revised this gist May 1, 2019. 1 changed file with 17 additions and 9 deletions.
    26 changes: 17 additions & 9 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    Original Article: http://www.diegoacuna.me/how-to-run-a-script-as-a-service-in-raspberry-pi-raspbian-jessie/
    By: Diego Acuña

    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
    @@ -9,6 +10,8 @@ A pretty common task when using this device, is to run some script (for example
    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.

    ## Create Python Script

    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 (hello_world.py):

    @@ -25,7 +28,11 @@ except KeyboardInterrupt, e:
    logging.info("Stopping...")
    ```

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

    ## Create a Service to Run Script

    Now we're going to define the service to run this script:

    ```bash
    cd /lib/systemd/system/
    @@ -60,25 +67,26 @@ sudo systemctl enable hello.service
    sudo systemctl start hello.service
    ```

    ## Service Maintenance
    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:

    `sudo systemctl status hello.service`

    In general:

    # Check status
    ### Check status
    `sudo systemctl status hello.service`

    # Start service
    ### Start service
    `sudo systemctl start hello.service`

    # Stop service
    ### Stop service
    `sudo systemctl stop hello.service`

    # Check service's log
    ### Check service's log
    `sudo journalctl -f -u hello.service`

    REFERENCES
    https://wiki.archlinux.org/index.php/systemd
    https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
    https://coreos.com/os/docs/latest/getting-started-with-systemd.html
    ## REFERENCES
    - https://wiki.archlinux.org/index.php/systemd
    - https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
    - https://coreos.com/os/docs/latest/getting-started-with-systemd.html
  10. @emxsys emxsys created this gist May 1, 2019.
    84 changes: 84 additions & 0 deletions raspberry_pi_script_as_service.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    Original Article: http://www.diegoacuna.me/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 (hello_world.py):

    ```python
    #!/usr/bin/python

    from time import sleep

    try:
    while True:
    print "Hello World"
    sleep(60)
    except KeyboardInterrupt, e:
    logging.info("Stopping...")
    ```

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

    ```bash
    cd /lib/systemd/system/
    sudo nano hello.service
    ```

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

    ```
    [Unit]
    Description=Hello World
    After=multi-user.target
    [Service]
    Type=simple
    ExecStart=/usr/bin/python /home/pi/hello_world.py
    Restart=on-abort
    [Install]
    WantedBy=multi-user.target
    ```

    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: https://wiki.archlinux.org/index.php/systemd.

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

    ```bash
    sudo chmod 644 /lib/systemd/system/hello.service
    chmod +x /home/pi/hello_world.py
    sudo systemctl daemon-reload
    sudo systemctl enable hello.service
    sudo systemctl start hello.service
    ```

    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:

    `sudo systemctl status hello.service`

    In general:

    # Check status
    `sudo systemctl status hello.service`

    # Start service
    `sudo systemctl start hello.service`

    # Stop service
    `sudo systemctl stop hello.service`

    # Check service's log
    `sudo journalctl -f -u hello.service`

    REFERENCES
    https://wiki.archlinux.org/index.php/systemd
    https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
    https://coreos.com/os/docs/latest/getting-started-with-systemd.html