supervision - discussion about system services, daemon supervision, init, runlevel management, and tools such as s6 and runit
 help / color / mirror / Atom feed
* Help with chpst -e
@ 2008-04-13 23:19 Robin Bowes
  2008-04-13 23:52 ` Robin Bowes
  0 siblings, 1 reply; 19+ messages in thread
From: Robin Bowes @ 2008-04-13 23:19 UTC (permalink / raw)
  To: supervision

Hi,

Is it possible to set vars using "chpst -e" and then use those vars 
later in the same command line?

e.g.

mkdir env
echo dnslog > env/LOGUSER
echo /var/log/dnscache > env/LOGDIR

Then in run:
#!/bin/sh
exec chpst -e ./env -u $LOGUSER svlogd -tt $LOGDIR

I realise that this specific example won't work - $LOGUSER and $LOGDIR 
will both retain the values they had when the script was launched.

Is what I describe above possible?

Thanks,

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-13 23:19 Help with chpst -e Robin Bowes
@ 2008-04-13 23:52 ` Robin Bowes
  2008-04-14  8:34   ` Andras Korn
  0 siblings, 1 reply; 19+ messages in thread
From: Robin Bowes @ 2008-04-13 23:52 UTC (permalink / raw)
  To: supervision

Robin Bowes wrote:
> Hi,
> 
> Is it possible to set vars using "chpst -e" and then use those vars 
> later in the same command line?
> 
> e.g.
> 
> mkdir env
> echo dnslog > env/LOGUSER
> echo /var/log/dnscache > env/LOGDIR
> 
> Then in run:
> #!/bin/sh
> exec chpst -e ./env -u $LOGUSER svlogd -tt $LOGDIR
> 
> I realise that this specific example won't work - $LOGUSER and $LOGDIR 
> will both retain the values they had when the script was launched.
> 
> Is what I describe above possible?

Answer: yes it is.

This works:

#!/bin/sh
exec \
   chpst -e ./env  sh -c '
     chpst -u ${LOGUSER} \
     svlogd -tt ${LOGDIR}
'

LOGUSER and LOGDIR are set in ./env

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-13 23:52 ` Robin Bowes
@ 2008-04-14  8:34   ` Andras Korn
  2008-04-14  8:40     ` Andras Korn
  2008-04-14 13:38     ` Robin Bowes
  0 siblings, 2 replies; 19+ messages in thread
From: Andras Korn @ 2008-04-14  8:34 UTC (permalink / raw)
  To: supervision

On Mon, Apr 14, 2008 at 12:52:25AM +0100, Robin Bowes wrote:

> Robin Bowes wrote:
>> Hi,
>> Is it possible to set vars using "chpst -e" and then use those vars later 
>> in the same command line?
>
> Answer: yes it is.
>
> This works:
>
> #!/bin/sh
> exec \
>   chpst -e ./env  sh -c '
>     chpst -u ${LOGUSER} \
>     svlogd -tt ${LOGDIR}
> '
>
> LOGUSER and LOGDIR are set in ./env

There is also another way, if you don't like the sh -c part:

#!/bin/sh
if [ "$1" = "have-env" ]; then
	exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
else
	exec chpst -e ./env "$0" have-env
fi

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
               Thank you for holding your breath while I smoke.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14  8:34   ` Andras Korn
@ 2008-04-14  8:40     ` Andras Korn
  2008-04-14 13:38     ` Robin Bowes
  1 sibling, 0 replies; 19+ messages in thread
From: Andras Korn @ 2008-04-14  8:40 UTC (permalink / raw)
  To: supervision

On Mon, Apr 14, 2008 at 10:34:29AM +0200, Andras Korn wrote:

> #!/bin/sh
> if [ "$1" = "have-env" ]; then
> 	exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
> else
> 	exec chpst -e ./env "$0" have-env
> fi

The \ is unnecessary in line 3, sorry.

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
                         Choose: (E)-mail or (F)emale?


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14  8:34   ` Andras Korn
  2008-04-14  8:40     ` Andras Korn
@ 2008-04-14 13:38     ` Robin Bowes
  2008-04-14 13:48       ` Andras Korn
  2008-04-14 14:16       ` Charlie Brady
  1 sibling, 2 replies; 19+ messages in thread
From: Robin Bowes @ 2008-04-14 13:38 UTC (permalink / raw)
  To: supervision

Andras Korn wrote:
> On Mon, Apr 14, 2008 at 12:52:25AM +0100, Robin Bowes wrote:
> 
>> Robin Bowes wrote:
>>> Hi,
>>> Is it possible to set vars using "chpst -e" and then use those vars later 
>>> in the same command line?
>> Answer: yes it is.
>>
>> This works:
>>
>> #!/bin/sh
>> exec \
>>   chpst -e ./env  sh -c '
>>     chpst -u ${LOGUSER} \
>>     svlogd -tt ${LOGDIR}
>> '
>>
>> LOGUSER and LOGDIR are set in ./env
> 
> There is also another way, if you don't like the sh -c part:
> 
> #!/bin/sh
> if [ "$1" = "have-env" ]; then
> 	exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
> else
> 	exec chpst -e ./env "$0" have-env
> fi

Isn't that effectively doing the same thing, but without implicitly 
invoking "sh -c" ?

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 13:38     ` Robin Bowes
@ 2008-04-14 13:48       ` Andras Korn
  2008-04-14 14:16       ` Charlie Brady
  1 sibling, 0 replies; 19+ messages in thread
From: Andras Korn @ 2008-04-14 13:48 UTC (permalink / raw)
  To: supervision

On Mon, Apr 14, 2008 at 02:38:11PM +0100, Robin Bowes wrote:

>>> LOGUSER and LOGDIR are set in ./env
>> There is also another way, if you don't like the sh -c part:
>> #!/bin/sh
>> if [ "$1" = "have-env" ]; then
>> 	exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
>> else
>> 	exec chpst -e ./env "$0" have-env
>> fi
>
> Isn't that effectively doing the same thing, but without implicitly 
> invoking "sh -c" ?

Yes, but it helps avoid quoting issues and keeps the script tidier (at least
in my view).

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
     Dying in itself isn't so bad, but you feel bloody awful the next day.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 13:38     ` Robin Bowes
  2008-04-14 13:48       ` Andras Korn
@ 2008-04-14 14:16       ` Charlie Brady
  2008-04-14 14:31         ` Robin Bowes
  1 sibling, 1 reply; 19+ messages in thread
From: Charlie Brady @ 2008-04-14 14:16 UTC (permalink / raw)
  To: Robin Bowes; +Cc: supervision


On Mon, 14 Apr 2008, Robin Bowes wrote:

>>  There is also another way, if you don't like the sh -c part:
>>
>>  #!/bin/sh
>>  if [ "$1" = "have-env" ]; then
>>  	exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
>>  else
>>  	exec chpst -e ./env "$0" have-env
>>  fi
>
> Isn't that effectively doing the same thing, but without implicitly invoking 
> "sh -c" ?

s/implicitly/explicitly/

Yes, that was my thought too. You run /bin/sh twice either way.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 14:16       ` Charlie Brady
@ 2008-04-14 14:31         ` Robin Bowes
  2008-04-14 16:41           ` Andras Korn
  0 siblings, 1 reply; 19+ messages in thread
From: Robin Bowes @ 2008-04-14 14:31 UTC (permalink / raw)
  To: supervision

Charlie Brady wrote:
> 
> On Mon, 14 Apr 2008, Robin Bowes wrote:
> 
>>>  There is also another way, if you don't like the sh -c part:
>>>
>>>  #!/bin/sh
>>>  if [ "$1" = "have-env" ]; then
>>>      exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR" \
>>>  else
>>>      exec chpst -e ./env "$0" have-env
>>>  fi
>>
>> Isn't that effectively doing the same thing, but without implicitly 
>> invoking "sh -c" ?
> 
> s/implicitly/explicitly/
> 
> Yes, that was my thought too. You run /bin/sh twice either way.

And, it would perhaps be less cluttered to do:

#!/bin/sh

if [ "$1" != "have-env" ]; then
   exec chpst -e ./env "$0" have-env
fi

exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR"

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 14:31         ` Robin Bowes
@ 2008-04-14 16:41           ` Andras Korn
  2008-04-14 21:00             ` Robin Bowes
  0 siblings, 1 reply; 19+ messages in thread
From: Andras Korn @ 2008-04-14 16:41 UTC (permalink / raw)
  To: supervision

On Mon, Apr 14, 2008 at 03:31:16PM +0100, Robin Bowes wrote:

> And, it would perhaps be less cluttered to do:
>
> #!/bin/sh
>
> if [ "$1" != "have-env" ]; then
>   exec chpst -e ./env "$0" have-env
> fi
>
> exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR"

Yes, that's even better. :)

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
                     NAVY: Never Again Volunteer Yourself


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 16:41           ` Andras Korn
@ 2008-04-14 21:00             ` Robin Bowes
  2008-04-14 21:09               ` Charlie Brady
                                 ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Robin Bowes @ 2008-04-14 21:00 UTC (permalink / raw)
  To: supervision

Andras Korn wrote:
> On Mon, Apr 14, 2008 at 03:31:16PM +0100, Robin Bowes wrote:
> 
>> And, it would perhaps be less cluttered to do:
>>
>> #!/bin/sh
>>
>> if [ "$1" != "have-env" ]; then
>>   exec chpst -e ./env "$0" have-env
>> fi
>>
>> exec chpst -u "$LOGUSER" svlogd -tt "$LOGDIR"
> 
> Yes, that's even better. :)
> 

OK, here's another refinement.

This script:

1. Uses chpst -e to set some vars from ./env (if ./env exists)
2. Uses the name of the service dir as the log user and log dir, if
    the are not set in ./env
3. Creates the log dir, if it does not exist, and sets permissions

#!/bin/sh

# if we haven't already done so, check for ./env dir
# if it exists, set some env vars
if [ "$1" != "have-env" ]; then
   if [ -d ./env ]; then
     exec chpst -e ./env "$0" have-env
   fi
fi

LOG_PARENT=/var/log

