Forked from emxsys/raspberry_pi_script_as_service.md
Created
December 24, 2020 16:21
-
-
Save ngtrthanh/45ee744b99a9dfd40d77751041d77e28 to your computer and use it in GitHub Desktop.
Revisions
-
emxsys revised this gist
May 1, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 for the Script Now we're going to define the service to run this script: -
emxsys revised this gist
May 1, 2019 . 1 changed file with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/_ 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. -
emxsys revised this gist
May 1, 2019 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ # How to Run a Script as a Service in Raspberry Pi - Raspbian Jessie **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 -
emxsys revised this gist
May 1, 2019 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: ```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: ```Shell sudo chmod 644 /lib/systemd/system/hello.service chmod +x /home/pi/hello_world.py sudo systemctl daemon-reload -
emxsys revised this gist
May 1, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: ```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: ```ShellSession sudo chmod 644 /lib/systemd/system/hello.service chmod +x /home/pi/hello_world.py sudo systemctl daemon-reload -
emxsys revised this gist
May 1, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: ```sh sudo chmod 644 /lib/systemd/system/hello.service chmod +x /home/pi/hello_world.py sudo systemctl daemon-reload -
emxsys revised this gist
May 1, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. ## 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/). ## 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 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` -
emxsys revised this gist
May 1, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
emxsys revised this gist
May 1, 2019 . 1 changed file with 17 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/). ## 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 `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 -
emxsys created this gist
May 1, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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