Re: [gentoo-user] Safe systemd "reload" command

2016-06-07 Thread Michael Orlitzky
On 06/07/2016 06:17 AM, Tom H wrote:
> 
> I've just looked at your script.
> 
> Do
>   systemctl try-restart spamassassin
> and
>   systemctl try-restart amavisd
> need "2>/dev/null" if you're booted with sysvinit+openrc but have
> systemd installed like the rc-service invocations in the opposite
> case? or do they fail silently?
> 

Beats me, I don't have any way to test it. I am planning to do another
revision bump at some point with ExecReload for spamd, so if anyone
tells me that I need 2>/dev/null on those lines, I'll fix it.




Re: [gentoo-user] Safe systemd "reload" command

2016-06-07 Thread Tom H
On Tue, Jun 7, 2016 at 12:59 AM, Michael Orlitzky  wrote:
> On 06/06/2016 06:04 PM, Tom H wrote:


>> 1) I've never used systemd on Gentoo but I assume that you can
>> co-install openrc and systemd. So you'd want to check whether systemd
>> is running:
>>
>> [ -d /run/systemd/system ]
>
> I think the way I did this, it will be a no-op if systemd is not running
> (or if e.g. spamd is not running *under* systemd). I committed the cron
> job yesterday, so I'll hear about it if it doesn't work.

I've just looked at your script.

Do
systemctl try-restart spamassassin
and
systemctl try-restart amavisd
need "2>/dev/null" if you're booted with sysvinit+openrc but have
systemd installed like the rc-service invocations in the opposite
case? or do they fail silently?


>> 2) spamassassin.service is running
>> 3) reload or restart spamassassin.service
>>
>> systemctl try-reload-or-restart spamassassin.service
>> if sa is running, it'll reload it if sa supports a reload, otherwise
>> it'll restart it
>
> Ah, that sounds like an improvement. It looks like amavisd.service
> supports reloading, but spamd.service doesn't. The way we do it in
> spamd.init is to send a HUP signal to the spamd process (determined from
> its PID file). Google tells me that
>
> ExecReload=/bin/kill -HUP $MAINPID
>
> should work...

It's the canonical way.



Re: [gentoo-user] Safe systemd "reload" command

2016-06-07 Thread Tom H
On Sun, Jun 5, 2016 at 6:27 PM, J.  wrote:
> El dom, 05-06-2016 a las 12:09 -0400, Michael Orlitzky escribió:
>>
>> Does that happen with rc-service, too? Or only with the init script
>> (which uses openrc-run)? I recently updated the job on the wiki to
>> use rc-service after we've checked that it exists.
>>
>> If it still prints the error, I'll just redirect stderr to /dev/null
>> during the check.
>
> It does, seems rc-service is just a wrapper to run stuff in
> /etc/init.d/

"/sbin/service" in Debian/Fedora/RHEL/Ubuntu/FreeBSD/NetBSD basically
runs "env -i set_some_env_vars /path/to/daemon command" so there's no
reason for OpenRC to do something different.



Re: [gentoo-user] Safe systemd "reload" command

2016-06-06 Thread Michael Orlitzky
On 06/06/2016 06:04 PM, Tom H wrote:
> 
> 1) I've never used systemd on Gentoo but I assume that you can
> co-install openrc and systemd. So you'd want to check whether systemd
> is running:
> 
> [ -d /run/systemd/system ]
> 

I think the way I did this, it will be a no-op if systemd is not running
(or if e.g. spamd is not running *under* systemd). I committed the cron
job yesterday, so I'll hear about it if it doesn't work.


> 2) spamassassin.service is running
> 3) reload or restart spamassassin.service
> 
> systemctl try-reload-or-restart spamassassin.service
> if sa is running, it'll reload it if sa supports a reload, otherwise
> it'll restart it
> 

Ah, that sounds like an improvement. It looks like amavisd.service
supports reloading, but spamd.service doesn't. The way we do it in
spamd.init is to send a HUP signal to the spamd process (determined from
its PID file). Google tells me that

  ExecReload=/bin/kill -HUP $MAINPID

should work...





Re: [gentoo-user] Safe systemd "reload" command

2016-06-06 Thread Tom H
On Sun, Jun 5, 2016 at 9:23 AM, J.  wrote:
>
> SYSTEMD_INIT_PID=`pgrep -o -U 0 systemd`

Doesn't systemd call "init" rather "systemd" if you use the "sysv-utils" flag?



Re: [gentoo-user] Safe systemd "reload" command

2016-06-06 Thread Tom H
On Sun, Jun 5, 2016 at 12:48 AM, Michael Orlitzky  wrote:
>
> I'm planning on adding USE=cron to mail-filter/spamassassin to perform
> nightly updates. I have a script that works for OpenRC,
>
> https://wiki.gentoo.org/wiki/SpamAssassin#Daily_updates
>
> but I've commented where I would like to have something similar for
> systemd users. Anybody know how to do that?
>
> We can't count on systemd being installed, so we need to...
>
> 1. Test that systemd is installed.
>
> 2. Check if e.g. spamd is running (depends on #1 for the commands).
>
> 3. Reload or restart the daemon if #1 and #2 hold; or do nothing if
> one of them doesn't.

1) I've never used systemd on Gentoo but I assume that you can
co-install openrc and systemd. So you'd want to check whether systemd
is running:

[ -d /run/systemd/system ]

2) spamassassin.service is running
3) reload or restart spamassassin.service

systemctl try-reload-or-restart spamassassin.service
if sa is running, it'll reload it if sa supports a reload, otherwise
it'll restart it

systemctl try-restart spamassassin.service
if sa is running, it'll restart it



Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread Michael Orlitzky
On 06/05/2016 12:27 PM, J. García wrote:
> It does, seems rc-service is just a wrapper to run stuff in
> /etc/init.d/, proof:
> 

Ok, thanks again. I'll redirect stderr when we check the status.





Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread J.
El dom, 05-06-2016 a las 12:09 -0400, Michael Orlitzky escribió:
> Oh ok, I didn't know about that. Yes it would be annoying to have
> that
> emailed to you every day. The first line of the script redirects
> stdout,
> 
>   # First, redirect stdout to /dev/null.
>   exec 1>/dev/null
> 
> but stderr is left intact.

I noticed that.

> Does that happen with rc-service, too? Or only with the init script
> (which uses openrc-run)? I recently updated the job on the wiki to
> use
> rc-service after we've checked that it exists.
> 
> If it still prints the error, I'll just redirect stderr to /dev/null
> during the check.
> 
> 
It does, seems rc-service is just a wrapper to run stuff in
/etc/init.d/, proof:

# rc-service spamd status
* You are attempting to run an openrc service on a
* system which openrc did not boot.
* You may be inside a chroot or you may have used
* another initialization system to boot this system.
* In this situation, you will get unpredictable results!
* If you really want to do this, issue the following command:
* touch /run/openrc/softlevel



Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread Michael Orlitzky
On 06/05/2016 11:37 AM, J. García wrote:
>>
> It works, and is harmless as you say, but since I'm not running OpenRC,
> it prints a warning to stderr about openrc not running, at least I find
> that a bit annoying,

Oh ok, I didn't know about that. Yes it would be annoying to have that
emailed to you every day. The first line of the script redirects stdout,

  # First, redirect stdout to /dev/null.
  exec 1>/dev/null

but stderr is left intact.


> The following message is print every time /etc/init.d/{spamd,amavisd}
> status is run, e.g.:
> 
>  # /etc/init.d/spamd status
> * You are attempting to run an openrc service on a
> * system which openrc did not boot.
> ...
> 

Does that happen with rc-service, too? Or only with the init script
(which uses openrc-run)? I recently updated the job on the wiki to use
rc-service after we've checked that it exists.

