[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

cron script generating errors. possibly taking down the site.

Im not sure where this falls under so I hope I assigned it to the correct zone, heres the issue:

We have a dedicated server running a fully custom site that relies on several scripts that are used to update the site and deliver content (the site is fully automated) lately we've run into an issue where at a specific time every night, it seems to go down (system becomes unresponsive, etc)

the only error we can find is from one of the cron jobs that run:

this is the error:
/root/servrs/freezerestart.sh: line 5: ((: 059: value too great for base (error token is "059")
/root/servrs/freezerestart.sh: line 5: ((: 059: value too great for base (error token is "059")

it goes up from 009 up to that last one, then seems to crash.

here is the script: (the site has been omitted with ------)

can someone look at the script and let me know, if anything would be generating the error ?

the purpose of the script is if the site becomes unresponsive, it forces a restart of the site,
which is fine, but more often than not it never runs and the rig grinds to a halt, at exactly 5am est.


#!/bin/sh

tm=$(date +%k%M)

if (( $tm < 500 )) || (( $tm > 530 )) ; then

    curdate=$(date)
    okfname=$(date +%F)
    freeze=$(/usr/bin/perl /root/servrs/socktest.pl)
    wst=$(/usr/bin/curl --connect-timeout 15 -s --head "http://---------.org" | /usr/bin/perl -e 'my ($a) = <STDIN>; print $a = (split /\s+/, $a)[1]*1')

    if (( $freeze == 1 )) || (( $wst == 500 )) || (( $wst == 0 )); then

        pids=$(/usr/sbin/lsof /tmp/site.socket | /usr/bin/perl -e 'my @pids = map {((split /\s+/, $_)[1])} (<STDIN>); $, = "\n", print @pids[1..$#pids]');

        su - siteuser -c "~/run_-----------_site"

        for i in $pids; do
                kill -9 $i
        done
      echo "Restarted because fail on $curdate" >> /root/servrs/fail_state
    else
        echo "OK and HTTP $wst on $curdate" >> /root/servrs/ok_state_$okfname
    fi
fi
0
justin33234
Asked:
justin33234
  • 5
  • 4
  • 4
2 Solutions
 
arnoldCommented:
can you post the cron entry you have?
try using /bin/sh cron_script.sh

*/6 * * * * /bin/sh /path/to/the/above/script.sh

The error you have only happens if the script is executed using anything but /bin/sh.
 
0
 
justin33234Author Commented:
00   2 * * * /root/servrs/dailyrestart.sh

is what shows up in crontab
0
 
Garry GlendownConsulting and Network/Security SpecialistCommented:
The leading "0" causes the shell to interpret $tm as an octal number, which of course causes the 8 and 9 to be a an illegal digit when you compare it to other numbers ...
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Garry GlendownConsulting and Network/Security SpecialistCommented:
P.S. - this might fix it ...
tm=$(date +%k%M | sed "s/^ 0//g" )

Open in new window

0
 
arnoldCommented:
You could use test ([]) as an alternative
if ( [ "$tm" -lt "509" -o "$tm" -gt "530" ] ) ; then
if ( test "$tm" -lt "509" -o "$tm" -gt "530"  ) ; then
0
 
justin33234Author Commented:
so replace: if (( $tm < 500 )) || (( $tm > 530 )) ; then
with: tm=$(date +%k%M | sed "s/^ 0//g" )

sorry been ages since i've used a linux os, and even longer since i've done any scripting.


Garry, you mentioned the leading 0 is causing an illegal digit, can you clarify which you mean ?

thanks!
0
 
arnoldCommented:
You can either alter the way the value is assign by preventing the value from having a leading 0 or alter the if statement to work without regard to the value set in tm with or without a leading 0
test converts treats the value the same way whether it has a leading 0 or not.
The condition -lt (less then) -gt (greater than) set the parameters to be treated as numbers.

Either replace the
tm=$(date +%k%M) with tm=$(date +%k%M | sed "s/^ 0//g" )
or replace
if (( $tm < 500 )) || (( $tm > 530 )) ; then with  if ( test "$tm" -lt "500" -o "$tm" -gt "530"  ) ; then



0
 
Garry GlendownConsulting and Network/Security SpecialistCommented:
Using the numerical comparison "<" or ">" causes the shell script to look at your variable as a number. If you have a number that begins with a "0", it's interpretet as octal ... so comparisons will not end up as intended:

if ((010<9)) ; then echo "first arg smaller than 9" ; fi

Open in new window


=> This does not compare 10 to 9, but 010 Base 8 to 9, so 8 < 9 !
0
 
justin33234Author Commented:
hey sorry guys had to go out of town for a week.

looking at the script i dont see any numbers that start with zero, so not sure how the issue is being caused by it.

sorry if its an obvious problem, just not familiar with this.
0
 
Garry GlendownConsulting and Network/Security SpecialistCommented:
the date-command generates them!
0
 
arnoldCommented:
%k is 0-23 hours
you can change that to %l which will represent the 1-12
but you would have to introduce another check dealing with pm to capture the 1-5pm window.
Another option is to preppend a number such that the condition will never have a leading 0
tm=1$(date +"%k%M" )
and modify the check to $tm <10500 || $tm > 10530
0
 
justin33234Author Commented:
Oh! Now it makes sense.

I put in the change, it's running tonight *crosses fingers*
0
 
justin33234Author Commented:
it worked! thanks guy!!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now