Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i have a small logging tool on my linux box that need to be restart once every 20-30 minutes because the log node shuts down the session every once in a while.

how wold you go about creating a script to make a sw restart like that.

the command for starting the tool wold be somthing like this

root@25-3b-1d-46-3f-13:/home/#logdrift -f 10.15.12.10 test.log

when it restarts it needs to add a number to the test.log file name like, test1.log

share|improve this question
add comment

migrated from stackoverflow.com Oct 2 '09 at 2:13

This question came from our site for professional and enthusiast programmers.

6 Answers

up vote 2 down vote accepted

Montecristo's cron command is a good way to schedule a job every 20 mins. As written, though, it will start a new instance of "command" without killing the old one. Nor does it place a record in "test1.log."

I assume the target program is named "#logdrift" (the leading hash is legal, but it presents some awkward escaping issues), and that you require exactly one instance of it running at all times.

Instead of having the cron entry run the target process directly, I'd create a simple wrapper script and execute that every 20 mins. Here, the wrapper would have 3 duties:

  • To kill the running instance of the target process
  • To start a new instance of the target process
  • To make a record in test1.log

However, most programs shouldn't require a regular restart. Instead of throwing together a quick workaround like a cron job and wrapper script, it may be more appropriate to address the underlying problem.

share|improve this answer
    
Good answer, in particular the ending part. Although I don't think the command is intended to last over 20 mins yours it's still a valid point. +1 –  Alberto Zaccagni Oct 1 '09 at 14:45
    
i have no control over the underlying issues, this is because the loging is done against STBs that is connected trough a Fiberoptic Router, the loging shuts down when the customer for instance removes the power for the router but not the STB and other scenarioes that i have no control over. Because my company does not controle the development of the SW on the STB we cant fix the way the STB behaves when it has no soure to relay info too. Im potensialy loging 100 of STBs a week and having to manualy restart maybe 5-10% of logs at various times becaus og issues like this. –  Darkmage Oct 1 '09 at 16:10
add comment

I would use logrotate to do this, you can then set up a post rotate script (using the postrotate directive) that is activated from logrotate to restart that program. Traditionally, that program should take a SIGHUP signal to reopen its logs, but I have never heard of logdrift.

Logrotate will be called using cron (which is what you use to run anything every X minutes, hours, days, etc). Logrotate will also handle incrementing the logs by taking previous logs and making them log.1, and then making log.1 log.2 etc...

If this program is a daemon, I would create an init script for starting it and stopping it. This article has an example on how to do this for redhat like systems. See scripts in /etc/init.d/ for examples of how programs are stopped and started.

share|improve this answer
add comment
*/20 * * * *  /path/to/your/command

This runs every 20 minutes.
To change the number of the logfile you could look in the directory where it resides and parse its name, if it has no number the new file will have a 1 before the dot, if it has a number just add 1 to it.
I personally dislike the idea of having a variable with a wider scope to hold the count.

share|improve this answer
add comment

There already exists a great utility to do this that a lot of people forget about: init

I suggest adding:

ld:35:respawn:/usr/local/bin/logdrift -f 10.15.12.10 test-$(%F-%T).log

to /etc/inittab.

The only thing you need to worry about is that if the process respawns too fast (>10 times in 2 minutes) it will refuse to rerun it for 5 minutes. Consider that a feature. :)

share|improve this answer
add comment

If you have a startup script that calls logrotate you could then put it in inittab with a respawn directive. This would re start the process any time it is terminated.

share|improve this answer
add comment

in cron:

*  *  *  *  sun,tue,thu,sat  shutdown -r now

This reboots Sunday, tuesday, thursday, and saturday.

share|improve this answer
    
The user asked for a command to restart the logging software, not the computer. –  pavium Oct 1 '09 at 12:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.