Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to automatically restart a crashed init.d service
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Mon Jan 13, 2020 9:13 am    Post subject: how to automatically restart a crashed init.d service Reply with quote

Hi,

I'm wondering how others are doing this.

For some critical daemons, I use inittab with the respawn option.

I'd like to do something similar with my init scripts, and automatically restart them if their corresponding services crash (but not necessarily all of them).

What is the best approach?

method 1:
Use a custom cron job or inittab script to parse the output of:
Code:
rc-status -c

Restart the list of services.

The inconvenience of this method is that the restart does not necessarily start right after a crash (depends on the cron periodicity setting).
Also, I am not sure every single service crash case is reflected as "crashed" in rc-status -c. Maybe some "crashes" are sometimes reported as "stopped" services? Finally, will "-c" report a crashed service in any runlevel, including the "manual runlevel"?

method 2:
Use a custom cron job or inittab script to parse the output of:
Code:
rc-status -m -C | grep -v '\[  started  \]' | cut -f 2 -d ' '

Restart the list of services in the manual runlevel.

The inconvenience of this method is that I don't know how to tell rc-status to list only the services in the default runlevel. I might also auto-restart a service that was purposely stopped manually. As in method 1, the restart does not necessarily take place right after a crash.

method 3:
Use supervise-daemon.

The huge inconvenience of this method is that Gentoo wiki states that it is still unstable software, requires manually hacking the init.d files of the services you want to supervise, and make sure the service doesn't use its own PID file or forks.

Any ideas?

Despite its disadvantages, method 1 seems to be the easiest to implement and maintain for now.
The supervise-daemon looks interesting, but it's too tedious to set up and maintain.
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 4438
Location: Frankfurt, Germany

PostPosted: Mon Jan 13, 2020 12:47 pm    Post subject: Reply with quote

Switch to Systemd. Use one of the restart options.
Code:
[Service]
Restart=always
RestartSec=3
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3940
Location: Hamburg

PostPosted: Mon Jan 13, 2020 1:22 pm    Post subject: Reply with quote

Does https://wiki.gentoo.org/wiki/OpenRC#Automatic_respawning_crashed_services help?
Back to top
View user's profile Send private message
Jaglover
Watchman
Watchman


Joined: 29 May 2005
Posts: 8291
Location: Saint Amant, Acadiana

PostPosted: Mon Jan 13, 2020 1:31 pm    Post subject: Reply with quote

method 0:

Investigate why they crash and fix it.

I have one process which relies on an unreliable source and terminates sometimes as a result, I use daemontools to keep it running.
_________________
My Gentoo installation notes.
Please learn how to denote units correctly!
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Mon Jan 13, 2020 1:36 pm    Post subject: Reply with quote

toralf wrote:
Does https://wiki.gentoo.org/wiki/OpenRC#Automatic_respawning_crashed_services help?


Not quite. OpenRC does not seem to restart crashed services in "Dynamic Runlevel: manual". It will restart crashed services in "Runlevel: default".
Thanks anyway.

Moving to systemd is another option, but that would require a lot of work and testing.
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Mon Jan 13, 2020 1:40 pm    Post subject: Reply with quote

Jaglover wrote:
method 0:

Investigate why they crash and fix it.


Good one, and of course the preferred method. However, sometimes there's simply no time or resources to investigate. For instance, I had 1 crash of the tomcat service every 6 months, nothing worthwhile in the logs. That's the kind of service crash I don't mind simply restarting, but it has to be done right away.
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Mon Jan 13, 2020 1:55 pm    Post subject: Reply with quote

Vieri wrote:
toralf wrote:
Does https://wiki.gentoo.org/wiki/OpenRC#Automatic_respawning_crashed_services help?


Not quite. OpenRC does not seem to restart crashed services in "Dynamic Runlevel: manual". It will restart crashed services in "Runlevel: default".


Let me rephrase that.
Code:
openrc -n

will start a crashed service even if it's in the manual dynamic runlevel.
That's fine, but I need to run "openrc -n" if I want to avoid openrc stopping the services that are not in the default runlevel but in "Dynamic Runlevel: manual". That's a manual run of openrc -n.
I haven't found a config option in rc.conf equivalent to "-n". I guess that's why they came up with supervise-daemon.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1699
Location: South America

PostPosted: Tue Jan 14, 2020 1:41 am    Post subject: Re: how to automatically restart a crashed init.d service Reply with quote