If it still prints the error, I'll just redirect stderr to /dev/null
during the check.




Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread J.
El dom, 05-06-2016 a las 09:37 -0600, J. García escribió:
>  but redirecting  any output from /etc/init.d/$SERVICE status
> to /dev/null.
Correcting, my previous message:
* but redirecting but redirecting  any output from /etc/init.d/$SERVICE
status> to /dev/null, might be OK too, as only the return code matters.



Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread J.
El dom, 05-06-2016 a las 09:37 -0400, Michael Orlitzky escribió:
> On 06/05/2016 03:23 AM, J. García wrote:
> > 
> > I run systemd , but I have not tested your script, as of now I'm
> > not
> > using spamassassin, but I will at some time in near future; but
> > looking
> > at the script, I see some problems, you run the OpenRC restart
> > commands
> > even if systemd is available, further it doesn't know which one is
> > running, as both can be installed(And are installed if using
> > systemd),
> That's what the "/etc/init.d/spamd status" command is meant to check.
> The "... status" will only return success if you are running spamd
> via
> openrc. Likewise, as I understand it (but haven't tested), the
> "systemctl try-restart spamassassin" command will only work if you
> are
> *using* systemd and the spamassassin service is running. They should
> be
> harmless otherwise.
> 
It works, and is harmless as you say, but since I'm not running OpenRC,
it prints a warning to stderr about openrc not running, at least I find
that a bit annoying, that's why I suggested to check which one is
running as PID 1. I don't know if systemctl prints anything in the
other case(OpenRC running, and systemd installed) but you can run it
with -q to be silent:
The following message is print every time /etc/init.d/{spamd,amavisd}
status is run, e.g.:

 # /etc/init.d/spamd status
* You are attempting to run an openrc service on a
* system which openrc did not boot.
* You may be inside a chroot or you may have used
* another initialization system to boot this system.
* In this situation, you will get unpredictable results!
* If you really want to do this, issue the following command:
* touch /run/openrc/softlevel 

IMHO, that is junk not needed in logs that might be generated from your
script, but redirecting  any output from /etc/init.d/$SERVICE status to
/dev/null.



Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread Rich Freeman
On Sun, Jun 5, 2016 at 9:37 AM, Michael Orlitzky  wrote:
>
> I did take your suggestion and check for rc-service though. I shouldn't
> count on openrc being in @system forever.
>

I suspect it would still work if openrc weren't installed:
bash -c "/etc/init.d/cups-browsed status || echo failed"
bash: /etc/init.d/cups-browsed: /sbin/runscript: bad interpreter: No
such file or directory
failed

You are correct in being careful about openrc being in @system.  Once
we fix everything using /etc/init.d/functions.sh that will probably
change.  (It will still be installed by default via a virtual, but
will get depcleaned if you switch to systemd and don't put it in
@world).

-- 
Rich



Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread Michael Orlitzky
On 06/05/2016 03:23 AM, J. García wrote:
> I run systemd , but I have not tested your script, as of now I'm not
> using spamassassin, but I will at some time in near future; but looking
> at the script, I see some problems, you run the OpenRC restart commands
> even if systemd is available, further it doesn't know which one is
> running, as both can be installed(And are installed if using systemd),

That's what the "/etc/init.d/spamd status" command is meant to check.
The "... status" will only return success if you are running spamd via
openrc. Likewise, as I understand it (but haven't tested), the
"systemctl try-restart spamassassin" command will only work if you are
*using* systemd and the spamassassin service is running. They should be
harmless otherwise.

For example...

  # spamd --daemonize
  # /etc/init.d/spamd status
   * status: stopped
  # echo $?
  3

If on the first line I had done "systemctl start spamassassin.service",
I should get the same result, so the openrc reload commands will be
skipped for systemd users.

I did take your suggestion and check for rc-service though. I shouldn't
count on openrc being in @system forever.




Re: [gentoo-user] Safe systemd "reload" command

2016-06-05 Thread J.
El sáb, 04-06-2016 a las 21:01 -0400, Michael Orlitzky escribió:
> On 06/04/2016 08:47 PM, Michael Orlitzky wrote:
> > 
> > 
> > Sounds good, do you want to try it and add it to the wiki? =)
> > 
> I just made an attempt and added it to the wiki:
> 
>   https://wiki.gentoo.org/wiki/SpamAssassin#Daily_updates
> 
> It doesn't have any affect on my openrc system (good), but I would
> still
> appreciate it if someone with systemd can tell me that it works.
> 
> 
I run systemd , but I have not tested your script, as of now I'm not
using spamassassin, but I will at some time in near future; but looking
at the script, I see some problems, you run the OpenRC restart commands
even if systemd is available, further it doesn't know which one is
running, as both can be installed(And are installed if using systemd),
so I would change it to be something like:

/etc/cron.daily/spamassassin-rule-updates:
#!/bin/bash
#
# Update SpamAssassin rules and reload daemons that use them.
#

# First, redirect stdout to /dev/null.
exec 1>/dev/null

# This thing sometimes spits out its progress onto stderr. If you
# don't have that problem (at least one user reports I'm crazy),
# then you'd be better off without the redirection here.
sa-update 2>/dev/null

# Exit code 0 means new updates were installed. Exit code 1 means 
# that we were already up-to-date. Anything else is failure.
if [ $? -eq 0 ]; then
sa-compile

# find out which init system is running, 
    # the one running  as PID 1, will be set to 1
OPENRC_INIT_PID=`pgrep -o -U 0 init`
SYSTEMD_INIT_PID=`pgrep -o -U 0 systemd`


# Do you run spamd or amavisd? Both daemons need to be reloaded
# in order to pick up the newly-updated rules. These "status"
# checks should succeed only when the daemon is running. They are
# OpenRC-specific, but sys-apps/openrc is part if @system so that
# should be fine.
if command -v rc-service &>/dev/null &&\
 [[ $OPENRC_INIT_PID -eq 1 ]] ; then 
rc-service spamd status && /etc/init.d/spamd reload
rc-service amavisd status && /etc/init.d/amavisd reload
fi

if command -v systemctl &>/dev/null &&\
 [[ $SYSTEMD_INIT_PID -eq 1 ]] ; then
# The systemctl (systemd) executable is installed,
# so try to use it to restart spamd and amavisd.
systemctl try-restart spamassassin
systemctl try-restart amavisd
fi
fi



Re: [gentoo-user] Safe systemd "reload" command

2016-06-04 Thread Michael Orlitzky
On 06/04/2016 08:47 PM, Michael Orlitzky wrote:
> 
> Sounds good, do you want to try it and add it to the wiki? =)
> 

