Repeat unterminated command every x interval of time - bash

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

Related

How to repeatedly start and kill a never ending bash process

How do I repeatedly start and kill a bash script that takes a long time. I have a analyze_realtime.sh that runs indefinitely, but I only want to run it for X second bursts (just say 15s for now).
while true; do analyze_realtime.sh; sleep 15; done
The problem with this is that analyze_realtime.sh never finishes, so this logic doesn't work. Is there a way to kill the process after 15 seconds, then start it again?
I was thinking something with analyze_realtime.sh&, ps, and kill may work. Is there anything simpler?
while true;
do
analyze_realtime.sh &
jobpid=$! # This gets the pid of the bg job
sleep 15
kill $jobpid
if ps -p $jobpid &>/dev/null; then
echo "$jobpid didn't get killed. Moving on..."
fi
done
You can do more under the if-statement, sending other SIGNALs if SIGHUP didn't work.
Try this out
while true; do
analyze_realtime.sh & # put script execution in background
sleep 15
kill %1
done
Explanation
%1 refer to the latest process ran in background
You can use timeout utility from coreutils:
while true; do
timeout 15 analyze_realtime.sh
done
(Inspired by this answer)

How to provide ctrl+C in the shell script?

Below is the script that i tried to execute/automate testing,
while [ 1 ]; do
val=`expr $val + 1`
ksh ./run.ksh //This line needs the keyboard interaction so i can't run in background. It takes too long time to complete so i need to kill the above command using ctrl+C
echo "pid=$!"
echo "pid=$$"
sleep 40
val1=val;
done;
./run.ksh - is script that has some business logic to send the data to other machine and waits for the response. Even if the response received it waits for reasonable amount of time to complete the processing. Because it waits for the connection to be closed and doing other cleanup activity.
My problem is that i want to kill that script after few seconds by sending ctrl+C. When i googled i found that $! can be used to get the process id of the background process, but the same cannot be used in this case.
Is it possible to send the ctrl+C in the shell script?
Thanks in advance.
Use timeout command. For example it will be killed after 30 seconds.
timeout 30 ksh ./run.ksh

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.

run forked process continuously, kill after interval

i'm having a difficult time writing a bash script, hoping someone could help. basically i'm trying to run a number of processes at the same time and then kill them all after an interval.
so for example, if i want to run my_long_running_task 50 times and kill after 10 minutes this is what i came up with:
#!/bin/bash
PIDS=()
(while :
do
my_long_running_task;
sleep 1
done ) &
PIDS+=($!)
...{repeat while loop 50 times or stick it in a for loop)...
sleep 600; # 10 minutes * 60 seconds
for p in "${PIDS[#]}"
do
kill $p
done
i'm not a bash expert but that seems like it should work - fork all the processes adding their pids to an array. then at the end just sleep for a certain amount of time before iterating over the array and killing all the pids. and indeed this worked for my very simple poc:
#!/bin/bash
PIDS=()
(while :
do
echo '1'
sleep 1;
done) &
PIDS+=($!)
(while :
do
echo '2'
sleep 1;
done) &
PIDS+=($!)
(sleep 10; \
for p in "${PIDS[#]}"
do
kill $p
done)
but when i do something more interesting than echo - like, in my case, running phantomjs, the processes don't get killed after the interval.
any thoughts? what am i missing?
Your wish is my command (at least, when your wish aligns sufficiently with my desires):
When you run phantomjs, do you run it with exec or just as a normal process?
Does it make any difference if you do use exec?
The thought behind the questions is that you kill the shell that runs the other process (which, in the case of echo, is the shell), but that doesn't necessarily kill the children of the process. Maybe you need to use something like:
kill -TERM -- -$p
kill -- -$p
to send a signal to the process group, rather than just the process.
Also, consider whether a 'time out' command would make your life easier (timeout on Linux).

script to execute program in loop and kill current process if time limit is exceeded

I have a bash script that executes a program in a loop, but I want to set a max time limit for each execution of the program, i.e. i just want to cancel the current execution if the time limit is exceeded but I don't want to break the entire loop.
Thanks!
There is a command named timeout on my Ubuntu. You could try this:
timeout 1s yes
This will make the process yes to ends after 1 second.
Note: with this command its also possible to specify the signal as an argument.
You can also validate that the duration constaint get respected using this command:
time timeout 3s yes
This should work in any Posix shell, including bash...
#!/bin/sh
while :; do
echo starting command
while :; do
trap break SIGTERM
sleep 5; kill $$ # 5 second timeout
sleep 10 # replace this sleep with your real command
done
echo Command terminated, restarting...
done

Resources