Linux path by pid

How to get process details from its PID

I have maintained a list of PIDs of processes currently running on my system (Linux). From this, now it would be great if I could get the process details from this PID. I have come over syscall.Getrusage() in Go, but I am not getting the desired results. What should I do?

for input 0x0 and 0x1 it is giving me some struct and i feel that this function can’t be used to get process details and is for exclusive use by the one who started a process the problem with below code By peterSO is when i am reading process details of each pids i am getting for few processes no such dir exists as it may not present as process might have been killed completed its task.

7 Answers 7

This might not be exactly what the asker wanted (there’s not much clear info on what type of details are required for each process id), but you can get some details of a task by its pid using the BASH command ps -p $PID (ps being short for process status)

With default options as ps -p $PID this returns:

  • PID: echos the process id
  • TTY: the name of the controlling terminal (if any)
  • TIME: how much CPU time the has process used since execution (e.g. 00:00:02)
  • CMD: the command that called the process (e.g. java )

More information about this process id can be shown using the -o options flag. For a list, see this documentation page.

Here’s one example that tells you a particular process PID’s full command with arguments, user, group and memory usage (note how the multiple -o flags each take a pair, and how the command outputs with lots of whitespace padding):

ps -p $PID -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS 

Tip: for human-read output in the console, make args the last option — it’ll usually be the longest and might get cut short otherwise.

Источник

How to get the path or name of the process from pid in linux

Solution 1: Use the (all processes) option and filter the result through : Solution 2: Just use , eg: Solution 3: try the following and see if you can discover the process as such This will give you all processes for all users, in a full-format listing where : axu = To see every process on the system using BSD syntax f = fullformat if the list is too long you can filter if you have an idea of the process name For example the command below will show you the pids for chrome. Question: hi i have used sys_getpid() from within kernel to get process id how can I find out process name from kernel struct?

Читайте также:  Linux cut пробел разделитель

How to get the path or name of the process from pid in linux

I have implemented a sigaction(SIGTSTP, &act, NULL) signal handler from which I am able to get the pid of the process which is sending the signal. And with this pid I am trying to get the path of that process using the command

but it is showing message

ls: /proc/23710/exe: No such file or directory 

Even I am executing the command «ps» the process is not listed. Can anybody tell me how to get the path or name of such process?

You can get the command line of the process from pid, you can read the /proc//cmdline :

 /proc/[pid]/cmdline This read-only file holds the complete command line for the process, unless the process is a zombie. In the latter case, there is nothing in this file: that is, a read on this file will return 0 characters. The command-line arguments appear in this file as a set of strings separated by null bytes ('\0'), with a further null byte after the last string. 

From that, you may get the path or name of the process.

Find Port Using PID in Linux, For Arch Linux. pacman -S netstat-nat. Once successfully installed, in Linux, enter the following command in the terminal: sudo netstat -ltnup. That’s how you will get a list as output where you can find the TCP port and note down the corresponding PID number. The flags used in the command used above are as follows:

How to find a PID of a process whose name I don’t know exactly?

I can get the PID of a specific process name by

but what if I don’t know the name of the process exactly? I can’t type something like

so is there any wildcard character, or is there another solution?

Use the -A (all processes) option and filter the result through grep :

$ pgrep -l sh 1821 sshd 2590 ssh-agent 2658 sh 2677 bash 3025 gvfsd-trash 14785 ksh93 17723 ksh93 

try the following and see if you can discover the process as such

This will give you all processes for all users, in a full-format listing

if the list is too long you can filter if you have an idea of the process name

For example the command below will show you the pids for chrome.

pidof$(ps -c |grep yor_pattern) 

Differences between PID, TID and PPID in Linux, The output lists all the PIDs with the specified process name. Alternatively, there is a tool called pgrep that provides advanced functionalities to retrieve PID information from the application name. To retrieve the PID of all the current system processes, commands like top are helpful. 5. Get All TID From a …

Get PID of a process as it opens

How do I get the pid of a process as soon as it opens. Like lets say we run ./file.pl and then ./file2.pl As both these files will create a pid in /proc/ folder. How do I instantly know if the process has been created when the executable is run. I have a file with all the commands ready to be run as soon as it gets the green signal that there is a new process in the /proc/ folder. How do I do that? EDIT: Please don’t answer with a shell command. I don’t need to know the pid. I need to develop a script which can know right away that we have a guest in the proc department

Читайте также:  Смена времени linux mint

If you start the process via a shell, then start process in background:

If the script give you the shell prompt back, you can do :

./your_prog pidof -x your_prog 

Tested OK with this perl script :

Every process can get its own pid with the getpid(2) syscall. At process creation by fork(2) the parent process (e.g. some shell) gets the pid of the new child process. Read e.g. Advanced Linux Programming for more. And the kernel (not the program) is creating some subdirectory /proc/1234/ see proc(5) as soon as it creates the process of pid 1234.

