Restart service or even reboot upon service stopping
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.