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

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.

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.

Run Bash Script in New Process Tree [duplicate]

This question already has answers here:
How can I launch a new process that is NOT a child of the original process?
(5 answers)
Closed 5 years ago.
I am working with a complex bash script that performs various operations and the restarts the Linux (CentOS 6) server on which it was run. This script is invoked from a couple of different places. I am looking for a way to initiate execution of this complex bash script in a new process tree.
I put together the following text diagram to illustrate the scenario:
a_process_that_calls_script
\_ subshells/processes/commands_of_calling_process
...
bash_script
\_ subshells/commands/other_scripts_called
Potential duplicate: How can I launch a new process that is NOT a child of the original process?
If you have a process invoke the script as a grandchild process, and then the child exits, the grandchild will become a child of the init process.

Multitasking in a bashscript [duplicate]

This question already has answers here:
Multithreading in Bash [duplicate]
(3 answers)
Closed 9 years ago.
I am basically trying to write a bash script that suspends some virtual machines running on a host. However, if I write the script sequentially, VMs will be suspended one at a time. Suspending a VM takes some time to save state. How can I let my script suspend the VMs concurrently. In other words, how can I run commands concurrently in a bash script instead of sequentially?
You can background the tasks.
some bash command with options and stuff then with a &
Adding the & will send the command to the background and begin the next.
Put a & after the command for suspending the the VM.
For example if
cmd_to_suspend_vm
was your command to run. You would run
cmd_to_suspend_vm &

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

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.

Resources