host.anzo.commons.utils.SystemdUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-core Show documentation
Show all versions of commons-core Show documentation
Commons library to make me happy.
package host.anzo.commons.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* @author ANZO
* @since 6/30/2023
*/
@Slf4j
public class SystemdUtils {
public static void notifyReady() {
notify("--ready");
}
public static void setStatus(String status) {
notify("--status=" + status);
}
private static void notify(String param) {
if (System.getenv("NOTIFY_SOCKET") == null) {
return;
}
try {
final Process process = new ProcessBuilder("systemd-notify", param)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start();
final int exitCode = process.waitFor();
if (exitCode != 0) {
log.error("Can't notify systemd exitCode={}", exitCode);
}
}
catch (IOException | InterruptedException e) {
log.error("Can't notify systemd", e);
}
}
}