linux service script for storing uptime

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions linux service script for storing uptime
# 1  
Old 12-14-2010
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




2. Relevant commands, code, scripts, algorithms:



3. 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





4. 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

Last edited by fed.linuxgossip; 12-14-2010 at 03:21 PM..
# 2  
Old 12-14-2010
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.
# 3  
Old 12-14-2010
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
# 4  
Old 12-14-2010
To run a Shell script as a service.
Code:
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.
Code:
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.
# 5  
Old 12-14-2010
Quote:
Originally Posted by fed.linuxgossip
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
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.
# 6  
Old 12-14-2010
DGPickett and methyl

thank you for all the valuable info.
# 7  
Old 12-14-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

2. Shell Programming and Scripting

Uptime script

How to write a shell script which can generate server uptime report from the UNIX servers? (3 Replies)
Discussion started by: paventhan
3 Replies

3. Shell Programming and Scripting

Perl script for taking inputs from one script and storing them into a document.

Hi. I wanted to create a Perl script which can take the outputs of a Perl script as it's input and temporarily store them in a document. Need help. Thanks.:) (8 Replies)
Discussion started by: xtatic
8 Replies

4. Shell Programming and Scripting

Extract the uptime from the output of the uptime command

Hi! I want to extract the uptime from the output of the uptime command. The output: 11:53 up 3:02, 2 users, load averages: 0,32 0,34 0,43 I just need the "3:02" part. How can I do this? Dirk (6 Replies)
Discussion started by: Dirk Einecke
6 Replies

5. Shell Programming and Scripting

linux service script for storing uptime

Hi, 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... (1 Reply)
Discussion started by: fed.linuxgossip
1 Replies

6. Shell Programming and Scripting

rsync - storing password in script

Hello, I wish to store the password in an rsync script so that when prompted it just enters the password. I know I can set up passwordless logins, but I have never been able to do this on this particular server so I am resorting to storing the password in the script: rsync -avz -e ssh... (4 Replies)
Discussion started by: stuaz
4 Replies

7. UNIX for Dummies Questions & Answers

Storing lines of output into a script variable

I'm sure this is a simple thing but I can't figure it out. In a script that I'm writing, I'd like to be able to store each line of output from "ls -l" into a variable. Ultimately I'd like to end up with something like: for a in `ls -l` do something with $a doneBut that's reading each... (2 Replies)
Discussion started by: ewoods
2 Replies

8. Programming

Linux Service

Hi, I want to write a service like windows service for my rhel4u5 server.And this service every day at 4:00 am connect a ftp server and download file then parse information at txt file then insert or update info at my database server(oracle).and this service can be stopped or paused by user.And... (1 Reply)
Discussion started by: ariyurek
1 Replies

9. Shell Programming and Scripting

calculate server uptime in % (99.98), using ksh script

Let me preface by saying, I have looked through many threads that deal with keep the decimal, however I'm not sure that any one resolution meets my needs, ok, ok, they could. So maybe it's just that I am not understanding the resolution - therefore I am posting a new thread. myknowledgebase=at... (2 Replies)
Discussion started by: cml2008
2 Replies

10. UNIX for Dummies Questions & Answers

script to autorestart if uptime greater than... help needed.

i'm trying to write a script that will check my os x box for uptime and autorestart gracefully if the uptime is greater than a certain number of days. thus far i have this: if uptime | cut -d ',' -f 1 | cut -d ' ' -f 4 -gt 10 ; then echo "yes" fi this doesn't work and i've tried... (11 Replies)
Discussion started by: alternapop
11 Replies
Login or Register to Ask a Question