[systemd-devel] What is the best way to run a shell script through 'ExecStart'

2013-09-30 Thread Muhammad Shakeel

Greetings all,

I have been trying to convert a LSB initscript of a package into 
corresponding systemd service file. 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 2) seems appropriate, can anyone please share something which 
involves using of loop inside systemd service file?


Thanks,
Shakeel
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] What is the best way to run a shell script through 'ExecStart'

2013-09-30 Thread Tom Gundersen
Hi Muhammad,

On Mon, Sep 30, 2013 at 3:25 PM, Muhammad Shakeel
muhammad_shak...@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).

HTH,

Tom
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] What is the best way to run a shell script through 'ExecStart'

2013-09-30 Thread Reindl Harald

Am 30.09.2013 15:31, schrieb Tom Gundersen:
 On Mon, Sep 30, 2013 at 3:25 PM, Muhammad Shakeel
 muhammad_shak...@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(75);
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;
  }
 }
?



signature.asc
Description: OpenPGP digital signature
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel