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
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.
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:
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.
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.
There already exists a great utility to do this that a lot of people forget about: init
I suggest adding:
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. :)
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.
in cron:
This reboots Sunday, tuesday, thursday, and saturday.