[systemd-devel] OT: Which tool to use to restart service if check-script fails
Reindl Harald
h.reindl at thelounge.net
Fri May 27 07:40:55 UTC 2016
Am 27.05.2016 um 06:48 schrieb Thomas Güttler:
> OK, I understand that I need different tool to restart a service
> if a check-script fails.
>
> In my case the check script will be custom code. But the restart stuff
> is very general and reusable:
>
> Step1: Call check-script
> Step2: If exit 0 (meaning service is working), then wait N seconds, got
> to Step1
> Step3: Service is not working: Send process the signal TERM_SIGNAL
> (should be configurable)
> Step4: Wait N seconds until process terminated.
> Step5: If process did terminate, restart the service. Wait, go to Step1
> Step6: Process did not terminate: Wait N seconds, Send signal KILL
> Step7: Wait until OS has cleaned up the service
> Step8: Restart the service, Wait, got to Step1
>
> I could implement these steps myself, but I am lazy. I guess this
> has already been done several times before.
>
> Any tool recommendation?
that are just a few lines code in any programming language
your logic is way too complex - just SIGTERM and SIGKILL the service and
a proper configured systemd-unit with "Restart=always" will
automatically restarted when you shoot the main process in the head
also; if the service was *manually* stopped it will keep stopped because
the kill-calls won't do anything
_________________________
[root at testserver:~]$ cat /etc/systemd/system/monitor-dbmail-lmtpd.service
[Unit]
Description=monitor dbmail-lmtpd
After=dbmail-lmtpd.service
[Service]
Type=simple
ExecStart=/usr/local/bin/check-dbmail-service.php 24 dbmail-lmtpd
Restart=always
RestartSec=1
TimeoutSec=5
Nice=19
IOSchedulingClass=3
User=dbmail
Group=dbmail
PrivateTmp=yes
PrivateDevices=yes
NoNewPrivileges=yes
CapabilityBoundingSet=CAP_KILL
ReadOnlyDirectories=/etc
ReadOnlyDirectories=/usr
[Install]
WantedBy=multi-user.target
________________________________________
[root at testserver:~]$ cat /usr/local/bin/check-dbmail-service.php
#!/usr/bin/php
<?php
/** make sure we are running as shell-script */
if(PHP_SAPI != 'cli')
{
exit('FORBIDDEN');
}
/** verify that port and binary-name are given */
if(empty($_SERVER['argv'][1]) || empty($_SERVER['argv'][2]))
{
exit('USAGE: check-dbmail-service <port> <process-name>' . "\n");
}
/** delay monitoring for 30 seconds */
sleep(30);
/** service loop */
while(1 == 1)
{
if(!check_service())
{
sleep(5);
if(!check_service())
{
passthru('/usr/bin/killall -s SIGTERM ' .
escapeshellarg($_SERVER['argv'][2]));
usleep(750000);
passthru('/usr/bin/killall -s SIGKILL ' .
escapeshellarg($_SERVER['argv'][2]));
}
}
sleep(30);
}
/**
* check if service is available and responds
*
* @access public
* @return boolean
*/
function check_service()
{
$errno = 0;
$errstr = '';
$fp = @fsockopen('tcp://127.0.0.1', $_SERVER['argv'][1], $errno,
$errstr, /**$timeout*/5);
if($fp)
{
$response = @fgets($fp, 128);
@fclose($fp);
if(!empty($response))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
?>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20160527/3df82e59/attachment.sig>
More information about the systemd-devel
mailing list