# Get the service name
logdir=`pwd`
svcdir=${logdir%/log}
SERVICE_NAME=${svcdir##*/}

# Set LOGUSER and LOGDIR to default values if not set in ./env
: ${LOGUSER:=${LOG_PARENT}/${SERVICE_NAME}}
: ${LOGDIR:=${LOG_PARENT}/${SERVICE_NAME}}

# Make sure the log dir exists
if [ ! -e "${LOGDIR}" ]; then
   mkdir -p "${LOGDIR}"
fi
# Set ownership & permissions on the log dir
chown -R ${LOGUSER} "${LOGDIR}"
chmod 750 "${LOGDIR}"
chmod g+s "${LOGDIR}"

# Now run the log process
exec chpst -u "${LOGUSER}" svlogd -tt "${LOGDIR}"


Any thoughts?

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 21:00             ` Robin Bowes
@ 2008-04-14 21:09               ` Charlie Brady
  2008-04-14 21:16                 ` Robin Bowes
  2008-04-14 22:17               ` Joan Picanyol i Puig
  2008-04-14 22:18               ` Andras Korn
  2 siblings, 1 reply; 19+ messages in thread
From: Charlie Brady @ 2008-04-14 21:09 UTC (permalink / raw)
  To: Robin Bowes; +Cc: supervision


On Mon, 14 Apr 2008, Robin Bowes wrote:

> This script:
>
> 1. Uses chpst -e to set some vars from ./env (if ./env exists)
> 2. Uses the name of the service dir as the log user and log dir, if
>   the are not set in ./env
> 3. Creates the log dir, if it does not exist, and sets permissions
...
> Any thoughts?

IMO directories should be created once and only once, probably at software 
install time.

IMO there should be only one way to set log user and log dir, and I'd be 
happy for that to be hard coded.

I like things as simple as possible (but no simpler), as recommended by 
Albert.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 21:09               ` Charlie Brady
@ 2008-04-14 21:16                 ` Robin Bowes
  0 siblings, 0 replies; 19+ messages in thread
From: Robin Bowes @ 2008-04-14 21:16 UTC (permalink / raw)
  To: supervision

Charlie Brady wrote:
> 
> On Mon, 14 Apr 2008, Robin Bowes wrote:
> 
>> This script:
>>
>> 1. Uses chpst -e to set some vars from ./env (if ./env exists)
>> 2. Uses the name of the service dir as the log user and log dir, if
>>   the are not set in ./env
>> 3. Creates the log dir, if it does not exist, and sets permissions
> ...
>> Any thoughts?
> 
> IMO directories should be created once and only once, probably at 
> software install time.
> 
> IMO there should be only one way to set log user and log dir, and I'd be 
> happy for that to be hard coded.

I don't disagree.

I was aiming to write a generic script that can be symlinked into the 
log dir of a service and "just work", but also be customised if required 
(e.g. using "dnslog" as the log user for dnscache and tinydns) by 
echoing into ./env.

> I like things as simple as possible (but no simpler), as recommended by 
> Albert.

/me too.

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 21:00             ` Robin Bowes
  2008-04-14 21:09               ` Charlie Brady
@ 2008-04-14 22:17               ` Joan Picanyol i Puig
  2008-04-14 22:18               ` Andras Korn
  2 siblings, 0 replies; 19+ messages in thread
From: Joan Picanyol i Puig @ 2008-04-14 22:17 UTC (permalink / raw)
  To: supervision

* Robin Bowes <robin-lists@robinbowes.com> [20080414 23:48]:
> 3. Creates the log dir, if it does not exist, and sets permissions
> 
> #!/bin/sh
> 
[snip env setting]
> LOG_PARENT=/var/log
> 
> # Get the service name
> logdir=`pwd`
> svcdir=${logdir%/log}
> SERVICE_NAME=${svcdir##*/}
> 
> # Set LOGUSER and LOGDIR to default values if not set in ./env
> : ${LOGUSER:=${LOG_PARENT}/${SERVICE_NAME}}
> : ${LOGDIR:=${LOG_PARENT}/${SERVICE_NAME}}
> 
> # Make sure the log dir exists
> if [ ! -e "${LOGDIR}" ]; then
>   mkdir -p "${LOGDIR}"
> fi
> # Set ownership & permissions on the log dir
> chown -R ${LOGUSER} "${LOGDIR}"
> chmod 750 "${LOGDIR}"
> chmod g+s "${LOGDIR}"

I've been using this:

#!/command/execlineb

emptyenv -p multisubstitute {
  define SIZE s1000000
  define NUM n10
  define DIR ./main/
}

envdir ./env/
statfile -ug ${DIR}
setstate -ug
multilog t ${NUM} ${SIZE} ${PROC} ${DIR}

> Any thoughts?

I should protect mine against a non-existant env, but I don't know how
to use execline's if with statfile's env var setting; Paul, Laurent?

qvb
--
pica


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 21:00             ` Robin Bowes
  2008-04-14 21:09               ` Charlie Brady
  2008-04-14 22:17               ` Joan Picanyol i Puig
@ 2008-04-14 22:18               ` Andras Korn
  2008-04-14 23:05                 ` Robin Bowes
  2 siblings, 1 reply; 19+ messages in thread
From: Andras Korn @ 2008-04-14 22:18 UTC (permalink / raw)
  To: supervision

On Mon, Apr 14, 2008 at 10:00:15PM +0100, Robin Bowes wrote:

>
> This script:
>
> 1. Uses chpst -e to set some vars from ./env (if ./env exists)
> 2. Uses the name of the service dir as the log user and log dir, if
>    the are not set in ./env
> 3. Creates the log dir, if it does not exist, and sets permissions

Here is what I use as a generic log/run script, fwiw:

---8<---
#!/bin/sh
SVNAME=$(basename $(dirname $(readlink -f .)))
LOGDIR=/var/log/sv/$SVNAME
LOGUSER=log
SIZE=50000
NUM=15
POSTPROC="gzip -9"
SVLOGDOPTS="-t"

[ -r /etc/default/svlogd ] && . /etc/default/svlogd
[ -r "/etc/default/$SVNAME" ] && . "/etc/default/$SVNAME"

mkdir -p $LOGDIR

chown $LOGUSER $LOGDIR $LOGDIR/lock $LOGDIR/state $LOGDIR/current 2>/dev/null
chmod 1700 $LOGDIR

cd $LOGDIR || exit 1
if [ ! -f config ]; then
	cat <<EOF >config
s$SIZE
n$NUM
!tryto -pP $POSTPROC
EOF
fi

exec chpst -u$LOGUSER svlogd $SVLOGDOPTS $LOGDIR
--->8---

This is more "Debianish" in that it gets the configuration from
/etc/default/svlogd, possibly overridden by /etc/default/name-of-service.
These files can override the svlogd config as well (e.g. set a different
postprocessor or retention policy).

I keep /var/log/sv on a separate volume from /var/log because not everything
logs via svlogd and /var/log might be filled.

I went easy on the quoting because I know I don't use LOGDIRs with spaces in
them.

Log directories are sticky and 'config' is owned by root; thus, in the
unlikely event that svlogd were to be compromised, it still couldn't
overwrite its own configuration. Yay!

As for your script, some comments:

> #!/bin/sh
>
> # if we haven't already done so, check for ./env dir
> # if it exists, set some env vars
> if [ "$1" != "have-env" ]; then
>   if [ -d ./env ]; then
>     exec chpst -e ./env "$0" have-env
>   fi
> fi
>
> LOG_PARENT=/var/log
>
> # Get the service name
> logdir=`pwd`
> svcdir=${logdir%/log}
> SERVICE_NAME=${svcdir##*/}

Isn't this a bashism?

> # Set LOGUSER and LOGDIR to default values if not set in ./env
> : ${LOGUSER:=${LOG_PARENT}/${SERVICE_NAME}}
> : ${LOGDIR:=${LOG_PARENT}/${SERVICE_NAME}}

You could use LOGDIR=${LOGDIR:-$LOG_PARENT/$SERVICE_NAME} for no real
benefit. Setting LOGUSER to the name of a directory is probably a
copy-paste mistake, I guess? :)

> # Make sure the log dir exists
> if [ ! -e "${LOGDIR}" ]; then
>   mkdir -p "${LOGDIR}"
> fi

This fails if LOGDIR exists but is not a directory. (Of course, that
shouldn't happen.)

> # Set ownership & permissions on the log dir
> chown -R ${LOGUSER} "${LOGDIR}"

This gives ownership of the config file, if it exists, to svlogd. I don't
think that's good. 

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
         "Meow" ...splat... "Aarf" ...splat... (raining cats and dogs)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 22:18               ` Andras Korn
@ 2008-04-14 23:05                 ` Robin Bowes
  2008-04-15  4:27                   ` Andras Korn
  0 siblings, 1 reply; 19+ messages in thread
From: Robin Bowes @ 2008-04-14 23:05 UTC (permalink / raw)
  To: supervision

Andras Korn wrote:
> This is more "Debianish" in that it gets the configuration from
> /etc/default/svlogd, possibly overridden by /etc/default/name-of-service.
> These files can override the svlogd config as well (e.g. set a different
> postprocessor or retention policy).

That's an idea. I may incorporate that (I'm using RH-flavours).

> I keep /var/log/sv on a separate volume from /var/log because not everything
> logs via svlogd and /var/log might be filled.

OK.

> As for your script, some comments:
> 
>> # Get the service name
>> logdir=`pwd`
>> svcdir=${logdir%/log}
>> SERVICE_NAME=${svcdir##*/}
> 
> Isn't this a bashism?

Yes. I welcome suggestions how to do this in a more portable way.

>> # Set LOGUSER and LOGDIR to default values if not set in ./env
>> : ${LOGUSER:=${LOG_PARENT}/${SERVICE_NAME}}
>> : ${LOGDIR:=${LOG_PARENT}/${SERVICE_NAME}}
> 
> You could use LOGDIR=${LOGDIR:-$LOG_PARENT/$SERVICE_NAME} for no real
> benefit.

Yes, indeed.

> Setting LOGUSER to the name of a directory is probably a
> copy-paste mistake, I guess? :)

No, it's based on the premise that each service is run by a user with 
the same name. This is over-ridden by the contents of ./env/LOGUSER

>> # Make sure the log dir exists
>> if [ ! -e "${LOGDIR}" ]; then
>>   mkdir -p "${LOGDIR}"
>> fi
> 
> This fails if LOGDIR exists but is not a directory. (Of course, that
> shouldn't happen.)

True. I could use -d, but then do I test for $LOGDIR existing but not 
being a directory? One has to stop somewhere...!

>> # Set ownership & permissions on the log dir
>> chown -R ${LOGUSER} "${LOGDIR}"
> 
> This gives ownership of the config file, if it exists, to svlogd. I don't
> think that's good. 

No, it gives ownership of the logdir to the user running the log process 
so it can write to that dir.

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-14 23:05                 ` Robin Bowes
@ 2008-04-15  4:27                   ` Andras Korn
  2008-04-15 12:10                     ` Robin Bowes
  0 siblings, 1 reply; 19+ messages in thread
From: Andras Korn @ 2008-04-15  4:27 UTC (permalink / raw)
  To: supervision

On Tue, Apr 15, 2008 at 12:05:02AM +0100, Robin Bowes wrote:

>>> # Get the service name
>>> logdir=`pwd`
>>> svcdir=${logdir%/log}
>>> SERVICE_NAME=${svcdir##*/}
>> Isn't this a bashism?
>
> Yes. I welcome suggestions how to do this in a more portable way.

How about

SERVICE_NAME=$(basename $(dirname $(readlink -f .)))

Of course, I don't know how portable readlink is.

>> Setting LOGUSER to the name of a directory is probably a
>> copy-paste mistake, I guess? :)
>
> No, it's based on the premise that each service is run by a user with the 
> same name. This is over-ridden by the contents of ./env/LOGUSER

Wow. I never considered using usernames with slashes in them, but of course,
why not?

>>> # Make sure the log dir exists
>>> if [ ! -e "${LOGDIR}" ]; then
>>>   mkdir -p "${LOGDIR}"
>>> fi
>> This fails if LOGDIR exists but is not a directory. (Of course, that
>> shouldn't happen.)
>
> True. I could use -d, but then do I test for $LOGDIR existing but not being 
> a directory? One has to stop somewhere...!

You're right, but at least "cd $LOGDIR || exit 1" fails in a predictable
way. :)

>>> # Set ownership & permissions on the log dir
>>> chown -R ${LOGUSER} "${LOGDIR}"
>> This gives ownership of the config file, if it exists, to svlogd. I don't
>> think that's good. 
>
> No, it gives ownership of the logdir to the user running the log process so 
> it can write to that dir.

chown -R is recursive, meaning that it will chown the svlogd 'config' file
which resides in the logdir to the user svlogd will run as.

I agree this only has theoretical significance.

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
              Smile... people will wonder what you've been up to.


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-15  4:27                   ` Andras Korn
@ 2008-04-15 12:10                     ` Robin Bowes
  2008-04-15 16:31                       ` Generic logging run script Robin Bowes
  2008-04-15 16:33                       ` Help with chpst -e Andras Korn
  0 siblings, 2 replies; 19+ messages in thread
From: Robin Bowes @ 2008-04-15 12:10 UTC (permalink / raw)
  To: supervision

Andras Korn wrote:
> How about
> 
> SERVICE_NAME=$(basename $(dirname $(readlink -f .)))
> 
> Of course, I don't know how portable readlink is.

I seem to remember reading that readlink is OS-dependent, but it's 
shell-agnostic.

>>> Setting LOGUSER to the name of a directory is probably a
>>> copy-paste mistake, I guess? :)
>> No, it's based on the premise that each service is run by a user with the 
>> same name. This is over-ridden by the contents of ./env/LOGUSER
> 
> Wow. I never considered using usernames with slashes in them, but of course,
> why not?

*blush* - sorry, you were right. It was a mistake. It should of course be:

# Set LOGUSER and LOGDIR to default values if not set in ./env
: ${LOGUSER:=${SERVICE_NAME}}
: ${LOGDIR:=${LOG_PARENT}/${SERVICE_NAME}}


>>>> # Make sure the log dir exists
>>>> if [ ! -e "${LOGDIR}" ]; then
>>>>   mkdir -p "${LOGDIR}"
>>>> fi
>>> This fails if LOGDIR exists but is not a directory. (Of course, that
>>> shouldn't happen.)
>> True. I could use -d, but then do I test for $LOGDIR existing but not being 
>> a directory? One has to stop somewhere...!
> 
> You're right, but at least "cd $LOGDIR || exit 1" fails in a predictable
> way. :)

Yeah, I'll give that some more thought.

>>>> # Set ownership & permissions on the log dir
>>>> chown -R ${LOGUSER} "${LOGDIR}"
>>> This gives ownership of the config file, if it exists, to svlogd. I don't
>>> think that's good. 
>> No, it gives ownership of the logdir to the user running the log process so 
>> it can write to that dir.
> 
> chown -R is recursive, meaning that it will chown the svlogd 'config' file
> which resides in the logdir to the user svlogd will run as.
> 
> I agree this only has theoretical significance.

Again, you're correct; I forgot about the config file - I've only just 
started using runit (vs. daemontools).

I don't think it's a major issue, but could be fixed by:

chown root "${LOGDIR}"/config

R.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Generic logging run script
  2008-04-15 12:10                     ` Robin Bowes
@ 2008-04-15 16:31                       ` Robin Bowes
  2008-04-15 16:33                       ` Help with chpst -e Andras Korn
  1 sibling, 0 replies; 19+ messages in thread
From: Robin Bowes @ 2008-04-15 16:31 UTC (permalink / raw)
  To: supervision

Hi all,

I've taken some of the ideas/suggestions from this thread and come up 
with this generic logging script.

I've written it to use /etc/sysconfig/ for configuration, as I work 
mainly on RH-flavour distributions and that's what they use.

To use:

1. Save the script somewhere and chmod +x
2. symlink the script into your services' log dir

By default it will run as user $SVNAME and log to dir /var/log/sv/$SVNAME

If you want to change this, either for all services, or for just one 
service, you'll need to create the following dir structure:

   mkdir -p /etc/sysconfig/sv/default/log/

Then for each of your services:

   mkdir -p /etc/sysconfig/sv/$SVNAME/log/

To change something for all services:

   echo "LOGPARENT=/var/log" > /etc/sysconfig/sv/default/log/settings

To change something for just one service:

   echo "LOGUSER=dnslog" > /etc/sysconfig/sv/dnscache/log/settings
   echo "LOGUSER=dnslog" > /etc/sysconfig/sv/tinydns/log/settings

If you want to use something other than the standard svlogd logging 
configuration, create a config file:

cat > /etc/sysconfig/sv/default/log/config << EOCONFIG
s2000000
n20
EOCONFIG

Or for a specific service:

cat > /etc/sysconfig/sv/tinydns/log/config << EOCONFIG
s4000000
n50
EOCONFIG

As always, I'd appreciate any comments/suggestions.

Here's the script:

#!/bin/sh

# This is a generic logging script services using runit/svlogd
#
# The service configuration is taken from the following files:
#   /etc/sysconfig/sv/$SVNAME/log/settings
#   /etc/sysconfig/sv/default/log/settings
#
# The following ENV vars can be set in the "settings" files:
#    LOGPARENT - log dir will be set to $LOGPARENT/$SVNAME
#    LOGDIR    - full path. If specified, over-rides $LOGPARENT
#    LOGUSER   - user the logging process should run as
#
# The service-specific files are used in preference to the defaults.
#
# If no settings files are found, the logging service will run as user
# $SVNAME and write to "/var/log/sv/$SVNAME"
#
# The behaviour of the svlogd is controlled by a file $LOGDIR/config.
# This file is copied from:
#   /etc/sysconfig/sv/$SVNAME/log/config
#   /etc/sysconfig/sv/default/log/config
#
# The service-specific files are used in preference to the defaults.
#
# If no config files are found, svlogd defaults are used.
#
# Copyright (c) 2008 by Robin Bowes <robin@robinbowes.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Get the name of the service this log belongs to
SVNAME=$(basename $(dirname $(readlink -f .)))

# Where to look for configuration settings
CFGBASE=/etc/sysconfig/sv

# cp is aliased to "cp -i" on some distros
cp="/bin/cp"

# Get defaults for logging and this service log
[ -r "${CFGBASE}/default/log/settings" ] && \
   . "${CFGBASE}/default/log/settings"
[ -r "${CFGBASE}/${SVNAME}/log/settings" ] && \
   . "${CFGBASE}/${SVNAME}/log/settings"

# Set LOG_PARENT, LOGUSER and LOGDIR to default values if not set already
LOGUSER=${LOGUSER:-$SVNAME}
LOGPARENT=${LOGPARENT:-/var/log/sv}
LOGDIR=${LOGDIR:-"$LOGPARENT/${SVNAME}"}

# make sure log parent exists
[ -d "${LOGPARENT}" ] || exit 1 ]

# Make sure the log dir exists
[ -d "${LOGDIR}" ] || mkdir "${LOGDIR}" || exit 1

# Configure logging. Check for sv-specific then default config
if [ -r "${CFGBASE}/${SVNAME}/log/config}" ]; then
   ${cp} "${CFGBASE}/${SVNAME}/log/config}" "${LOGDIR}/config"
elif [ -r "${CFGBASE}/default/log/config}" ]; then
   ${cp} "${CFGBASE}/default/log/config}" "${LOGDIR}/config"
fi

# Set ownership on the log dir
chown -R ${LOGUSER} "${LOGDIR}"
# reset perms on log config file, if it exists
[ -r "${LOGDIR}/config" ] && chown root "${LOGDIR}/config"
# Set perms on the log dir
chmod 750 "${LOGDIR}"
chmod g+s "${LOGDIR}"

# Now run the log process
exec chpst -u "${LOGUSER}" svlogd -tt "${LOGDIR}"



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Help with chpst -e
  2008-04-15 12:10                     ` Robin Bowes
  2008-04-15 16:31                       ` Generic logging run script Robin Bowes
@ 2008-04-15 16:33                       ` Andras Korn
  1 sibling, 0 replies; 19+ messages in thread
From: Andras Korn @ 2008-04-15 16:33 UTC (permalink / raw)
  To: supervision

On Tue, Apr 15, 2008 at 01:10:09PM +0100, Robin Bowes wrote:

>> chown -R is recursive, meaning that it will chown the svlogd 'config' file
>> which resides in the logdir to the user svlogd will run as.
>> I agree this only has theoretical significance.
>
> Again, you're correct; I forgot about the config file - I've only just 
> started using runit (vs. daemontools).
>
> I don't think it's a major issue, but could be fixed by:
>
> chown root "${LOGDIR}"/config

If we're going to be hypercorrect, that solution introduces a race
condition. :)

