LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 08-06-2014, 02:01 AM   #1
ganesan_p
Member
 
Registered: Jun 2014
Posts: 37

Rep: Reputation: Disabled
Question 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.
 
Old 08-06-2014, 02:14 AM   #2
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 509

Rep: Reputation: 203Reputation: 203Reputation: 203
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!
 
Old 08-06-2014, 02:21 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 08-06-2014, 02:32 AM   #4
ganesan_p
Member
 
Registered: Jun 2014
Posts: 37

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-06-2014, 02:38 AM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 08-06-2014, 02:54 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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)....
 
Old 08-06-2014, 04:55 AM   #7
ganesan_p
Member
 
Registered: Jun 2014
Posts: 37

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-06-2014, 09:48 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ganesan_p View Post
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.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] can i write a shell script in /etc/init.d that will run a python script? lapishater276 Debian 1 06-27-2014 03:23 PM
timer in a shell script sanjith11 Programming 8 04-15-2011 10:32 PM
SHELL SCRIPT Write at the right of the shell window solo9300 Linux - General 3 09-29-2009 04:56 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration