LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Restart service or even reboot upon service stopping (https://www.linuxquestions.org/questions/linux-newbie-8/restart-service-or-even-reboot-upon-service-stopping-4175607272/)

NotionCommotion 06-04-2017 06:31 AM

Restart service or even reboot upon service stopping
 
The following service startups upon reboot. I don't have remote access to the machine, and wish to restart it even if something goes wrong and the service halts. How can I restart it so? What about rebooting the machine if it halts? Thanks
Code:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Logger Client Interface
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:    $local_fs $network $named $time $syslog
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Logger Client
### END INIT INFO

SCRIPT=/usr/local/loggerclient/client
RUNAS=loggerclient
PIDFILE=/var/run/loggerclient.pid
LOGFILE=/var/log/loggerclient.log
PROG="Loggerclient Interface"

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo "$PROG already running" >&2
    return 1
  fi
  echo "Starting $PROG…" >&2
  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
  echo "$PROG started" >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo "$PROG not running" >&2
    return 1
  fi
  echo "Stopping $PROG…" >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo "$PROG stopped" >&2
}

status() {
    if [ -f "$PIDFILE" ] && ps -p $(cat $PIDFILE) >/dev/null;
    #if [ -d /proc/$(<$PIDFILE) ];
    then
      echo "$PROG is running"
    else
      echo "$PROG is not running"
    fi
    RETVAL=$?
    return $RETVAL
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
esac


AwesomeMachine 06-04-2017 02:02 PM

init 6

NotionCommotion 06-04-2017 06:02 PM

Quote:

Originally Posted by AwesomeMachine (Post 5718960)
init 6

Thanks awsome, how do I capture the init 6 event?

michaelk 06-04-2017 06:30 PM

It depends on the distribution/version and init system.

With systemd you can use the restart option in the unit configuration file.

https://www.freedesktop.org/software...d.service.html

In upstart you can use the respawn in the service's configuration file

With SysV you can use /etc/inittab

Not exactly sure if using a LSB script in systemd

Rebooting the computer if the OS crashes is typically done using a watchdog timer not normally found on home desktop computers. You would need something like an IPMI card.

https://en.wikipedia.org/wiki/Intell...ment_Interface

NotionCommotion 06-04-2017 08:13 PM

Quote:

Originally Posted by michaelk (Post 5719016)
It depends on the distribution/version and init system.

With systemd you can use the restart option in the unit configuration file.

https://www.freedesktop.org/software...d.service.html

In upstart you can use the respawn in the service's configuration file

With SysV you can use /etc/inittab

Not exactly sure if using a LSB script in systemd

Rebooting the computer if the OS crashes is typically done using a watchdog timer not normally found on home desktop computers. You would need something like an IPMI card.

https://en.wikipedia.org/wiki/Intell...ment_Interface

Thanks MichaelK. The machine is a Raspberry Pi. systemd?

michaelk 06-04-2017 08:34 PM

Depends on distribution/version installed. If using raspbian jessie then it is running systemd.

While systemd supports LSB scripts I'm not sure at the moment what to configure to make it restart automatically. If your Pi is running a systemd distribution you might want to consider writing a systemd unit file.

The Pi does have a built in watchdog timer but have not tried using it. I found one link that claims the builtin timer was unreliable and an external one was added. Maybe others will chime in that have actually tried to use it.

http://www.raspberry-pi-geek.com/Arc...-more-reliable

https://www.element14.com/community/...e-raspberry-pi

scasey 06-05-2017 03:26 PM

D. J. Bernstein's daemontools will do exactly what you're asking. It starts a daemon on boot, and will restart it if it halts or fails. http://cr.yp.to/daemontools.html.

NotionCommotion 06-06-2017 08:39 AM

Quote:

Originally Posted by michaelk (Post 5719036)
The Pi does have a built in watchdog timer but have not tried using it. I found one link that claims the builtin timer was unreliable and an external one was added. Maybe others will chime in that have actually tried to use it.

Thanks for pointing this out. The link is based on a pi2, so maybe better now? I will trying it out and post my results.

The link described using an daemon to pat the internal watchdog. The daemon is independent of my service, so the service can choke but the machine will be fine. So, the watchdog protects against system failure and not just the process choking, right?

But then the link described using "the following code" to pat the external watchdog. If this code is in the application, then this implementation is really to prevent the service from not operating correctly.

Am I interpreting this correctly?

Thanks

michaelk 06-06-2017 09:23 AM

Something needs to reset the watchdog timer. If a separate process then the watchdog just protects against system failure. If the reset is accomplished by your process then it protects both. If using a daemon watchdog then you still need another watchdog to monitor your process.

NotionCommotion 06-06-2017 11:14 AM

Quote:

Originally Posted by michaelk (Post 5719654)
Something needs to reset the watchdog timer. If a separate process then the watchdog just protects against system failure. If the reset is accomplished by your process then it protects both. If using a daemon watchdog then you still need another watchdog to monitor your process.

Thanks. As I expected. First I thought why use two watchdogs when I can only use one. But after more thought, probably best to use two so my process doesn't need to be running to reset the hardware watchdog, and the second watchdog doesn't reboot but just restarts the process. I am assuming there is no way Linux can magically know if the process is in some bad state, and the only way it can know is for the processes to actively reset some timer. Agree?

michaelk 06-06-2017 11:56 AM

Not that I know.


All times are GMT -5. The time now is 07:25 AM.