This question already has answers here:
Command line command to auto-kill a command after a certain amount of time
(15 answers)
Closed 3 years ago.
I am trying to run a script which takes input from text file and based on the number of entries in it, a command is executed as many number of times.
Below is an overview:
cat /tmp/file.txt | while read name
do
<<execute a command using value of $name>>
done
What is happening is sometimes the command executed for particular $name is getting hung due to known issues. Therefore I need in such cases that the command on every value of $name runs only for X number of seconds and if it is not able to complete within that stipulated time, terminate the process and increment loop counter.
I was able to make use of sleep and kill but it is terminated the entire loop. I want the next values to be processed in case command gets hung on a row/value.
Please advise.
Sounds like you might want something like timeout.
timeout 4 <command>
Related
This question already has answers here:
How would you stream output from a Process?
(2 answers)
Run command, stream stdout/stderr and capture results
(2 answers)
Closed 8 months ago.
When someone just wants to get the output of a simple command like ls, then one can write like this:
let output = Command::new("ls")
.stdout(Stdio::piped())
.output()
.unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
However, if I want to get the output of an infinite running program like http-server, it won't stop until I kill it manually. So if I use the method above, my project will stuck there forever. How should I do for such situation? More specifically, how to get the output of the first five seconds, or the first five lines.
Use .stdout(Stdio::piped()).spawn().unwrap().stdout.unwrap() and read from it. If you want to read the first five lines, use a BufReader and BufRead::lines(). The first five seconds is more tricky, you'll either need to do the reading in a separate thread, or read nonblocking.
Playground
I am making a simple ping script in python, and was looking to add functionality for getting host names for IPs that are up. To do this, im getting the output of nmblookup -A {ip} using os.popen, and parsing the output. the problem im running into is that for systems where nmblookup wont work (such as routers), the command takes a long time to get an error, whereas when the command runs successfully, it retruns results in under a second. My question is how to only wait N seconds for the nmblookup command to return something, and if it doesn't, move on with the program? PS, this is all in linux.
You can prefix the command with timeout.
Refer to the man page of it.
You can do a Popen and run your command as
root#ak-dev:~# timeout 1 sleep 20
root#ak-dev:~# echo $?
124
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.
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.
I need to compute N operation (same operation) each hour.
For the moment I have my operations but I can't understand execute it N time per hour:
#!/bin/bash
N=10;
# my operation
sleep 3600/${N}
Any suggestion?
Well, the way you have it written now, it will only perform "my operation" once, after which it will sleep for 6 minutes, and then exit. What you need is a loop construct around those two lines. bash supports several different loop constructs, but probably the simplest for this case would be:
#!/bin/bash
N=10
((delay=3600/N))
while true
do
# do something
sleep ${delay}
done
Of course, there's no provision for terminating the loop in this case, so you'll have to exit with ^C or kill or something. If you only wanted it to run a certain number of times, you could use a for loop instead, or you could have it check for the [non-]existence of a certain file each iteration and exit the loop when you create or remove that file. The most appropriate approach depends on the bigger picture of what you are really trying to do.