LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-24-2011, 02:13 PM   #1
sfaiqh
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Rep: Reputation: 0
Bash daemon encounters syntax error after midnight


Hi all,

I have a daemon script which wakes up every 5 minutes and checks the health of started processes. It works fine during the day but throws a syntax error just after midnight. Here is the log:

(02/22-23:49) Check all started processes
(02/22-23:54) Check all started processes
(02/22-23:59) Check all started processes
(02/23-00:04) Check all started processes
Utility.sh: line 273: 1298437199
1298437474 + 60: syntax error in expression (error token is "1298437474 + 60")

The code in question is as follows:

LD_LOG_FILE_MOD_TIME=60
file_mod_time_secs=$(stat -c %Y $log_file)
current_time_secs=$(date +%s)
#Line 273 below
if [ "$current_time_secs" -gt "$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))" ]; then
return $FAILURE
fi

return $SUCCESS
 
Old 02-24-2011, 03:44 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Looks like a problem with using the quotes -the '-gt' operator is for integers -and doing the math 'inline' may also not be working. Try this:
Code:
VALUE=$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))
if [ $current_time_secs -gt $VALUE ]; then
also, if there is any chance that $current_time_secs is a null value, then you need to use double brackets, or reverse the order:
Code:
if [ $VALUE -lt $current_time_secs ]; then
 
Old 04-27-2011, 09:33 AM   #3
sfaiqh
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Updates not working

Thanks for the suggestions. Updated my script to the following:

file_mod_time_secs=$(stat -c %Y $log_file)
current_time_secs=$(date +%s)
file_noupdate_time_allowed_secs=$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))
if [ $file_noupdate_time_allowed_secs -lt $current_time_secs ]; then
return $FAILURE
fi


Still getting the same error around midnight:

(04/26-23:46:02) -- Checking all processes
(04/26-23:51:02) -- Checking all processes
(04/26-23:56:02) -- Checking all processes
(04/27-00:01:02) -- Checking all processes
Utility.sh: line 273: 1303862391
1303862451 + 60: syntax error in expression (error token is "1303862451 + 60")

Any help would be appreciated.

Last edited by sfaiqh; 04-27-2011 at 09:34 AM.
 
Old 04-27-2011, 11:40 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Mysterious! I can't reproduce the error message by mangling what line 273 might be after substitutions.

How about getting some more information:
Code:
...
echo $file_mod_time_secs + $LD_LOG_FILE_MOD_TIME
echo "$current_time_secs" -gt "$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))"
if [ "$current_time_secs" -gt "$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))" ]; then
...
 
Old 04-28-2011, 09:48 AM   #5
sfaiqh
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Original Poster
Rep: Reputation: 0
With modified code to print values:

file_mod_time_secs=$(stat -c %Y $log_file)
current_time_secs=$(date +%s)
echo "file_mod_time_secs = $file_mod_time_secs"
echo "LD_LOG_FILE_MOD_TIME = $LD_LOG_FILE_MOD_TIME"
echo "current_time_secs = $current_time_secs"

file_noupdate_time_allowed_secs=$(($file_mod_time_secs + $LD_LOG_FILE_MOD_TIME))
if [ $file_noupdate_time_allowed_secs -lt $current_time_secs ]; then
return $FAILURE
fi



Output of the script:

(04/27-23:54:56) -- Checking all started processes
file_mod_time_secs = 1303948208
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303948496
(04/27-23:59:56) -- Checking all started processes
file_mod_time_secs = 1303948508
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303948796
(04/28-00:04:56) -- Checking all started processes
file_mod_time_secs = 1303948798
1303948808
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303949096
Utility.sh: line 276: 1303948798
1303948808 + 60: syntax error in expression (error token is "1303948808 + 60")

Not sure where "1303948808 + 60" is coming from as I expect it to be "1303948798 + 60".

Also 1303948808 = 1303948798 + 10
 
Old 04-28-2011, 12:16 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by sfaiqh View Post
Output of the script:

(04/27-23:54:56) -- Checking all started processes
file_mod_time_secs = 1303948208
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303948496
(04/27-23:59:56) -- Checking all started processes
file_mod_time_secs = 1303948508
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303948796
(04/28-00:04:56) -- Checking all started processes
file_mod_time_secs = 1303948798
1303948808
LD_LOG_FILE_MOD_TIME = 60
current_time_secs = 1303949096
Utility.sh: line 276: 1303948798
1303948808 + 60: syntax error in expression (error token is "1303948808 + 60")
Hi,

the bold lines look strange. I seems that your variable $file_mod_time_secs holds two values delimited by a newline character. I am not sure if there is any situation where stat might return multiple values in such a manner.
Could you post the complete script as attachment? If you want to post it directly in a post then make sure it is inside
[code] tags; since it appears to be hundreds of lines long posting it as an attachment is really better.
 
1 members found this post helpful.
Old 04-28-2011, 01:04 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
crts already hit the nail on the head! From your output the variable file_mod_time_secs contains two lines because most likely after midnight a new log file is created and the variable log_file stores two file names. Is it assigned as the output of a ls command? If this is the case you should probably sort the log files by modification time and retrieve only the last one, e.g.
Code:
log_file=$(ls -rt *.log | tail -1)
after that the stat command in
Code:
file_mod_time_secs=$(stat -c %Y $log_file)
will bring to a single value.
 
Old 05-02-2011, 08:58 AM   #8
sfaiqh
LQ Newbie
 
Registered: Feb 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Problem solved!!

This was it folks. Thanks all for your help.
 
Old 05-02-2011, 09:01 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Mark the thread as solved.
 
Old 05-02-2011, 10:43 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Threads can be marked SOLVED using the Thread Tools menu.

EDIT: Oops! Sorry! It already was marked SOLVED
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash: syntax error denneyd Linux - Software 3 04-08-2010 09:41 PM
Bash Error: syntax error near unexpected token `{ araouf Linux - Newbie 1 03-04-2010 05:43 PM
syntax error in bash tugce_zehra Linux - Newbie 2 12-05-2008 10:59 AM
bash, syntax error santana Linux - Software 2 01-23-2008 02:39 PM
BASH : syntax error semaja2 Programming 6 01-26-2006 01:33 AM


All times are GMT -5. The time now is 07:40 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration