Run Bash Script in New Process Tree [duplicate] - linux

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.

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 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.

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