How do I find the process ID of a program with a particular name from inside a shell script or C program?
In a shell script: There is no utility specifically designed to map between program names and process IDs. Furthermore, such mappings are often unreliable, since it’s possible for more than one process to have the same name, and since it’s possible for a process to change its name once it starts running. However, a pipeline like this can often be used to get a list of processes (owned by you) with a particular name: ps ux | awk ‘/name/ && !/awk/ {print $2}’ You replace “name” with the name of the process for which you are searching. The general idea is to parse the output of ps, using awk or grep or other utilities, to search for the lines with the specified name on them, and print the PID’s for those lines. Note that the “!/awk/” above prevents the awk process for being listed. You may have to change the arguments to ps, depending on what kind of Unix you are using. In a C program: Just as there is no utility specifically designed to map between program names and process IDs, there ar
In a shell script: There is no utility specifically designed to map between program names and process IDs. Furthermore, such mappings are often unreliable, since it’s possible for more than one process to have the same name, and since it’s possible for a process to change its name once it starts running. However, a pipeline like this can often be used to get a list of processes (owned by you) with a particular name: ps ux | awk ‘/name/ && !/awk/ {print $2}’ You replace “name” with the name of the process for which you are searching. The general idea is to parse the output of ps, using awk or grep or other utilities, to search for the lines with the specified name on them, and print the PID’s for those lines. Note that the “!/awk/” above prevents the awk process for being listed. You may have to change the arguments to ps, depending on what kind of Unix you are using. In a C program: Just as there is no utility specifically designed to map between program names and process IDs, there ar
Related Questions
- How do I {set an environment variable, change directory} inside a program or shell script and have that change affect my current shell?
- How do I find the process ID of a program with a particular name from inside a shell script or C program?
- What are the rules for recording a particular PBS program to use in schools?