Running gnome-terminal in a loop doesn't block waiting for programs to finish - bash

I basically need to automate running 2 commands in separate terminals.
while :
do
timeout 10 gnome-terminal --geometry=95x56 -e "COMMAND1" &&
timeout 7 gnome-terminal -e "COMMAND2" &&
sleep 30
done
Expected behavior:
A terminal opens, running COMMAND1 for 10 seconds, then closes
A second terminal opens, runs COMMAND2 for 7 seconds, then closes
30 seconds pass
The cycle repeats
Actual behavior:
Both COMMAND1 and COMMAND2 start at the same time
COMMAND1 is displayed in the terminal, but does not actually run.
What's going on here?

Self-answer moved from the question into a Community Wiki answer, per What is the appropriate action when the answer to a question is added to the question itself?:
The following, as intended, lets only the first command stay open, while running the second in a loop:
COMMAND1 &
while :
do
sleep 15
gnome-terminal -- timeout 7 COMMAND2 &&
sleep 30
done

Related

Bash - kill a command after a certain time [duplicate]

This question already has answers here:
Timeout a command in bash without unnecessary delay
(24 answers)
Closed 1 year ago.
In my bash script I run a command that activates a script. I repeat this command many times in a for loop and as such want to wait until the script is finished before running it again. My bash script is as follows
for k in $(seq 1 5)
do
sed_param='s/mu = .*/mu = '${mu}';/'
sed -i "$sed_param" brusselator.c
make brusselator.tst &
done
As far as I know the & at the end lets the script know to wait until the command is finished, but this isn't working. Is there some other way?
Furthermore, sometimes the command can take very very long, in this case I would maximally want to wait 5 seconds. But if the command is done earlier I would't want to wait 5 seconds. Is there some way to achieve this?
There is the timeout command. You would use it like
timeout -k 5 make brusselator.tst
Maybe you would like to see also if it exited successfully, failed or was killed because it timed out.
timeout -k 5 make brusselator.tst && echo OK || echo Failed, status $?
If the command times out, and --preserve-status is not set, then command exits with status 124. Different status would mean that make failed for different reason before timing out.

Shell script: How to loop run two programs?

I'm running an Ubuntu server to mine crypto. It's not a very stable coin yet and their main node gets disconnected sometimes. When this happens it crashes the program through fatal error.
At first I wrote a loop script so it would keep running after a crash and just try again after 15 seconds:
while true;
do ./miner <somecodetoconfiguretheminer> &&break;
sleep 15
done;
This works, but is inefficient. Sometimes the loop will keep running for 30 minutes until the main node is back up - which costs me 30 minutes of hashing power unused. So I want it to run a second miner for 15 minutes to mine another coin, then check the first miner again if its working yet.
So basically: Start -> Mine coin 1 -> if crash -> Mine coin 2 for 15 minutes -> go to Start
I tried the script below but the server just becomes unresponsive once the first miner disconnects:
while true;
do ./miner1 <somecodetoconfiguretheminer> &&break;
timeout 900 ./miner2
sleep 15
done;
Ive read through several topics / questions on how &&break works, timeout works and how while true works but I can't figure out what I'm missing here.
Thanks in advance for the help!
A much simpler solution would be to run both of the programs all the time, and lower the priority of the less-preferred one. On Linux and similar systems, that is:
nice -10 ./miner2loop.sh &
./miner1loop.sh
Then the scripts can be similar to your first one.
Okay, so after trial and error - and some help - I found out that there is nothing wrong with my initial code. Timeout appears to behave differently on my linux instance when used in terminal than in a bash script. If used in Terminal it behaves as it should, it counts down and then kills the process it started. If used in bash however - it acts as if I typed 'sleep' and then after counting down stops.
Apparently this has to do with my Ubuntu instance (running on a VPS). Even though I installed latest versions of coreutils, have all the latest versions installed through apt-get update etc. This is the case for me on Digital Ocean as well as Google Compute.
The solution is to use the Timeout code as a function within the bash script, as found on another thread in stackoverflow. I named the function timeout2 as to not confuse the system in triggering the not properly working timeout command:
#!/bin/bash
# Executes command with a timeout
# Params:
# $1 timeout in seconds
# $2 command
# Returns 1 if timed out 0 otherwise
timeout2() {
time=$1
# start the command in a subshell to avoid problem with pipes
# (spawn accepts one command)
command="/bin/sh -c "$2""
expect -c "set echo "-noecho"; set timeout $time; spawn -noecho
$command; expect timeout { exit 1 } eof { exit 0 }"
if [ $? = 1 ] ; then
echo "Timeout after ${time} seconds"
fi
}
while true;
do
./miner1 <parameters for miner> && break;
sleep 5
timeout2 300 ./miner2 <parameters for miner>
done;

Repeat unterminated command every x interval of time

I'm trying to run a command after amount of time. I found these solutions:
watch -n60 command
while true; do command; sleep 60; done
They are working good if the command terminates (for example: echo "message")
The code which I'm running doesn't terminate. That's why those solutions are not working for me. But I want to run it, terminate it after 60 seconds and run it again. How can I do that?
Use the timeout command
while true; do timeout 60 command; done
Note that if the command exits before the 60 seconds are up, it will re-execute immediately rather than waiting for the minute to be up.
This starts the command every 60 seconds and kills the process if it is unterminated:
while true; do command &; LAST_PID=$!; sleep 60; kill -9 $LAST_PID; done

Need to write a script that runs two scripts, but needs to stop the first one before the 2nd runs [duplicate]

This question already has answers here:
Timeout a command in bash without unnecessary delay
(24 answers)
Closed 6 years ago.
This is a CentOS 6.x box, on it I have two things that I need to run one right after the other - a shell script and a .sql script.
I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours, and then calls the .sql script (they can't run simultaneously).
I'm unsure how to do the middle part, that is terminating the first script after a certain time limit, any suggestions?
script.sh &
sleep 4h && kill $!
script.sql
This will wait 4 hours then kill the first script and run the second. It always waits 4 hours, even if the script exits early.
If you want to move on immediately, that's a little trickier.
script.sh &
pid=$!
sleep 4h && kill "$pid" 2> /dev/null &
wait "$pid"

Run a command every 6 min in Bash [duplicate]

This question already has answers here:
How would I get a cron job to run every 30 minutes?
(6 answers)
Closed 7 years ago.
The community reviewed whether to reopen this question 2 months ago and left it closed:
Original close reason(s) were not resolved
I want to schedule a command like ./example every 6 minutes and when 6 minutes is done it exits the process and runs it again. How would I do that in Bash? I run CentOS.
I would make a cronjob running every sixth minutes and using the timeout command to kill it after, say, 5 minutes and 50 seconds.
This is a sample crontab rule:
*/6 * * * * cd /path/to/your/file && timeout -s9 290s ./example
It changes working directory to where you have your script and then executes the script. Note that I send it signal 9 (SIGKILL) using the -s9 flag which means "terminate immediately". In most cases you might want to consider sending SIGTERM instead, which tells the script to "exit gracefully". If that is the case you can consider giving the script a little bit more time to exit by decreasing the timeout value even more. To send SIGTERM instead of SIGKILL, just remove the -s9 flag.
You edit your crontab by running crontab -e
Replace mycommand in the script below...
#! /bin/bash
## create an example command to launch for demonstration purposes
function mycommand { D=$(date) ; while true ; do echo $D; sleep 1s ; done; }
while true
do
mycommand & PID=$!
sleep 6m
kill $PID ; wait $PID 2>/dev/null
done
Every six minutes, this kills the command then restarts it.
Use Ctrl-C as one way to terminate this sequence.

Resources