Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-06-2014, 02:01 AM
|
#1
|
Member
Registered: Jun 2014
Posts: 37
Rep: 
|
how to write timer shell script?
hi,
i am checking filesystem using chkfsys utility . this utility return zero(0) if filesystem is good. sometimes chis utility taking more time if error occured in filesystem .
so i have to check filesystem. if it will return the value less then 5 sec i have to return 0 else return 1.
i have to write this in shell script please help me .
regards,
ganesan.
|
|
|
08-06-2014, 02:14 AM
|
#2
|
Member
Registered: Jan 2012
Location: South Africa
Posts: 509
|
Hi,
I'm not sure that I agree with the reasoning that because a command is taking long there must be a problem with the filesystem, but don't know enough about what you're doing to really form an opinion about that.
Regarding how to time a command and return a value based on that, something like this should do:
Code:
#!/bin/bash
## time a command
# Remember start time
starttime=$(date +"%s")
# Place your command here, instead of my "sleep" command
sleep 1
# Remember end time
endtime=$(date +"%s")
# Calculate elapsed time
(( elapsed = endtime - starttime ))
# Display result - for debugging - optional
echo $elapsed
# Return exit status depending on time
if [ $elapsed -lt 5 ]
then
exit 0
else
exit 1
fi
Good luck!
|
|
|
08-06-2014, 02:21 AM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
you might be able to use the "time" command for this. For example something like
Code:
#!/bin/bash
# Don't use shell built in time, because we want to format the output
# Redirection gymnastics because the output is to stderr
tfloat=$(/usr/bin/time -f %e chkfsys 2 2>&1)
# Strip off the decimal part so we can use integer comparison
tint=${tfloat%.*}
# Do the comparison
if [ $tint -lt 5 ] ; then
exit 0
fi
exit 1
HTH,
Evo2.
|
|
|
08-06-2014, 02:32 AM
|
#4
|
Member
Registered: Jun 2014
Posts: 37
Original Poster
Rep: 
|
thanks for reply,
but chkfsys utility taking more time it won't end (because it is asking user input). we have to kill stop that process it its taking more time
regards,
ganesan.
|
|
|
08-06-2014, 02:38 AM
|
#5
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
ok, how about something like
Code:
#!/bin/bash
chkfsys &
sleep 5
pkill chksfys
if [ "$?" = "0" ] ; then # chksys was killed
exit 1
fi
exit 0
Note that this will kill all running chkfsys processes that the user has permission to. Also note, that like cliffordw, I really wonder if you are using the right approach here.
Evo2.
Evo2.
|
|
|
08-06-2014, 02:54 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,005
|
you need to start two processes, the first one will run chkfsys and the second one will kill it after the specified timeout. but actually it has no meaning, if chkfsys waits for user input it will fail anyway (because you will kill it)....
|
|
|
08-06-2014, 04:55 AM
|
#7
|
Member
Registered: Jun 2014
Posts: 37
Original Poster
Rep: 
|
thanks for your reply,
actually i have four partition in my CF(hd0t77,78,79,8) . filesystem corrupted may happen any partition. i have to check the all partitions . i checked the 4 partition first two partitions filesystems are good .third partitions is problem .so i have to find which partitions is good ,which partition is bad.
regards,
ganesan
|
|
|
08-06-2014, 09:48 AM
|
#8
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,605
|
Quote:
Originally Posted by ganesan_p
thanks for your reply,
actually i have four partition in my CF(hd0t77,78,79,8) . filesystem corrupted may happen any partition. i have to check the all partitions . i checked the 4 partition first two partitions filesystems are good .third partitions is problem .so i have to find which partitions is good ,which partition is bad.
|
Ok...so show us what YOU Have written/tried to fix this problem. So far, you've had others write your script for you, and shown no effort of your own.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 06:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|