What does "$$" means in shell script? [duplicate] - linux

This question already has answers here:
What does $$ mean in the shell?
(12 answers)
Closed 8 years ago.
I came across "$$" expression in shell script, something like this
TFILE=$$
Can anyone tell me its meaning?

$$ means the process ID of the currently-running process.

It's the process id of the current process - see a previous question on StackOverflow for details: What does $$ mean in the shell?

$$ is the process id of the currently running process in UNIX.
mostly it is used with naming of logfiles aor temp files, such that there is no conflict of file names while multiple instances of the same scripts are running.

echo "$$" prints PID of current running process.

Related

Is there anyway to find the PID of parent process without using getppid? [duplicate]

This question already has answers here:
Programmatically get parent pid of another process?
(7 answers)
Closed 5 months ago.
I know that the PCB is a data structure which includes parent
process id(pPID), process id(PID), pointers, and etc. Is there anyway
to find the PID of parent process without using getppid function?
The fields of /proc/self/stat (as documented in proc(5)) include PPID. Take care in parsing as comm may contain spaces and other unusual characters.
(But I second #JohnZwinck's comment. Why?)

Execute a command when a bash script is not responding? [duplicate]

This question already has answers here:
How to kill a child process after a given timeout in Bash?
(9 answers)
simple timeout on I/O for command for linux
(3 answers)
Closed 5 years ago.
Here's my situation: I've made a script in a while loop, but sometimes (say after 20-30 loops) it stops unexpectedly.
I tried to debug it but I couldn't.
I noticed that it stops while executing a command, and it just doesn't do anything when it stops. Now I was thinking: is there a way to tell to another script when the first script stops and it doesn't execute any command in, say 120 seconds? Maybe by constantly observing the output of the first script and when it's giving no output, the second script kills the first one and makes it start again? Sorry for my bad English hope I was clear.

How to know the process id of current bash session? [duplicate]

This question already has answers here:
How to determine the current interactive shell that I'm in (command-line)
(28 answers)
Why is $$ returning the same id as the parent process?
(8 answers)
Closed 6 years ago.
I'm on a linux server and admin user. There're many admin users using this machine at the same time. So under current bash command line, how to know the current process id of the bash process I'm using?
Thanks!
You can use echo $$ to get the PID of the current Bash shell you are using.

How come pid files are used when pids can clash with an unrelated process? [duplicate]

This question already has answers here:
Ensure a single instance of an application in Linux
(12 answers)
Closed 8 years ago.
It seems that a common way of running only one copy of a process is to write a pid to a file and then on start check whether a process with that pid exists. I imagine OS is trying not to reuse a pid quickly after a process has crashed, but since the number of pids is limited, sooner or later there is going to be another unrelated process using that pid. And the original one won't start. How can this situation be avoided?
Typically you would also check for the process name. For example the following will return 1 if there is a process called fork-server running on PID 10616:
ps -xp 10616 | grep fork-server | wc -l
It is of course still possible to have a collision, but the likelihood should be much less.

:(){ :|:& };: Forkbomb? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
The following bash command will spawn processes to kernel death. Can you explain the syntax?
:(){ :|:& };:
running this fork() unlimited .. Can one please explain this bash script ?
You define a function called :. Inside this function you call this function twice (:|:) and then you send that process to the background (&). Then you finally call it at the end.
Because of the recursive nature, you'll keep forking. Since there is no base case, the recursion will never end.

Resources