class ProcessUtil {

    public static void pidCheck(String pidDir, String run) throws ConfigurationException {

        String dir = pidDir == null ? "/var/run" : pidDir;

        try {
            final File propsFile = PropertiesUtil.findConfigFile("environment.properties");
            if (propsFile == null) {
                s_logger.debug("environment.properties could not be opened");
            } else {
                final Properties props = PropertiesUtil.loadFromFile(propsFile);
                dir = props.getProperty("paths.pid");
                if (dir == null) {
                    dir = pidDir == null ? "/var/run" : pidDir;
                }
            }
        } catch (IOException e) {
            s_logger.debug("environment.properties could not be opened");
        }

        final File pidFile = new File(dir + File.separator + run);
        try {
            if (!pidFile.createNewFile()) {
                if (!pidFile.exists()) {
                    throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath() + ".  Are you sure you're running as root?");
                }

                final String pidLine = FileUtils.readFileToString(pidFile).trim();
                if (pidLine.isEmpty()) {
                    throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                }
                try {
                    final long pid = Long.parseLong(pidLine);
                    final Script script = new Script("bash", 120000, s_logger);
                    script.add("-c", "ps -p " + pid);
                    final String result = script.execute();
                    if (result == null) {
                        throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                    }
                    if (!pidFile.delete()) {
                        throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                    }
                    if (!pidFile.createNewFile()) {
                        throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                    }
                } catch (final NumberFormatException e) {
                    throw new ConfigurationException("Java process is being started twice.  If this is not true, remove " + pidFile.getAbsolutePath());
                }
            }
            pidFile.deleteOnExit();

            final Script script = new Script("bash", 120000, s_logger);
            script.add("-c", "echo $PPID");
            final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
            script.execute(parser);

            final String pid = parser.getLine();

            FileUtils.writeStringToFile(pidFile, pid + "\n");
        } catch (final IOException e) {
            throw new CloudRuntimeException("Unable to create the " + pidFile.getAbsolutePath() + ".  Are you running as root?", e);
        }
    }

}