I just made an attempt and added it to the wiki:

  https://wiki.gentoo.org/wiki/SpamAssassin#Daily_updates

It doesn't have any affect on my openrc system (good), but I would still
appreciate it if someone with systemd can tell me that it works.




Re: [gentoo-user] Safe systemd "reload" command

2016-06-04 Thread Michael Orlitzky
On 06/04/2016 08:19 PM, Rich Freeman wrote:
> 
> Couldn't you just check for the existence of systemctl, and if it
> exists call "systemctl try-restart spamd".  That will only work if
> systemd is running and spamd is running.  I'm sure you can accomplish
> the same via dbus as well.
> 

Sounds good, do you want to try it and add it to the wiki? =)

I don't need anything complicated, but I don't run systemd, so I can't
actually test anything to make sure it works. But as long as someone
else says it does and it doesn't hurt openrc users (I can test that),
I'll ship it.





Re: [gentoo-user] Safe systemd "reload" command

2016-06-04 Thread Rich Freeman
On Sat, Jun 4, 2016 at 6:48 PM, Michael Orlitzky  wrote:
> I'm planning on adding USE=cron to mail-filter/spamassassin to perform
> nightly updates. I have a script that works for OpenRC,
>
>   https://wiki.gentoo.org/wiki/SpamAssassin#Daily_updates
>
> but I've commented where I would like to have something similar for
> systemd users. Anybody know how to do that?
>
> We can't count on systemd being installed, so we need to...
>
>   1. Test that systemd is installed.
>
>   2. Check if e.g. spamd is running (depends on #1 for the commands).
>
>   3. Reload or restart the daemon if #1 and #2 hold; or do nothing if
>  one of them doesn't.
>

Couldn't you just check for the existence of systemctl, and if it
exists call "systemctl try-restart spamd".  That will only work if
systemd is running and spamd is running.  I'm sure you can accomplish
the same via dbus as well.


-- 
Rich



[gentoo-user] Safe systemd "reload" command

2016-06-04 Thread Michael Orlitzky
I'm planning on adding USE=cron to mail-filter/spamassassin to perform
nightly updates. I have a script that works for OpenRC,

  https://wiki.gentoo.org/wiki/SpamAssassin#Daily_updates

but I've commented where I would like to have something similar for
systemd users. Anybody know how to do that?

We can't count on systemd being installed, so we need to...

  1. Test that systemd is installed.

  2. Check if e.g. spamd is running (depends on #1 for the commands).

  3. Reload or restart the daemon if #1 and #2 hold; or do nothing if
 one of them doesn't.