Put a PID into a pidfile
331358
def put_pidfile( pidfile_path, pid ):
    
    with open( pidfile_path, "w" ) as f:
        f.write("%s" % pid)
        os.fsync(f.fileno())

    return
642373
def make(self, pnum):
        
        try:
                
            # Create the PID file
            self.mkfile(self.pid_file, pnum)
        except Exception as e:
            self.die('Failed to generate PID file: {}'.format(str(e)))
539880
def write_pid(self, pid):
        
        self.fpid.seek(0)
        self.fpid.truncate()
        self.fpid.write("%d" % pid)
        self.fpid.close()
        del self.fpid
641658
def start(self, *args):
        

        if os.path.exists(self.pidfile):
            msg = "pidfile exists. Exit.\n"
            sys.stderr.write(msg % self.pidfile)
            sys.exit(1)

        self.daemonize()
        self.run(*args)
1025466
def read_pid_from_pidfile(pidfile_path):
    
    pid = None
    try:
        pidfile = open(pidfile_path, 'r')
    except IOError:
        pass
    else:
        # According to the FHS 2.3 section on PID files in /var/run:
        # 
        #   The file must consist of the process identifier in
        #   ASCII-encoded decimal, followed by a newline character.
        # 
        #   Programs that read PID files should be somewhat flexible
        #   in what they accept; i.e., they should ignore extra
        #   whitespace, leading zeroes, absence of the trailing
        #   newline, or additional lines in the PID file.

        line = pidfile.readline().strip()
        try:
            pid = int(line)
        except ValueError:
            pass
        pidfile.close()

    return pid
22752
def update_pid
      pid_from_file = self.pid_from_file
      if pid_from_file && pid_from_file != @pid
        @pid = pid_from_file
        @started_at = File.mtime(self.pid_file_path)
        Procodile.log(@process.log_color, description, "PID file changed. Updated pid to #{@pid}")
        true
      else
        false
      end
    end
470199
def _setup_piddir(self):
        """"""
        if self.pidfile is None:
            return
        piddir = os.path.dirname(self.pidfile)
        if not os.path.isdir(piddir):
            # Create the directory with sensible mode and ownership
            os.makedirs(piddir, 0o777 & ~self.umask)
            os.chown(piddir, self.uid, self.gid)
1303858
def start(self):
        
        # Check for a pidfile to see if the daemon already runs
        try:
            pf = file(self.pidfile, 'r')
            pid = int(pf.read().strip())
            pf.close()
            os.kill(pid, 0)
        except IOError:
            pid = None
        except OSError:
            pid = None

        if pid:
            message = "pidfile %s already exist. Daemon already running?\n"
            sys.stderr.write(message % self.pidfile)
            sys.exit(1)

        # 
        self._daemonize()
706743
private static Long getPid(File pidFile) {
      FileReader reader = null;
      BufferedReader lineReader = null;
      String pidLine  = null;
      try {
        reader = new FileReader(pidFile);
        lineReader = new BufferedReader(reader);
        pidLine = lineReader.readLine();
        if(pidLine!=null) {
          pidLine = pidLine.trim();     
        }   
      } catch (Exception ex) {
        log.error("Failed to read PID from file  [" + pidFile.getAbsolutePath() + "]", ex);
      } finally {
        if(reader!=null) try { reader.close(); } catch (Exception ex) { /* No Op */ }
      }
      try {
        return Long.parseLong(pidLine);
      } catch (Exception ex) {
        return null;
      }     
    }
1017131
function writePIDFile(pid, name) {
  var pidFilePath = pidDir + name + '.pid';
  exec('mkdir -p ' + pidDir, function(err, stdout, stderr) {
    if (err) {
      winston.error('[forewoman] Error creating the pids directory');
      winston.error(err.message);
    } else {
      fs.writeFileSync(pidFilePath, pid);
    }
  });
}
1126407
def write_pid(file)
      begin
        File.open(file, "w") do |pid_file|
          pid_file.puts(Process.pid)
        end
      rescue
        @logger.fatal("could not write to pid file", :pid_file => file)
        @logger.fatal("SENSU NOT RUNNING!")
        exit 2
      end
    end
203202
def get_process_pids(self, process):
        """"""
        pids = []
        cmd_line_glob = "/proc/[0-9]*/cmdline"
        cmd_line_paths = glob.glob(cmd_line_glob)
        for path in cmd_line_paths:
            try:
                with open(path, 'r') as f:
                    cmd_line = f.read().strip()
                    if process in cmd_line:
                        pids.append(path.split("/")[2])
            except IOError as e:
                continue
        return pids
1155569
def _write_pidfile(self):
        """"""
        LOGGER.debug('Writing pidfile: %s', self.pidfile_path)
        with open(self.pidfile_path, "w") as handle:
            handle.write(str(os.getpid()))
205883
def check_process(pidfile):
    
    # Check pid file
    try:
        handle = open(pidfile, 'r')
    except IOError as exc:
        if exc.errno == errno.ENOENT:
            # pid file disappeared
            return False, 0
        raise

    try:
        pid = int(handle.read().strip(), 10)
    except (TypeError, ValueError) as exc:
        raise EnvironmentError("Invalid PID file '%s' (%s), won't start!" % (pidfile, exc))
    finally:
        handle.close()

    # Check process
    try:
        os.kill(pid, 0)
    except EnvironmentError as exc:
        return False, pid
    else:
        return True, pid
1259349
def lock_pidfile_or_die(pidfile):
    
    pid = os.getpid()
    try:
        remove_if_stale_pidfile(pidfile)
        pid_write_file = pidfile + '.' + str(pid)
        fpid = open(pid_write_file, 'w')
        try:
            fpid.write("%s\n" % pid)
        finally:
            fpid.close()
        if not take_file_lock(pid_write_file, pidfile, "%s\n" % pid):
            sys.exit(1)
    except SystemExit:
        raise
    except Exception:
        log.exception("unable to take pidfile")
        sys.exit(1)
    return pid
89938
def pid(self):
        """"""
        try:

            os.remove(self.pidfile)

        except IOError:

            if not os.path.isfile(self.pidfile):

                return None

            LOG.exception("Failed to clear pidfile {0}).".format(self.pidfile))
            sys.exit(exit.PIDFILE_INACCESSIBLE)
32073
def write_pidfile(pidfile):
    
    pid = os.getpid()
    if pidfile:
        with open(pidfile, "w", encoding="utf-8") as fobj:
            fobj.write(six.u(str(pid)))
    return pid