Linux start process and get pid

How can a Linux/Unix Bash script get its own PID?

I have a script in Bash called Script.sh that needs to know its own PID. In other words, I need to get PID inside Script.sh . Any idea how to do this?

7 Answers 7

The variable $$ contains the PID.

See the [manual][1] for more information, including differences between the two.

  • $$ Expands to the process ID of the shell.
    • In a () subshell, it expands to the process ID of the invoking shell, not the subshell.
    • In a () subshell, it expands to the process ID of the subshell [1]: http://www.gnu.org/software/bash/manual/bashref.html#Bash-Variables

    In addition to the example given in the Advanced Bash Scripting Guide referenced by Jefromi, these examples show how pipes create subshells:

    $ echo $$ $BASHPID | cat - 11656 31528 $ echo $$ $BASHPID 11656 11656 $ echo $$ | while read line; do echo $line $$ $BASHPID; done 11656 11656 31497 $ while read line; do echo $line $$ $BASHPID; done  

    It redirects a string into the loop (or anything that reads stdin ). The string is referred to as a "here string".

    Example: kill -9 $$ will kill the shell instance it is called from.

    kill -9 (with -9 flag) is considered to be harmful and only to be used if it is absolutely necessary).

    It's considered "dangerous" because the process does not get a chance to respond to the signal (and possibly clean up after itself). Doing kill -9 $$ does exactly 1 thing. It kills the current shell process. This is useful if you have done something in the shell session that you do not want written to .bash_history Like: docker run -e PASSWORD=hunter2 ircbot

    You can use the $$ variable.

    If the process is a child process and $BASHPID is not set, it is possible to query the ppid of a created child process of the running process. It might be a bit ugly, but it works. Example:

    sleep 1 & mypid=$(ps -o ppid= -p "$!") 

    Wherever you are (on an interactive command line or in a script), and in the case you do NOT have access to $BASHPID, you can get access to your current shell pid with this :

    where simple quotes are essential to prevent premature string interpretation (so as to be sure the interpretation occurs in the child process, not in the current one). The principle is just to create a child process and ask for its parent pid, that is to say "myself". This code is simpler than ps-based Joakim solution.

    Источник

    Getting PID of process in Shell Script

    I am writing one shell script and I want to get PID of one process with name as "ABCD". What i did was :

    process_id=`/bin/ps -fu $USER|grep "ABCD"|awk ''` 

    This gets PID of two processes i.e. of process ABCD and the GREP command itself what if I don't want to get PID of GREP executed and I want PID only of ABCD process? Please suggest.

    8 Answers 8

    Just grep away grep itself!

    process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk ''` 

    Thanks for quick Answer.It worked.Also what if i dont want PID of script which is executing this. Because my script also contains ABCD

    Surely you can think of something to "grep away" again, as the PID of your script, which should be available via the $$ variable

    A trick for avoiding the 2nd grep is to give grep a regular expression that matches the same thing, but appears different on the command line: . | grep "[A]BCD" | awk . .

    Have you tried to use pidof ABCD ?

    The pidof command does not behave the same on every Linux distribution. So using pidof is not a good solution. It is also necessary to distinguish between the version of ps included in BusyBox - in lightweight Linux distributions (for example in a set-top box) and another ps , which is designed for full-fledged Linux. In the BusyBox version of the ps command, almost no arguments / switches work. In the case of BusyBox, the ps command is used a little differently. You have to be careful.

    It's very straight forward. ABCD should be replaced by your process name.

    #!/bin/bash processId=$(ps -ef | grep 'ABCD' | grep -v 'grep' | awk '< printf $2 >') echo $processId 

    Sometimes you need to replace ABCD by software name. Example - if you run a java program like java -jar TestJar.jar & then you need to replace ABCD by TestJar.jar.

    Источник

    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

    The /proc/ directory (not folder) is filled by the kernel. Every running process has some subdirectory in it (and programs don't need to do anything particular to get that created). Your question is not clear.

    Please edit your question to explain really what you want to do, and show some code that you tried. Your question is unclear.

    Please also explain the context, and why you want to do all this (and in which language you are coding, also show the code you have tried). There might be a simpler way to achieve your goal.

    4 Answers 4

    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 :

    Hello @sputnick. Thanks for the response but I am looking for a script which does that. I don't want to run the file. I mean the file has more processes in it. I just want to get the pid asap

    If you don't have the prompt back immediately, then you can run pidof -x your_prog in another pseudo-terminal

    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.

    Источник

    How to get pid of just started process

    Is there any simpler solution? I would be happy to execute myCommand and get its PID as a result of one line command.

    8 Answers 8

    What can be simpler than echo $! ? As one line:

    in a bash script, in a loop which starts programs, $! is not accurate. Sometimes it returns pid of the script itself, sometimes of grep or awk run from the script any solution to specifically get the pid of the process just launched in this scenario? something like pid= myprogram would have been awesome

    NB that this requires you to start the command using a & as the previous line, otherwise the echo basically returns blank.

    This doesn't work if you don't explicitly background the process! For instance, your process may background itself after it has done something you need to wait for before continuing.

    You can use sh -c and exec to get the command's PID even before it runs.

    To start myCommand , so that its PID is printed before it begins to run, you can use:

    sh -c 'echo $$; exec myCommand' 

    How it works:

    This starts a new shell, prints the PID of that shell, and then uses the exec builtin to replace the shell with your command, ensuring it has the same PID. When your shell runs a command with the exec builtin, your shell is actually becoming that command, rather than the more common behavior of forking a new copy of itself, which has its own separate PID and which then becomes the command.

    I find this to be much simpler than alternatives involving asynchronous execution (with & ), job control, or searching with ps . Those approaches are fine, but unless you have a specific reason to use them--for example, perhaps the command is already running, in which case searching for its PID or using job control would make sense--I suggest considering this way first. (And I would certainly not consider writing a complex script or other program to achieve this).

    This answer includes an example of this technique.

    Parts of that command could occasionally be omitted, but not usually.

    Even if the shell you're using is a Bourne-style and thus supports the exec builtin with these semantics, you generally shouldn't try to avoid using sh -c (or equivalent) to create a new, separate shell process for this purpose, because:

    • Once the shell has become myCommand , there's no shell waiting to run subsequent commands. sh -c 'echo $$; exec myCommand; foo would not be able to attempt to run foo after replacing itself with myCommand . Unless you're writing a script that runs this as its last command, you can't just use echo $$; exec myCommand in a shell where you are running other commands.
    • You cannot use a subshell for this. (echo $$; exec myCommand) may be syntactically nicer than sh -c 'echo $$; exec myCommand' , but when you run $$ inside ( ) , it gives the PID of the parent shell, not of the subshell itself. But it is the subshell's PID that will be the PID of the new command. Some shells provide their own non-portable mechanisms for finding the subshell's PID, which you could use for this. In particular, in Bash 4, (echo $BASHPID; exec myCommand) does work.

    Finally, note that some shells will perform an optimization where they run a command as if by exec (i.e., they forgo forking first) when it is known that the shell will not need to do anything afterward. Some shells try to do this anytime it is the last command to be run, while others will only do it when there are no other commands before or after the command, and others will not do it at all. The effect is that if your forget to write exec and just use sh -c 'echo $$; myCommand' then it will sometimes give you the right PID on some systems with some shells. I recommend against ever relying on such behavior, and instead always including exec when that's what you need.

    Источник

    How to find the Process ID (PID) of a running terminal program?

    I am running a program in the terminal that I can't escape with Ctrl - C and that I want to kill. How can I find its PID?

    This is a branch of What should I do when Ubuntu freezes? as a reference to prevent details in that question from becoming too technical.

    11 Answers 11

    Open another terminal and run ps ax | grep foo where foo is the name of the unresponsive program. This should return a line of output that looks something like this:

    $ ps ax | grep firefox 2222 ? S 0:00 /bin/sh /usr/lib/firefox-3.6.9/firefox 2231 ? Sl 514:36 /usr/lib/firefox-3.6.9/firefox-bin 30290 pts/2 S+ 0:00 grep --color=auto firefox 

    The first field of each line of output is a number which represents the Process ID of the program matched by grep (you can safely ignore the last one, which represents grep itself.

    To halt the offending process, do: kill pid where pid is the Process ID of the program. You might have to use your judgment as to which of the matches needs to be kill ed, or you could use top instead. Using kill by itself sends SIGTERM, which you should try first as it allows the program to properly clean up after itself. If SIGTERM fails, try SIGHUP, which is stonger medicine: kill -HUP pid . If all else fails, send SIGKILL. But, you should only do so as a last resort, because SIGKILL causes the kernel to terminate the process immediately with no possibility for cleanup. This can at times result in data corruption or other problems. So again, only send SIGKILL as a last resort. To do so, do kill -KILL pid or kill -9 pid .

    If you are running a graphical interface, of course, you don't have to fool with this crazy command-line stuff to get the job done. Just open "System Monitor", navigate to the Processes tab, choose the process you want to halt (Hm, could it be the one using 90% CPU?) and right-click it. Since the process is already stopped, (that's the problem, right?) choose End Process or Kill Process from the resulting menu.

    Источник

    Читайте также:  Linux get all user list
Оцените статью
Adblock
detector