[systemd-devel] What is the best way to run a shell script through 'ExecStart'
Reindl Harald
h.reindl at thelounge.net
Mon Sep 30 06:44:27 PDT 2013
Am 30.09.2013 15:31, schrieb Tom Gundersen:
> On Mon, Sep 30, 2013 at 3:25 PM, Muhammad Shakeel
> <muhammad_shakeel at mentor.com> wrote:
>> I have been trying to convert a LSB initscript of a package into
>> corresponding systemd service fyile. Most init scripts are simple and
>> translating them into systemd unit files is non-trivial. In this case it is
>> a relatively long script involving some loops.
>>
>> 1) Should I write a script file separately and then call it through
>> 'ExecStart'?
>> or
>> 2) It would be good (and do-able) idea to fit whole script inside
>> ExecStart=/bin/sh -c " <script>"? (If yes, I would prefer it as it will free
>> me from hassle of maintaining two files)
>
> If you need something non-trivial (such as loops or a script over more
> than a couple of lines), then I suggest keeping it in a separate file.
> Systemd service files intentionally do not support loops (or other
> control structures)
i tend to use PHP for most things because re-use of internal libraries
and a simple endless loop with "type=simple" does it's job
#!/usr/bin/php
while(1 == 1)
{
}
_______________________________________________
cat /etc/systemd/system/monitor-dbmail-imapd.service
[Unit]
Description=monitor dbmail-imapd
After=dbmail-imapd.service
[Service]
Type=simple
ExecStart=/usr/local/bin/check-dbmail-service.php 143 dbmail-imapd
Restart=always
RestartSec=1
TimeoutSec=1
Nice=19
IOSchedulingClass=3
User=dbmail
Group=dbmail
PrivateTmp=true
NoNewPrivileges=yes
CapabilityBoundingSet=CAP_KILL
ReadOnlyDirectories=/etc
ReadOnlyDirectories=/usr
ReadOnlyDirectories=/proc
ReadOnlyDirectories=/sys
InaccessibleDirectories=/boot
InaccessibleDirectories=/home
InaccessibleDirectories=/var/lib/rpm
InaccessibleDirectories=/var/lib/yum
InaccessibleDirectories=/var/spool
[Install]
WantedBy=multi-user.target
_______________________________________________
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");
}
/** 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, 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: 263 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20130930/a01092e9/attachment.pgp>
More information about the systemd-devel
mailing list