Skip to content

Commit

Permalink
Issue #10271 - new start.jar --pid-file=<name> arg
Browse files Browse the repository at this point in the history
+ Have jetty.sh use --pid-file=<name> too

Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Aug 8, 2023
1 parent 897b666 commit adca81d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 6 deletions.
9 changes: 3 additions & 6 deletions jetty-home/src/main/resources/bin/jetty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,10 @@ case "$ACTION" in
# FIXME: Broken solution: wordsplitting, pathname expansion, arbitrary command execution, etc.
su - "$JETTY_USER" $SU_SHELL -c "
cd \"$JETTY_BASE\"
echo ${RUN_ARGS[*]} start-log-file=\"$JETTY_START_LOG\" | xargs ${JAVA} > /dev/null &
disown \$!
echo \$! > \"$JETTY_PID\""
echo ${RUN_ARGS[*]} --pid-file=\"$JETTY_PID\" start-log-file=\"$JETTY_START_LOG\" | xargs ${JAVA} > /dev/null &
"
else
echo ${RUN_ARGS[*]} | xargs ${JAVA} > /dev/null &
disown $!
echo $! > "$JETTY_PID"
echo ${RUN_ARGS[*]} | xargs ${JAVA} --pid-file="$JETTY_PID" > /dev/null &
fi

fi
Expand Down
15 changes: 15 additions & 0 deletions jetty-home/src/main/resources/etc/jetty-pid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- =============================================================== -->
<!-- Mixin the Start FileNoticeLifeCycleListener -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.util.component.PidFileLifeCycleListener">
<Arg><Property name="jetty.pid.file" default="./jetty.pid"/></Arg>
</New>
</Arg>
</Call>
</Configure>
13 changes: 13 additions & 0 deletions jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public class StartArgs

private boolean help = false;
private boolean stopCommand = false;
private String pidFile = null;
private List<String> listModules = null;
private List<String> showModules = null;
private boolean listClasspath = false;
Expand Down Expand Up @@ -919,6 +920,11 @@ public String getModuleGraphFilename()
return moduleGraphFilename;
}

public String getPidFile()
{
return pidFile;
}

public Props getProperties()
{
return properties;
Expand Down Expand Up @@ -1169,6 +1175,13 @@ public void parse(final String rawarg, String source)
return;
}

if (arg.startsWith("--pid-file="))
{
setProperty("jetty.pid.file", pidFile, source);
xmlRefs.add("etc/jetty-pid.xml");
return;
}

if (arg.startsWith("--download="))
{
addFile(null, Props.getValue(arg));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// ========================================================================
// Copyright (c) 1995-2023 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.util.component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PidFileLifeCycleListener implements LifeCycle.Listener
{
private static final Logger LOG = LoggerFactory.getLogger(PidFileLifeCycleListener.class);

private final Path pidFile;

public PidFileLifeCycleListener(String filename)
{
pidFile = Paths.get(filename);
}

@Override
public void lifeCycleStarting(LifeCycle event)
{
long pid = ProcessHandle.current().pid();
try
{
Files.writeString(pidFile, Long.toString(pid), StandardCharsets.UTF_8);
}
catch (IOException e)
{
LOG.warn("Unable to create pidFile: {}", pidFile, e);
}
}

@Override
public void lifeCycleFailure(LifeCycle event, Throwable cause)
{
removePid();
}

@Override
public void lifeCycleStopped(LifeCycle event)
{
removePid();
}

private void removePid()
{
try
{
Files.deleteIfExists(pidFile);
}
catch (IOException e)
{
LOG.warn("Unable to remove pidFile: {}", pidFile, e);
}
}
}

0 comments on commit adca81d

Please sign in to comment.