Andras

-- 
                 Andras Korn <korn at chardonnay.math.bme.hu>
                 <http://chardonnay.math.bme.hu/~korn/>	QOTD:
           I haven't lost my mind; it's backed up on tape somewhere.


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2008-04-15 16:33 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-13 23:19 Help with chpst -e Robin Bowes
2008-04-13 23:52 ` Robin Bowes
2008-04-14  8:34   ` Andras Korn
2008-04-14  8:40     ` Andras Korn
2008-04-14 13:38     ` Robin Bowes
2008-04-14 13:48       ` Andras Korn
2008-04-14 14:16       ` Charlie Brady
2008-04-14 14:31         ` Robin Bowes
2008-04-14 16:41           ` Andras Korn
2008-04-14 21:00             ` Robin Bowes
2008-04-14 21:09               ` Charlie Brady
2008-04-14 21:16                 ` Robin Bowes
2008-04-14 22:17               ` Joan Picanyol i Puig
2008-04-14 22:18               ` Andras Korn
2008-04-14 23:05                 ` Robin Bowes
2008-04-15  4:27                   ` Andras Korn
2008-04-15 12:10                     ` Robin Bowes
2008-04-15 16:31                       ` Generic logging run script Robin Bowes
2008-04-15 16:33                       ` Help with chpst -e Andras Korn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).