Run a bash linux command for a specified time - linux

Is there a way in linux please to execute a command just a certain duration of time like 10 minutes ?
I wanna make a capture with: airodump-ng -w $CAPT_DEST $mon
But i just want it to last 10 minutes and then the command stops automatically.

The command you are looking for is timeout:
timeout 600 airodump-ng -w "$CAPT_DEST" "$mon"
See man timeout for more information.

Related

Cronjob stuck without exiting

I have 50+ cronjobs like the one given below running in my Centos 7 server.
curl -s https://url.com/file.php
This runs every 10 minutes. When running manually from the shell it only takes 1-2 minutes. It also is working fine using cronjob. The problem is that it does not exit after the execution. When i check my processes using ps command, it shows many cronjobs of previous dates(even 10 days before) which accumulates the total proccesses in my server.
Line in crontab :-
*/10 * * * * user curl -s https://url.com/file.php > /dev/null 2>&1
Is there any reasion for this? If i rememmber correctly this happened after latest patch update.
Please help.
Modify your command to store the logs in log files instead of dumping it to /dev/null.
Options
--max-time
--connect-timeout
--retry
--retry-max-time
can be used to control the curl command behaviour.

Issue with time command

When I run several background jobs, I need to get a runtime for the set of jobs.
I use the following example successfully:
$ sleep 10 &
$ time wait
real 0m9.74s
user 0m0.00s
sys 0m0.00s
However, if I try to format the output to seconds, I get the following error:
$ time -f %e wait
time: cannot run wait: No such file or directory
0.00
This only seems to happen with shell builtins. Is there a workaround for this?
The problem here is two-fold.
we don't know if you're using the bash (assuming you're using the centos default shell) built-in or the binary time command.
in either case wait is a bash built-in
Ways to deal with this:
/usr/bin/time -f %e bash -c wait
TIMEFORMAT="%pR" time bash -c wait

How to check if a command is hanged in a bash script?

I have a bash script, in which I call another script and sometimes second script hangs. Is there any way to check if it is hung or not. And I can't make any changes is the second script.
#!/bin/bash
calling second script(thata might hang)
if hang then do something
If you already know a threshold time, that after that script is considered hung. you can use timeout.
timeout 30 bash script.sh
command bash script.sh will run until it finish in less that 30 seconds or gets killed by timeout. You can adjust the time according to your environment.
Command Reference:
timeout
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
or: timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
Please refer to respective man page for options.

Linux - Do a command repeatedly for a certain length of time

I know what the command watch -n does. I would like to do something like the following:
watch -n 5 "ls" //Do this for 30 minutes, then stop.
Essentially I want to repeat a command every 5 seconds, then stop after 30 minutes has passed. I'm missing the stop 30 minutes part. What command should I use to achieve this?
Thanks.
Use timeout:
timeout 1800 watch -n 5 "ls"
You can use the timeout command, that is part of coreutils, something like:
timeout 30m watch -n 5 "ls"

Running 'top' command of linux for few minutes and then come out of system command in perl

I am new to Perl. I am working on a web UI where I have to give CPU and memory monitoring data, so I am using top and gnuplot command. I am able to do it through my terminal. but when I am executing same commands with a perl script its not working. The problem I am having is that whenever I am executing top command in my terminal than I have to wait for few minutes and then I have to plot it using GNUPLOT but when I am doing the same work using system command in perl I am unable to give that delay.
Here is what I am doing
system "top -p 1758 -b -d5 | tee -a stats.log";
system "./top_stats.sh -f stats.log"
Here 1758 is the app ID whose top data I have to monitor and stats.log file is the one where I am saving logs and then using this stats.log file as input to top_stats.sh script I am plotting graphs. This top_stats.sh script takes the log file and uses gnuplot to plot data
Now the problem is whenever I am executing this first system command in terminal I have to wait for some time say 2 to 3 minutes to have ample number of data points and then I have to press Ctrl+C to come out of top command and then run the script. but here as soon as this first system command is encountered it is executed and then next command is executed without any delay so I am not getting any data points to plot the graph. Is there any way I can execute my first command and wait for 3 minutes without coming out of system command and then execute the next command.??
Is there any way I can execute my first command and wait for 3 minutes without coming out of system command and then execute the next command.??
Why not specify the number of iteration for top. So your command will rougly run for (N-1)*5 seconsds. 37 iterations with an interval of 5 seconds are going to take 36*5 seconds:
system "top -p 1758 -b -d5 -n37 | tee -a stats.log && ./top_stats.sh -f stats.log";

Resources