0

I've wrote a script for fetch real uptime and display in monit. But I think it displays just exit error status. My uptime script is:

#!/bin/sh
uptime=`uptime | awk -F " " '{print $3}'`
echo $uptime
exit $uptime

When I run it in terminal result is true

For example:

# uptime
 08:39:01 up 421 days, 19:54,  1 user,  load average: 0.06, 0.10, 0.06
# ./up_time.sh
421

Script works well. But when monit starts to run this script by up_time.conf file;

Program   Status     Last started                Exit value
days_up   Status ok  Thu, 01 Aug 2013 08:41:08      165

I saw on the browser. I guess that Exit value is the bash exit error code. I cant get why other script are works fine but this one does not do his job

Speaking of which, monit up_time.conf file is like this:

check program days_up with path "/etc/monit/scripts/up_time.sh"
    if status < 1 then alert
Sencer H.
  • 532
  • 1
  • 8
  • 17
  • BTW, is your uptime really 421 days? That strongly indicates to me that you haven't been properly been patching your system, unless you also happen to have ksplice setup. – Zoredache Aug 01 '13 at 18:03
  • No ksplice setup I've been installed, but I do security updates whenever it needs. Kernel updates are very rare so it does not needs reboot much. – Sencer H. Aug 02 '13 at 12:53

2 Answers2

1

The exit value of a commands are an unsigned 8 bit integer (aka a 1 byte). This means the exit value must be between 0 and 255. Your script seems to be trying to set an exit value of 412 which simply isn't valid as an exit code. The extra bits will get ignored, and so the actual value that gets returned is 156.

412 = (0001 1001 1100), 156 = (1001 1100)

Here is some output using bash's $? to demonstrate. The `$? variable is stores the exit value of the previous command.

root@:~# bash -c 'exit 5' ; echo $?
5
root@:~# bash -c 'exit 253'; echo $?
253
root@:~# bash -c 'exit 255'; echo $?
255
root@:~# bash -c 'exit 256'; echo $?
0
root@:~# bash -c 'exit 257'; echo $?
1
root@:~# bash -c 'exit 412' ; echo $?
156
Zoredache
  • 128,755
  • 40
  • 271
  • 413
0

Why exit $uptime? Shouldn't it be just exit 0, meaning your shell script did finish OK?

Janne Pikkarainen
  • 31,454
  • 4
  • 56
  • 78
  • He is trying to return the uptime in days to monit, so monit can perform actions based on that information. His method fails because of limitations of the exit command. – Zoredache Aug 01 '13 at 18:29
  • Or monit limitations of reading script output. I'm not sure enough of monit capabilities. Can monit read output of `uptime | awk -F " " '{print $3}'` command or It can read just exit status? – Sencer H. Aug 02 '13 at 12:55