Rtcwake run script after waking up - bash

I want to run script after executing and waking up from rtcwake command. Next commands are run procedurally but I want to have other part of script running only after fully execution command. How to achieve it avoiding hacks like sleep? Sometimes it takes a few seconds for typing password and when I do it in more than 10 seconds it runs already script after rtcwake.
sudo rtcwake -m mem -t $DESIRED &
sleep 10
echo "Hello again $USER!"

Related

How can I prevent idle sleep in a long-running bash script on Mac OS?

I have a bash script that executes several long-running commands on Mac OS. How can I prevent the computer from going to sleep while the script is running?
I found that I can achieve the desired behavior by adding this line to the start of the script:
caffeinate -i -w $$ &
How it works:
caffeinate is a built-in Mac OS utility for creating power management assertions to prevent sleep.
The -i flag tells caffeinate to prevent idle sleep.
The -w flag tells caffeinate to wait until the specified process finishes.
$$ is a shell variable containing the current PID. Together with the -w flag this tells caffeinate to prevent sleep for the duration of the shell script.
The final & runs the command in the background so the script can continue running.

Issue unable to execute all bash script within bash script

I have following bash script which runs two process in parallel (two bash scripts internally), I need two bash scripts two run in parallel once both are finished I need total time execution. But the issue is the first bash script ./cloud.sh doesn't run but when I run it individually it runs successfully, and I am running main test bash script with sudo rights.
Test
#!/bin/bash
start=$(date +%s%3N)
./cloud.sh &
./client.sh &
end=$(date +%s%3N)
echo "Time: $((duration=end-start))ms."
Client.sh
#!/bin/bash
sudo docker build -t testing .'
Cloud.sh
#!/bin/bash
start=$(date +%s%3N)
ssh kmaster#192.168.101.238 'docker build -t testing .'
end=$(date +%s%3N)
echo "cloud: $((duration=end-start)) ms"
A background process won't be able to get keyboard input from you. As soon as it tries so, it will receive a SIGTTIN signal which will stop it (until it is brought back to the foreground).
I suspect that one or both of your scripts asks you to enter something, typically a password.
Solution 1: configure sudo and ssh in order to make them password-less. With ssh this is easy (ssh key), with sudo this is a security risk. If docker build needs you to enter something, you are doomed.
Solution 2: make only the ssh script (Cloud.sh) password-less and keep the sudo script (Client.sh) in foreground. Here again, if the remote docker build needs you to enter something, this won't work.
How to wait for your background processes? Just use the wait builtin (help wait).
An example with solution 2:
#!/bin/bash
start=$(date +%s%3N)
./cloud.sh &
./client.sh
wait
end=$(date +%s%3N)
echo "Time: $((duration=end-start))ms."

Windows bash script to run something from WSL

I am trying to write down a Windows bash script to:
Start Windows Subsystem for Linux (WSL) and
cd within WSL to "../myfolder/"
run ./foo first_parameter second_parameter
Wait until finished and exit WSL
cd within Windows to "../myWinFolder/"
run Foo.exe parameter
wait until finished
This is my attempt:
bash -c "cd ../myFolder/ && ./foo first_parameter second_parameter"
cd ..
cd myWinFolder
START /WAIT Foo.exe parameter
But sadly CMD does not wait for WSL to finish before running the EXE.
Anything I can do?
I'm happy that the interop between dos and WSL does in fact wait for commands to finish. Try the following:
In a file called runit.bat
echo bat01
bash -c "echo bat02; cd ./bash/; ./runit.sh; echo bat03"
echo bat04
In a sub-folder called ./bash/ paste the following in a file called runit.sh
echo sh01
sleep 2s
echo sh02
When you run runit.bat from within dos you will see a wait of 2 seconds
You have not specified what is inside your ./foo script. I suspect that it is running a task in the background or running something that returns immediately. This can be simulated by putting & after the sleep so that it runs in the background within wsl sleep 2s &. When you do this you see that there is no pause in the execution of the script.
So I would check ./foo maybe add some echo debug statements around inside it and run it from within WSL first to make sure that it does indeed wait until all the commands are finished before it exits.

Shell script not waiting

ssh user#myserver.com<<EOF
cd ../../my/path/
sh runscript.sh
wait
cd ../../temp/path
sh secondscript.sh
EOF
The first script runs and asks me the questions in that script, but before i'm even able to start typing to answer them the second script starts running. From what I'm reading this shouldn't be happening even without the wait.

Running processes simultaneously, Bash

I would like to run n processes (in my case simulations) simultaneously, using bash.
Right now this is what I'm running:
for file in $ini/SAN*.ini;
do
echo "Running $file...";
temp=$(basename $file .ini)
mosrun -G opp_run -r 0 -u Cmdenv -n ..:../../src -l ../../src/inet SAN.ini > $outputs/$temp.out;
done
Problem is, the loop only progresses to the next iteration after the simulation is done. Any suggestions? Thanks!
You should be able to run your command in the background by adding a & after it.
Should make them run in parallell, although in the background.
(Small side note: the processes will continue to run even if you abort the script, so you might want to add a trap to kill the processes if you hit for eg. ctrl-c when script is running. Look at bash manual.)

Resources