Results 1 to 8 of 8
Hi,
I have written a script that runs in background. This script runs a command and while the command is running it has to wait before it can perform following ...
- 01-16-2012 #1Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
BASH: How to write script that checks other script and controls it?
Hi,
I have written a script that runs in background. This script runs a command and while the command is running it has to wait before it can perform following commands. Let's say this command takes varying amount of time to complete. What the script does is, once the command is completed it checks how long it took to complete the command and if over a certain length of time, it issues a restart command to a certain service. This service restart will solve the lag to that command.
The problem with the script is, for as long as the command hasn't completed ( be it an hour or two) the script wouldn't be able to restart the service.
So is there a way to write a script to check if that script, or better that command, has run for over a specified length of time then issue a stop command and restart the service without having the script to wait for that command to complete?
- 01-16-2012 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
You can use ps to track the elapsed time of the process. get the pid of the process (your script), then use ps to look for that time. you can use pidof to get it. you might have to format the output of the etime value to make sense of it:
Code:ps -o etime --no-heading -p $(pidof -x script.sh)
- 01-18-2012 #3Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
Thanks atreyu,

Your code works. I have been experimenting with it until I came across a command called timeout which does exactly what I want. I have to drop the original direction and use the new one. Are you familiar with timeout? I'm having a problem with its output, when I typed this:
it outputs:Code:/usr/bin/time -f %e timeout 0.1 ./script.sh
Now, I don't want the status message, I just need the time. Any idea?Code:Command exited with non-zero status 124 0.10
- 01-18-2012 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Hmm, never even heard of timeout before! So after poking around on one of my systems...are you sure the timeout command is even available on your system?
It is in /usr/bin/timeout on my system, and belongs to the coreutils package.Code:which timeout
Even if it is there, i don't think it takes floating point numbers for duration (e.g., 0.1). this works on my system:
Code:[root@localhost ~]# /usr/bin/time -f %e /usr/bin/timeout 1 sleep 5 1.00
Last edited by atreyu; 01-18-2012 at 01:25 AM. Reason: removed ghost text
- 01-18-2012 #5Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
Unless I misunderstand it..here's from manpage :
But that isn't importantCode:NAME timeout - run a command with a time limit SYNOPSIS timeout [OPTION] DURATION COMMAND [ARG]... timeout [OPTION] ... DURATION is a floating point number with an optional suffix: `s' for seconds (the default), `m' for minutes, `h' for hours or `d' for days.
.
I tried the same commands you provided:
Strange that it output that message..Code:[root@localhost ~]# /usr/bin/time -f %e /usr/bin/timeout 1 sleep 5 Command exited with non-zero status 124 1.04
I'm using Arch Linux by the way..
- 01-18-2012 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Interesting...not the same on my system - Fedora 15 and timeout (GNU coreutils) 8.10
Anyway, it seems that you'll just have to remove the portion of the output that you don't want. Here's some simple examples:Code:[root@localhost ~]# /usr/bin/timeout --help Usage: /usr/bin/timeout [OPTION] DURATION COMMAND [ARG]... or: /usr/bin/timeout [OPTION] Start COMMAND, and kill it if still running after DURATION. Mandatory arguments to long options are mandatory for short options too. -k, --kill-after=DURATION also send a KILL signal if COMMAND is still running this long after the initial signal was sent. -s, --signal=SIGNAL specify the signal to be sent on timeout. SIGNAL may be a name like `HUP' or a number. See `kill -l` for a list of signals --help display this help and exit --version output version information and exit DURATION is an integer with an optional suffix: `s' for seconds(the default), `m' for minutes, `h' for hours or `d' for days. If the command times out, then exit with status 124. Otherwise, exit with the status of COMMAND. If no signal is specified, send the TERM signal upon timeout. The TERM signal kills any process that does not block or catch that signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.
Code:/usr/bin/time ... timeout ... command 2>&1|tail -n1 /usr/bin/time ... timeout ... command 2>&1|grep ^[0-9]*
- 01-18-2012 #7Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
Cool! never thought of tail.
tail solves the problem. grep also but I think I stick with tail
Thank you very much
It seems that Arch coreutils is version 8.15.
www archlinux org/packages/core/i686/coreutils/
- 01-18-2012 #8Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
man, i thought Fedora was bleeding edge. I just checked and Fedora 16 is only on coreutils 8.12...nice Arch!
Originally Posted by aizul357
glad its sorted.