Actually, /proc/ is not a real file system. It is just a pseudo file system giving a view on the state of the kernel and the entire Linux system.

Perl gives you its POSIX module to interface the syscalls. The getpid() syscall is interfaced using the $PID or $$ Perl variable.

The /proc/ pseudo filesystem is filled by the kernel. You could perhaps use inotify to follow change in /proc/ but this is very probably a bad idea.

Your question is not clear, we cannot understand what you really want to do and what you have tried.

Try out below shell script.(You may have to include changes in below script for your expected output )

#!/bin/bash nr_proc_before=`ls -l /proc | wc -l` ls /proc > proc_list_before ./any_executable & nr_proc_after=`ls -l /proc | wc -l` ls /proc > proc_list_after nr_new=`expr $nr_proc_after - $nr_proc_before` echo "$nr_new processes are created newly" echo "new processes pids are :" diff proc_list_after proc_list_before > new_pids sed "1d" new_pids if [ nr_new > 0 ] ; then #trigger your file which has commands. fi 

Insted of any_execuatble you can replace with your things so that new processes will be created.

Note : This is not a script which monitors for new process. This sample of script may give you idea to solve your problem. Please do reply for this answer, i can redefine my answer.

Find PID of a Process by Name without Using popen() or, One way I could think of is to send a socket request to that process and ask to its PID. The other way is a little too complicated for a simple code I’m writing: to do what pidof command’s source code is actually doing (it uses a function call find_pid_by_name() but that’s doing a lot of things).

Linux get process name from pid within kernel

hi i have used sys_getpid() from within kernel to get process id how can I find out process name from kernel struct? does it exist in kernel??

struct task_struct contains a member called comm, it contains executable name excluding path .

Get current macro from this file will get you the name of the program that launched the current process (as in insmod / modprobe).

Using above info you can use get the name info.

Not sure, but find_task_by_pid_ns might be useful.

My kernel module loads with «modprobe -v my_module —allow-unsupported -o some-data» and I extract the «some-data» parameter. The following code gave me the entire command line, and here is how I parsed out the parameter of interest:

struct mm_struct *mm; unsigned char x, cmdlen; mm = get_task_mm(current); down_read(&mm->mmap_sem); cmdlen = mm->arg_end - mm->arg_start; for(x=0; xarg_start + x) == '-' && *(unsigned char *)(mm->arg_start + (x+1)) == 'o') < break; >> up_read(&mm->mmap_sem); if(x == cmdlen) < printk(KERN_ERR "inject: ERROR - no target specified\n"); return -EINVAL; >strcpy(target,(unsigned char *)(mm->arg_start + (x+3))); 

«target» holds the string after the -o parameter. You can compress this somewhat — the caller (in this case, modprobe) will be the first string in mm->arg_start — to suit your needs.

Читайте также:  Авто монтирование диск linux

you can look at the special files in /proc//

For example, /proc//exe is a symlink pointing to the actual binary.

/proc//cmdline is a null-delimited list of the command line, so the first word is the process name.

How to get only process ID in specify process name in, How to get only the process ID for a specified process name in Linux? ps -ef|grep java test 31372 31265 0 13:41 pts/1 00:00:00 grep java Based on the process id I will write some logic. So how do I get only the process id for a specific process name. Sample program:

Источник

How to check the path which has been a running process?

But this script is running from eg. /home/user/my/program/script.sh So, how I can get the full path of from where the script was running? I have many scripts which name is exactly same, but they are running from different locations and I need to know from where the given script was running. Thanks for reply!

4 Answers 4

for each in `pidof script.sh` do readlink /proc/$each/cwd done 

This will find the pid.s of all script.sh scripts running and find the corresponding cwd (current working directories) for /proc.

use pwdx usage: pwdx pid . (show process working directory) for example,

where 20102 is the pid this will show the process working directory of the process

#!/bin/bash #declare the associative array with PID as key and process directory as value declare -A dirr #This will get the pid of the script pid_proc=($(ps -eaf | grep "$1.sh" | grep -v "grep" | awk '')) for PID in $ do #using Debasish method dirr[$PID]=$(pwdx $PID) # Below are different process to get the CWD of running process # using user1984289 method #dirr[$PID]=$(readlink /proc/"$PID"/cwd) #dirr[$PID]=$(cd /proc/$PID/cwd; /bin/pwd) done # iterate using the keys of the associative and get the working directory for PID in "$" do echo "The script '$1.sh' with PID:'$PID' is in the directory '$'" done 

Use pgrep to get the PIDs of your instances, and then read the link of the associated CWD directory. Basically, the same approach as @user1984289 but using pgrep instead of pidof which does not match bash script names on my system (even with the -x option):

for pid in $(pgrep -f foo.sh); do readlink /proc/$pid/cwd; done 

Just change foo.sh to the name of your script.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector