Linux – Create auto restart Script in Linux

linuxscripting

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

Best Answer

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.