1

feels like I visited stackoverflow 100 times before. Always found good solutions, but now I'm stuck.

I want to monitor how long a computer runs every day. This is part of a program written in Ruby, running on a Fedora machine.

Previously I did similar things under Windows, where it is possible to catch the related power events using WndProc. However, with Fedora (or Linux in general) I don't find a good hook to detect power-on and power-off/suspend/hibernate/....

Any suggestion is highly appreciated.

Thanks, jayrock

EDIT: Seems I haven't been clear in what I want to achieve. Actually want to have information about not only the current uptime but also past uptimes. Thus I should know "the computer has been running for x hours over the last 14 days" or similar.

Another addition is that e.g. the last command doesn't do the job either - it doesn't show if the system goes to hibernate or suspend.

jayrock
  • 13
  • 3

3 Answers3

1

You may want to parse the output from the last command, which logs users' logins. If the pseudo-user reboot "logged in", you know the system was rebooted. (This is valid for Ubuntu; maybe Fedora uses a different name, such as boot.)

There may be a more direct interface; Google around for ruby + utmp. The utmp file, commonly in /var/run, records the data that the last command reports.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • The last command unfortunately doesn't work for my purpose, because it doesn't log when the system goes to sleep. – jayrock Feb 14 '11 at 22:34
  • @jayrock: `last` also displays how long a user has been logged in, in the last column. For `reboot`, that would equal system uptime. – Fred Foo Feb 14 '11 at 22:40
  • Thanks, it looks like this does it. Let me do some more testing. – jayrock Feb 14 '11 at 22:56
  • Tested this extensively now. Turns out that indeed reboot stays logged on when the system goes to suspend or hibernation. So unfortunately I still don't have a good solution. For instance I get this line `reboot system boot 2.6.35.10-74.fc1 Wed Feb 16 20:00 - 08:36 (12:36)` - but for sure the system was on standby during the night. – jayrock Feb 17 '11 at 07:41
  • @jayrock: Too bad. Maybe you can try writing a "proxy program" for APMD in Ruby? See http://linux.die.net/man/8/apmd – Fred Foo Feb 17 '11 at 12:19
0

I don't know the ruby specifics but there is a command uptime in linux.

I think in ruby you need to use System

so it would be something like system 'uptime'

Then you can parse the results of that

Shaun Hare
  • 3,771
  • 2
  • 24
  • 36
0

You would use backticks like uptime StackOverflow regards them as code styles though in this answer box.

maletor
  • 7,072
  • 7
  • 42
  • 63