5

I'm using apache2 and postgres running on Ubuntu Server 10.04.
I have removed the startup scripts for both of these apps and I'm using supervisor to monitor and control them. The problem I have run into is that both of these need directories in /var/run (with the correct permissions for the users they run under) for pid files. How do I create these during startup as they need to be created as root and then chown'd to the correct user?

Edit
It seems the best way to so this is to creat the directories with custom init scripts. As I have no shell scripting skills at all how do I go about this?

2
  • Can't you create this directories during installation and just leave it there until the server reboots? Sep 16, 2010 at 0:00
  • Since you are using a process supervisor, you should not need pid files. Can you configure apache2 and postgres not to create them?
    – camh
    Sep 16, 2010 at 3:15

5 Answers 5

6

According to Debian policy,

/var/run and /var/lock may be mounted as temporary filesystems, so the init.d scripts must handle this correctly. This will typically amount to creating any required subdirectories dynamically when the init.d script is run, rather than including them in the package and relying on dpkg to create them.

Obviously, Ubuntu inherits from Debian, and as far as I know this policy is unchanged there.

The best solution is to modify your new startup scripts such that when the services are launched, if these directories do not exist, they will be created.

1
  • There are currently no startup scripts for the servcies. The supervisor daemon is started by the init.d scripts and then the other services are started by this service, which should not run as root.
    – Frozenskys
    Sep 16, 2010 at 8:42
4

This kind of thing should be put in "/etc/rc.local". Example:

#!/bin/sh -e
# 
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

if [ ! -d /var/run/foo ]; then
  mkdir /var/run/foo/
  chown user1:group1 /var/run/foo/
fi
if [ ! -d /var/run/bar ]; then
  mkdir /var/run/bar/
  chown user2:group2 /var/run/bar/
fi
exit 0
1

In reply to this comment:

There are currently no startup scripts for the servcies. The supervisor daemon is started by the init.d scripts and then the other services are started by this service, which should not run as root.

If your supervisor is started from init.d script, then just create another init.d script with the preferences to be run before the supervisor starts ( how you achieve this is totally dependent on your flavor of **IX ).

In its start method create needed directories with required permissions.

In its stop method tear those directories down.

1
  • That sounds fine - combined with "Create a file of all directories to be created at startup, then write a program that create all of those files and run it at startup." it should be perfect for me. I don't need to teardown as /var/run is mounted as tmpfs.
    – Frozenskys
    Sep 16, 2010 at 12:17
1

In the end I used this code in the init.d script for the supervisor process:

while IFS=: read file user group
    do
        if [ -d $file ]; then
            chmod 2775 $file
        else
            install -d -m 2775 -o $user -g $group $file
        fi
    done < "/etc/run_dirs.txt"

Which reads a file containing rows with the following format and creates the appropriate dirs and permissions:

path/to/dir:user:group

0

I see a few options:

  1. Create the directories at install time
  2. Create a file of all directories to be created at startup, then write a program that create all of those files and run it at startup (from a startup script that has root privs).

Other options that are riffs on this could also be made to work.

1
  • 7
    Creating the directories at install time is no good, since /var/run is lost after each restart
    – Rich
    Jun 4, 2013 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.