How to find which pid is modify the file - pid

I want to modify a sas file. But the liunx system told me that it is readonly and it had been used by other program. So, I want to find which PID is using this file and try to kill the PID. How could I find this PID, please?

You can use some commands to verify the process in execution to kill. A tool that I like to use is the pgrep, where i search for the name of the application and it returns me the pid.
Ex:
pgrep firefox
Output:
2588
Or, you can use other commons tools:
top, ps.

Related

I donĀ“t know how i can get the pid of an process by process name

I want to get the process id for example of the process "notepad.exe".
I only found threads etc. for how to get process by PID. But I want to get the PID by process name (in this scenario "notepad.exe").
And this with my kernel driver.
There can be more than one process with a process image coming from a file notepad.exe. You must enumerate all the running processes and retrieve the module name from that. It's described in the MSDN: https://learn.microsoft.com/en-us/windows/desktop/psapi/enumerating-all-processes
From the list of processes you then extract all the processes with a module name notepad.exe and get the list of matching PIDs.

What are the ways to find the session leader or the controlling TTY of a process group in Linux?

This is not a language specific question, although I am using golang at the moment.
I am writing a command line program, and I wanted to find the real UID of the program.(By realUID, I meant, if the user did a sudo, the effective uid changes, but the real uid would be the same as the user's.)
I've read that finding the owner of the controlling tty is one way to find this, and on linux, we can use "tty" command, that will return the filename of the terminal connected to STDINPUT. Checking its ownership is one way.
Another way is to find the session leader process, and who owns it.
I tried the first way, using
cmdOut []byte
cmdOut, _ = exec.Command("tty").Output()
but it returns the output not a tty, when I run the program from my shell. Chances are that this might be getting executed in a separate forked shell that is detached from a tty (again, just a wild guess).
I tried the second way using os.Getppid() to get the parent pid, but in reality, when running sudo, it forks again, and it is giving the parent pid of the sudo process(16031 in the below case, whereas I am looking to grab 3393 instead.). (Pasting the process hierarchy from pstree output)
/usr/bin/termin(3383)-+-bash(3393)---sudo(16031)---Myprogram(16032), so effectively I am not able to get the session leader process, but just the parent pid.
Can someone guide me on how do I implement this functionality using either of this method?
Edit:
sudo set's $SUDO_USER environment variable, but it will help just with one sudo, i.e. if there was something like sudo sudo -u nobody your-program, $SUDO_USER will be set to "root". And there is $SUDO_UID too.
Old answer: How about exec.Command("who am i").Output() ? (won't work, still needs a tty).

How to get the pid if i only know process name in linux using proc

I am going to make program to get process informaition(pid, process name, virtual memory, physical memory ) using procfs.
and i want print like this
PID Name Virtual physical shared
1 init 1234 123 22
33 firefox 33 33 2
I think I can get the process information through proc/[pid]/stat
I think I should know the pid if i want to get process information.
but the problem is how can can I get the pid only given the process name.
because process pid is changed after reboot or after terminated and restart.
so my question is
Is there a way to get the pid if I only know process name? not using shell script
is there file in proc folder that contains all of processse running now?
suppose that I only know process name( ex) firefox), I want to get pid using proc and proc filesystem
If you can't use a tool like pgrep you could look in all the /proc/<pid> directories and looking at the exe link in each to find the one that points to the executable you want. Or you could look at cmdline in each if that helps instead.

Why I can't see my process which is created fork function in linux top command

I have a C program which creates processes with fork() and prints pid of them with get_pid(). While this program is running, I use "top" command to see same processes, but I can't see them in there. Why? and how I can see my processes with "top" command?
top sorts according to a field, by default this is cpu time
your C program is probably too small and efficient to show up in the list
to see similar metrics to top but for a specific pid use ps, for example
ps -lp 12188
I'm with #Vorsprung. You may also note that you can use top with a pid argument:
top -p PID

How can I programmatically get the list of open file descriptors for a given PID on OS X?

Everything I've seen says to use lsof -p, but I'm looking for something that doesn't require a fork/exec.
For example on Linux one can simply walk /proc/{pid}/fd.
You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process. You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path.

Resources