linux service script for storing uptime

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

Can you please advise a script for the following:

write linux service named system_up_duration .This service will create one file named uptime in directory �/temp/�. In this file the service will store the total time for which the system is up and running .The file will be updated after every one minute. If system is rebooted the time inside the file uptime should be reset to 0

  1. Relevant commands, code, scripts, algorithms:

  2. The attempts at a solution (include all code and scripts):

With my limited knowledge, I feel the script ( say xyz.some_ext) should be like this:

#!/bin/bash
DIR=/temp

if [ ! -d "$DIR" ]
then

mkdir /temp/
uptime | awk {'print $3,$4}' | cut -d, -f1 > /temp/uptime

else

echo > /temp/uptime
uptime | awk {'print $3,$4}' | cut -d, -f1 > /temp/uptime

Then we can set this script to run once from rc.local during startup. However I do not know how to run this as service ( else we will have to run a cron to update time every min).

Please advise.

Thank you

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

For a friend
Ashok Deshmukh
nowrosjee wadia college
Pune, India

Well, /tmp is a bad place for records. I would put a nicely structured line in a file using 5 minute cron + a script that can tell if it is all 'UP', and maybe daily (not another cron, but smarts in this script to see that the file date is yesterday, maybe using date in the file name, start/use a new file and summarize any old file file in a summary log with a line for the day or any span of uptime or downtime. To tell if an old file has been summarized, move them to an archive dir after summarizing them. Preiodically, add them to a zip file and delete the uncompressed files.

Hi,

the path is /temp not /tmp . Moreover this is a assignment script only, not to be used in production. Plus this needs to run as a service, is running a cron necessary?

Thanks

To run a Shell script as a service.

nohup /path/scriptname & ; PID=$?
#Don't forget to record the PID in a file because we will need it on shutdown.

Some pointers:

1) Check with your tutor
Is using the unix "uptime" command allowed?

2) How do you stop the process?
Some prefer using a "semaphore" file which when it exists causes the script to remove the file then exit.
Others prefer using a Shell "trap" command and issuing say a "kill -15" to stop the script.
I use both. Sometimes you can't wait for a poll.

3) Here is an example of continuous operation of a Shell script as a Service.

MYEXIT ()
{
if [ -f /var/tmp/exit_uptime_monitor ]
then
       rm /var/tmp/exit_uptime_monitor
fi
exit
}
#
trap 'MYEXIT' 1 2 3 15
#
#
while true
do
       # Check whether we need to exit
       if [ -f /var/tmp/exit_uptime_monitor ]
       then
              break
       fi
       #
       #
       # Check whether we have been told of a reboot
       # Hint process the "start" parameter if this is a rc script.
       #
       #
       # Do my process here
       #
       #
       sleep 60             # Seconds
done
#
# Clean up and exit
MYEXIT

4) Can your process keep running ?
This is unix. Uptime in months and years is not unusual.

5) If you are using rc scripts
You have a S script to get things started on reboot.
Don't forget to have a K script which issues an appropriate "kill -15"
to get stop the background script quickly.

Lots of school class code ends up in production, so why not pretend harder and get extra points? It just feels like teaching bad practice.

cron has startup costs for each test, but solves the restart on reboot problem intrinsically.

By service you just mean nohup daemon looping, sleeping? Well, after you solve the reboot problem, the total might be more complex than a good cron line. Most of the effort goes into data processing to turn the points into a report. I suppose you could look at (today) 3:05 and see the system has been up from 2010-09-31 08:35 to (today) 3:00 and just update it to say 3:05, but then you might get an error when half updated or something such. If you mmap() a file, it could be a binary update of one int.

DGPickett and methyl

thank you for all the valuable info.

PS: cron process flush their FILE* and pipes when they exit, so nothing is stuck in a pipe or buffer not in the log file.