LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2017, 06:31 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Rep: Reputation: Disabled
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
 
Old 06-04-2017, 02:02 PM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
init 6
 
Old 06-04-2017, 06:02 PM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
init 6
Thanks awsome, how do I capture the init 6 event?
 
Old 06-04-2017, 06:30 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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

Last edited by michaelk; 06-04-2017 at 06:43 PM.
 
Old 06-04-2017, 08:13 PM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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?
 
Old 06-04-2017, 08:34 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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

Last edited by michaelk; 06-04-2017 at 08:49 PM.
 
1 members found this post helpful.
Old 06-05-2017, 03:26 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
 
Old 06-06-2017, 08:39 AM   #8
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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
 
Old 06-06-2017, 09:23 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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.
 
Old 06-06-2017, 11:14 AM   #10
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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?
 
Old 06-06-2017, 11:56 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Not that I know.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
audit.rules does not retain certain settings after reboot or service restart johnmccarthy Linux - Security 6 01-13-2011 08:24 AM
Cron service and oracle service stopped unexpectedly. Can't restart oracle. camron Linux - Newbie 6 06-10-2010 06:00 PM
what is the difference between service network reload and service network restart ? markraem Linux - Networking 3 11-21-2007 03:08 PM
How do I set the regulatory daemon to restart when I restart the network service? zahadumy Linux - Networking 0 11-05-2006 11:24 AM
reboot works but service network restart does not spikeroo Linux - Networking 4 12-15-2005 04:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration