From 4585b4f2feea07c28f3f8bd9ca6331fe78da674c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 11 Oct 2017 18:54:04 +0530 Subject: [PATCH] Added scripts for liota as service --- scripts/README.md | 29 ++++++++++++ scripts/autostartliota | 102 ++++++++++++++++++++++------------------- scripts/liotad.conf | 20 ++++++++ scripts/liotad.service | 9 ++++ 4 files changed, 112 insertions(+), 48 deletions(-) create mode 100644 scripts/README.md create mode 100644 scripts/liotad.conf create mode 100644 scripts/liotad.service diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 00000000..81809701 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,29 @@ +# Autostarting Liota Daemon +For starting liotad.py in background automatically at reboot perform the following steps: + - On Ubuntu 15.04 or above or any of [these](https://en.wikipedia.org/wiki/Systemd#Availability) operating systems that use systemd. You can also check using this command `[[ `systemctl` =~ -\.mount ]] && echo yes || echo no` in terminal. + - Place liotad.service from scripts folder in `/etc/systemd/system` folder. + - Make your script executable with: + `chmod u+x /path/to/spark/sbin/start-all.sh` + - Start it: + `sudo systemctl start myfirst` + - Enable it to run at boot: + `sudo systemctl enable myfirst` + - Stop it: + `sudo systemctl stop myfirst ` +- On Ubuntu 14.04 or [these](https://en.wikipedia.org/wiki/Upstart#Adoption) operating systems which use upstart. You can also check using `[[ `/sbin/init --version` =~ upstart ]] && echo yes || echo no` in terminal. + - Copy liotad.conf from scripts to `/etc/init/`. + - To start service `service liotad start`. + - To stop service `service liotad stop`. + - To check status `service liotad status`. +- On sys-v init systems like Ubuntu 12.04 or older you can use autostartliota script. You can check sys-v init using `[[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]] && echo yes` : + * Place autostartliota in `/etc/init.d` + * Execute : + ```bash + $ sudo update-rc.d autostartliota defaults + $ sudo invoke-rc.d autostartliota start + ``` + + * To stop the script and remove it from different runlevels, execute: + ```bash + $ sudo update-rc.d -f autostartliota remove + ``` diff --git a/scripts/autostartliota b/scripts/autostartliota index ad86f6fb..67c4612b 100755 --- a/scripts/autostartliota +++ b/scripts/autostartliota @@ -1,64 +1,70 @@ -#! /bin/sh -# -*- coding: utf-8 -*- -# ----------------------------------------------------------------------------# -# Copyright © 2015-2016 VMware, Inc. All Rights Reserved. # -# # -# Licensed under the BSD 2-Clause License (the “License”); you may not use # -# this file except in compliance with the License. # -# # -# The BSD 2-Clause License # -# # -# Redistribution and use in source and binary forms, with or without # -# modification, are permitted provided that the following conditions are met:# -# # -# - Redistributions of source code must retain the above copyright notice, # -# this list of conditions and the following disclaimer. # -# # -# - Redistributions in binary form must reproduce the above copyright # -# notice, this list of conditions and the following disclaimer in the # -# documentation and/or other materials provided with the distribution. # -# # -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"# -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # -# THE POSSIBILITY OF SUCH DAMAGE. # -# ----------------------------------------------------------------------------# +#!/bin/sh ### BEGIN INIT INFO -# Provides: Autostarts liota. -# Required-Start: $all -# Required-Stop: +# Provides: liotad +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 -# Default-Stop: -# Short-Description: Starts liotad.py in background at reboot. +# Default-Stop: 0 1 6 +# Description: Starts liota daemon ### END INIT INFO -PATH=/sbin:/usr/sbin:/bin:/usr/bin +DIR=/usr/lib/liota/packages/liotad +DAEMON=$DIR/liotad.py +DAEMON_NAME=liotad -do_start() { - sudo python /etc/liota/packages/liotad.py & +# This next line determines what user the script runs as. +# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python. +DAEMON_USER=root + +# The process ID of the script when it runs is stored here: +PIDFILE=/var/run/$DAEMON_NAME.pid + +#Get lsb functions +. /lib/lsb/init-functions + +check_init() { + # see also init_is_upstart in /lib/lsb/init-functions (which isn't available in Ubuntu 12.04, or we'd use it directly) + if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | grep -q upstart; then + log_failure_msg "$DAEMON_NAME is managed via upstart, try using service $DAEMON_NAME $1" + exit 1 + fi +} + +do_start () { + log_daemon_msg "Starting system $DAEMON_NAME daemon" + start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON + log_end_msg $? +} + +do_stop () { + log_daemon_msg "Stopping system $DAEMON_NAME daemon" + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE + log_end_msg $? } case "$1" in - start) - do_start + + start|stop) + check_init + do_${1} ;; + restart|reload|force-reload) - echo "Error: argument '$1' not supported" >&2 - exit 3 + check_init + do_stop + do_start ;; - stop) + + status) + check_init + status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? ;; + *) - echo "Usage: $0 start|stop" >&2 - exit 3 + echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}" + exit 1 ;; -esac +esac +exit 0 diff --git a/scripts/liotad.conf b/scripts/liotad.conf new file mode 100644 index 00000000..f503a7e5 --- /dev/null +++ b/scripts/liotad.conf @@ -0,0 +1,20 @@ +description "Liota daemon" +author "Prasha" + +start on runlevel [2345] +stop on shutdown + +script + + export HOME="/root" + exec /usr/bin/python /etc/liota/packages/liotad.py + exec echo Liota service ran at `date` >> /var/log/liota/liota.log + +end script + +post-start script + + # optionally put a script here that will notify liota has (re)started + echo "[`date`] Service liota running" >> /var/log/liota/liota.log + +end script diff --git a/scripts/liotad.service b/scripts/liotad.service new file mode 100644 index 00000000..4db4cd5e --- /dev/null +++ b/scripts/liotad.service @@ -0,0 +1,9 @@ +[Unit] +Description=Liota + +[Service] +Users=root +ExecStart=/etc/liota/packages/liotad.py + +[Install] +WantedBy=multi-user.target