I know how to run one cron job after another from this: https://askubuntu.com/questions/923260/cron-run-a-script-after-the-other
But, if I have jobs A, B, and C, how do I make job C wait for BOTH A and B to be finished? Either A or B could finish first, and C cannot start running until both A and B have finished.
I would just put the entire thing into a script and use standard process management, something like:
#!/bin/env bash
jobA & # start A in the background.
jobB # start B in the foreground, A and B running.
wait # B now finished, wait for A if necessary.
jobC # A and B now finished, do C.
Related
I want to script an alarm to be triggered when any of crucial to me processes enters a D state (uninterruptible sleep).
In order to test it, I want to have a dummy script to enter D state and trigger the alarm.
How that can be achieved? Maybe with some one-liner?
In line x in script A, I want to start running script B, which takes 10 seconds. However, I do not want line x+1 to be executed 10 seconds after line x. Is there a way to fix this?
Script B is simply an independent series of command sent to another external device. Script A is used to monitor that device. Script B does not have any return, and code after line x in script A does not rely on script B.
Overall, I want to trigger the start of Script B and let it run independent of Script A. While script A is kept running continuously.
Thanks to the comment, I found subprocess.Popen() can do the work.
Actually, Script B can be simplified into one single function (Function B). In this case, shall I create one Python script as Script B that only calls Function B, and use subprocess.Popen() to call Script B in Script A?
Or, is there a better way to call Function B, rather than Script B, in a similar fashion as in subprocess.Popen()?
I am trying to call Function B directly because my task is very time-dependent, and half a second of delay may be significant. I have measured the delay from the line x-1 in Script A to line 1 in Script B, if I call Script B in Script A. The delay is ~450 ms. I suspect the delay is from the time for the interpreter to compile Script B and execute it, even though Script B is one or two lines long.
Thank you very much!
How to run two scripts at the same time:
Use the GNU parallel ( sudo apt install parallel if not already installed on your system )
Alan#Enigma:~$ parallel python ::: TheScript_A TheScript_B [ TheScript_C [ ... ]]
This way is way cheaper, than trying to orchestrate process-spawns from inside the first python session. It is possible, yet the processing costs and latency side-effects and software engineering costs are way higher, than using the smart O/S services present right for this simple problem-definition.
Reading man parallel you get all the smart bash-scripting options to add for flexible, parametrised process-options, as one may need and wish
...
total_jobs() number of jobs in total
slot() slot number of job
seq() sequence number of job
...
I have two commands, one is logging things in the background and won't stop until I kill it and the second one will eventually stop.
Let's mark them A and B respectively.
I want to execute:
A and B in parallel
Wait for B to finish
Kill A
[do some more stuff]
and repeat that in a loop.
I'm running on MacOS and can't update to Bash 4.x because of GPLv3.
Preferably A (the logger) would start first but I wouldn't mind if it's undefined or B would start first since the difference in time is negligible.
Help would be appreciated.
Thanks
I have two services A and B which I want to start on boot. But A should start first and then only B should start.
I enabled the services using systemctl enable service_name.
Now the services are starting but not in order i.e B is starting before A. Is there any way I can configure their start order?
You can add the following command at the end of the startup script of A, and disable B to be started on bootup: systemctl start B
They're starting out of order because Linux uses "makefile style concurrent boot" during startup -- and the A process is taking longer to start than the B process. The simplest way to delay process B is with a sleep command -- a few seconds is likely enough -- though this will delay the completion of startup by a fixed amount (and, if process A takes a variable time to start, as with opening a wifi connection etc., this may not always work unless you set the time higher than it usually needs to be).
More reliable, and possibly less delay in startup, would be to use something like lsproc | grep proc_a | wc -l to check for existence of process A (or a child of A) as a condition for starting process B -- put this in a short loop with a 1 or 2 second sleep (so it doesn't hog all your CPU while it waits) and it'll effectively keep B back until A is running, without unnecessary delay.
Is there any kind of a program or a script that I can manually queue processes of applications on Ubuntu? For example there are 40 processes running at a specific point of time and 10 more are about to run in some time after.Is there anyway I can tell the system to run for example the 3 out of 10 at the same time and after they complete to run the 7 remaining one at a time in a specific order?
You could achieve that result with a job-aware shell (zsh,bash).
For instance in bash:
# run first 3 apps in background and in parallel
app1 &
app2 &
app3 &
# wait for all background jobs to finish
wait
# run the remaining apps in specified order
app4
app5
...
The & means run the program in the background (i.e. you get another shell prompt the moment the program is started). All backgrounded jobs run in parallel. However, backgrounded jobs cannot access the standard input (i.e. you can't provide keyboard input to them - well, you can, by bringing to the foreground first, but that's another story).