Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-06

check if process ID is running under BSD

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner

Hi all,

I need to check under BSD if a PID is running and what name it has. Is there already a function for that?

Mattias

phoebus phoebus
to Mattias Gaertner

Hi Mattias,

I don't know if it exist a FPC function for that.

i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk '{print $8}'".

I 'm assuming the output is:
user1 2793 2789 0 22:52 pts/0 00:00:00 bash

and encapsulate the command is one of the following system call.

1) TProcess (http://wiki.freepascal.org/Executing_External_Programs)
2) SysUtils.ExecuteProcess
(http://wiki.freepascal.org/Executing_External_Programs)
3) fpsystem
(http://wiki.freepascal.org/Console_Mode_Pascal#Execute_shell_command)
4) POpen (http://www.freepascal.org/docs-html/rtl/unix/popen.html)
5) Shell (http://www.freepascal.org/docs-html/rtl/unix/shell.html)
6) FpExecve (http://www.freepascal.org/docs-html/rtl/baseunix/fpexecve.html)

Choice 5 is quick to implement and you can move to others if you want later.

Hope my update help you, if the way i'll do it. I don't think there are some major difference between FPC implementation on Linux and BSD.

Regards,
Phoebus

Earlier message 12 lines
----- Message d'origine ---- De : Mattias Gaertner À : FPC-Pascal users discussions Envoyé le : Lun 6 juin 2011, 21h 50min 06s Objet : [fpc-pascal] check if process ID is running under BSD Hi all, I need to check under BSD if a PID is running and what name it has. Is there already a function for that? Mattias
Mattias Gaertner
to phoebus phoebus
Show quoted text 6 lines
Hi Mattias, I don't know if it exist a FPC function for that. i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk '{print $8}'".

Thanks, I knew that.
I prefer a more direct way - for example via a sysctl.

I need to check under BSD if a PID is running and what name it has. Is there already a function for that?

Mattias

Marco van de Voort
to Mattias Gaertner: On Mon, 6 Jun 2011 22:02:39 +0100 (BST) Thanks, I knew that. I prefer a more direct way -…

In our previous episode, Mattias Gaertner said:

Show quoted text 10 lines
> I don't know if it exist a FPC function for that. > > i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk > '{print $8}'". Thanks, I knew that. I prefer a more direct way - for example via a sysctl. I need to check under BSD if a PID is running and what name it has. Is there already a function for that?

2.5.1 since r17692 try this:

uses freebsd,baseunix,sysutils;
var pid : pid_t;
    s: ansistring;
begin
  pid:=strtointdef(paramstr(1),-1);
  writeln('pid (-1 for current):',pid);
  // loads name of process + args with #0's inbetween.
  if kernproc_getargs(pid,s)-1 then // does not work with -1
    writeln('arguments:',s)
  else
    writeln('error fetching arguments:',fpgeterrno);
  // loads full path of running binary
  if kernproc_getpath(pid,s)-1 then   // -1 means own process
    writeln('arguments:',s)
  else
    writeln('error fetching path:',fpgeterrno);
end.

See the implementation of the above procedures in freebsd.pas for more info, note that the kern_proc_* values were also updated in sysctl.

Mattias Gaertner
to Marco van de Voort
Show quoted text 36 lines
In our previous episode, Mattias Gaertner said: > > I don't know if it exist a FPC function for that. > > > > i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk > > '{print $8}'". > > Thanks, I knew that. > I prefer a more direct way - for example via a sysctl. > > I need to check under BSD if a PID is running and what name it has. > Is there already a function for that? 2.5.1 since r17692 try this: uses freebsd,baseunix,sysutils; var pid : pid_t; s: ansistring; begin pid:=strtointdef(paramstr(1),-1); writeln('pid (-1 for current):',pid); // loads name of process + args with #0's inbetween. if kernproc_getargs(pid,s)-1 then // does not work with -1 writeln('arguments:',s) else writeln('error fetching arguments:',fpgeterrno); // loads full path of running binary if kernproc_getpath(pid,s)-1 then // -1 means own process writeln('arguments:',s) else writeln('error fetching path:',fpgeterrno); end. See the implementation of the above procedures in freebsd.pas for more info, note that the kern_proc_* values were also updated in sysctl.

Thanks a lot.

This solution is only for FreeBSD.
What about the other BSD flavors?
What about darwin?

Mattias