Multitasking in a bashscript [duplicate] - linux

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 &

Related

How to acquire a lock file in Linux Bash [duplicate]

This question already has an answer here:
Linux flock, how to "just" lock a file?
(1 answer)
Closed 1 year ago.
I have an existing lock file sometimes used by other processes. I want to temporarily acquire this lock file so other programs that potentially use it have to wait for me to unlock. Then I want to run a few commands, and then unlock it. How do I do this? I thought this would be easy but for some reason I cannot figure it out at all. I understand that I would most likely need to use flock for this, but what arguments should I be using in this scenario? flock seems to always need a command or second file to work, however in this situation there doesn't seem to be one.
Context: A bash script I am using is running into race conditions around a particular lock file (/var/lib/apt/lists/lock to be specific), and to test my solution I want to reliably be able to lock this file so I can check if my changes to the script work.
You have an example in the flock (1) man page. You should execute the commands in a subshell:
(
flock -n 9 || exit 1
...
) 9>/var/lib/apt/lists/lock
Using this form the lock is released when the subshell exits.

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.

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.

Bash scripting with skeleton in Debian [duplicate]

This question already has answers here:
Cron jobs -- to run every 5 seconds
(6 answers)
Closed 8 years ago.
Before of first, I'm a Linux (administrator|developer) newbie.
I need to run a bash script every 5 seconds, it's very simple; export service's information to text files.
I try to do this with cron daemon, but it's run every minute at least.
I'm discover Skeleton script and have many questions about this:
I need write some special code in my bash file?
How to run every 5 seconds?
There are a best practices manual?
Yes its not possible through cron as daemon runs once in every minute. Or when job list is modified
Put whatever you want to run in a script inside infinite while loop and put sleep of 1 sec
something like
while [ 1 ]
do
run_your_cmds here
sleep 1
done
BUT I dont think anything need that kind of monitoring.
Best Practice!!
Please dont try and do it with cron.

Resources