The easiest alternatives, compared to changing the init system or installing additional software, are inittab respawn lines and supervise-daemon, indeed.

inittab respawn lines are the reliable alternative, at the cost of having almost no service management (services start all at the same time when a runlevel is entered, are killed when the runlevel is left, and pretty much that's it).

If you are using OpenRC, supervise-daemon comes for free, at the cost of having imperfect supervision (nobody supervises the supervisors).

Vieri wrote:
method 1:
Use a custom cron job or inittab script to parse the output of:
Code:
rc-status -c
[...]
The inconvenience of this method is that the restart does not necessarily start right after a crash (depends on the cron periodicity setting)
[...]
method 2:
Use a custom cron job or inittab script to parse the output of:
Code:
rc-status -m -C | grep -v '\[  started  \]' | cut -f 2 -d ' '
[...]
As in method 1, the restart does not necessarily take place right after a crash.
Exactly. That is polling. See "notification vs polling"

Vieri wrote:
method 3:
Use supervise-daemon.

The huge inconvenience of this method is that Gentoo wiki states that it is still unstable software,
New OpenRC features are usually labeled "experimental", and seemingly stay experimental forever. It's like the package never reaching version 1.0 :)

Vieri wrote:
requires manually hacking the init.d files of the services you want to supervise,
If the service script does not have a start() and a stop() function, i.e. it relies implicitly on start-stop-daemon, the supervisor=supervise-daemon assignment to turn on supervision for the service, and the assignment to command_args_foreground, if needed, can be added to the corresponding /etc/conf.d file, which is meant to be modified (or created) by the administrator. And if the supervisor=supervise-daemon assignment is present, command_background and command_args_background are ignored in the service script, pidfile is used as the PID file of the supervise-daemon process, and start_stop_daemon_args is interpreted as command line options for supservise-daemon instead (or ignored if supervise_daemon_args is set). So directly modifying the service script might not be needed. But if you have to, what would be the problem?

Vieri wrote:
and make sure the service doesn't use its own PID file or forks.
If this can be configured with command line arguments, command_args_foreground in the /etc/conf.d file can be used to do that.
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Tue Jan 14, 2020 7:19 am    Post subject: Re: how to automatically restart a crashed init.d service Reply with quote

GDH-gentoo wrote:
So directly modifying the service script might not be needed. But if you have to, what would be the problem?


Maintainability.
I'd have to keep track of file changes between upgrades. I grant you it seldom happens with init scripts, but I'd need to check for that anyway.

Thanks for all the feedback.
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2852

PostPosted: Tue Jan 14, 2020 7:57 am    Post subject: Re: how to automatically restart a crashed init.d service Reply with quote

Vieri wrote:
I'd have to keep track of file changes between upgrades. I grant you it seldom happens with init scripts, but I'd need to check for that anyway.
What I like to do in case like this is to use a user patch instead, so packages will install the pre-modified files (I notably have a few for openrc). Also nice to keep track of changes, if the patch breaks it likely mean I should check rather than use my old file as-is which may be incompatible.
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 901

PostPosted: Tue Jan 14, 2020 5:30 pm    Post subject: Re: how to automatically restart a crashed init.d service Reply with quote

Ionen wrote:
if the patch breaks it likely mean I should check


Thanks, Ionen.
I already do that for other parts of the system, so I was hoping not to have to do this for init scripts too...
Oh, well, openrc -n every couple of minutes isn't too bad if I don't get any major crashes during working hours.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1699
Location: South America

PostPosted: Tue Jan 14, 2020 11:50 pm    Post subject: Re: how to automatically restart a crashed init.d service Reply with quote

Vieri wrote:
Maintainability.
I'd still have a look at the service script of the daemon you want to restart. Depending on what it does, maybe all you need to have it supervised is:
/etc/conf.d/$insert-service-name-here
Code:
supervisor=supervise-daemon
command_args_foreground="some command-line options"
Back to top
View user's profile Send private message
apurkrt
Tux's lil' helper
Tux's lil' helper


Joined: 26 Feb 2011
Posts: 116
Location: Czechia, Europe

PostPosted: Sun Jan 19, 2020 8:39 pm    Post subject: Reply with quote

You might be interested in sys-process/daemontools

https://cr.yp.to/daemontools.html

https://cr.yp.to/daemontools/faq/create.html#why
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum