private static void writePid ( String file , boolean ignorePidFile ) { File pidFile = new File ( file ) ; if ( pidFile . exists ( ) ) { Long oldPid = getPid ( pidFile ) ; if ( oldPid == null ) { pidFile . delete ( ) ; } else { log . warn ( "\n\t==================================\n\tThe OpenTSDB PID file [" + file + "] already exists for PID [" + oldPid + "]. \n\tOpenTSDB might already be running.\n\t==================================\n" ) ; if ( ! ignorePidFile ) { log . warn ( "Exiting due to existing pid file. Start with option --ignore-existing-pid to overwrite" ) ; System . exit ( - 1 ) ; } else { log . warn ( "Deleting existing pid file [" + file + "]" ) ; pidFile . delete ( ) ; } } } pidFile . deleteOnExit ( ) ; File pidDir = pidFile . getParentFile ( ) ; FileOutputStream fos = null ; try { if ( ! pidDir . exists ( ) ) { if ( ! pidDir . mkdirs ( ) ) { throw new Exception ( "Failed to create PID directory [" + file + "]" ) ; } } fos = new FileOutputStream ( pidFile ) ; String PID = ManagementFactory . getRuntimeMXBean ( ) . getName ( ) . split ( "@" ) [ 0 ] ; fos . write ( String . format ( "%s%s" , PID , EOL ) . getBytes ( ) ) ; fos . flush ( ) ; fos . close ( ) ; fos = null ; log . info ( "PID [" + PID + "] written to pid file [" + file + "]" ) ; } catch ( Exception ex ) { log . error ( "Failed to write PID file to [" + file + "]" , ex ) ; throw new IllegalArgumentException ( "Failed to write PID file to [" + file + "]" , ex ) ; } finally { if ( fos != null ) try { fos . close ( ) ; } catch ( Exception ex ) { /* No Op */ } } }
Writes the PID to the file at the passed location