Linux "time bound" script help for a n00b

Soldato
Joined
18 Oct 2002
Posts
8,973
Guys,

Hello, not posted here for a long time but I've been lurking. Decided to try and start contributing again anyhow.

I've recently taken to a little bit of Linux and I've realised that the easiest way for me to achieve what I'm after is to script a load of commands, however, I've no idea where to start. Perhaps if I explain my objective maybe someone could give me some assistance on how to get there or maybe even give me a template? As I say I'm a complete Linux n00b and scripting n00b so go easy.

I'm trying to get a program to start, then write to a file it creates itself for x number of hours, then stop and start again writing but to a new file. I've been doing this manually for a while and I'm sure theres a simpler way of doing this, so I thought of scripting it, however, I've no idea where to start.

Any help would be Greatly appreciated.

Many thanks in advance.

Regards,

Greg
 
Thinking about this now, to add to this, I'd also like to then process that file once it's been created. The data that's written into the file then needs to be processed into another two, perhaps this can be done using a different script?

First things first I supose!

Thanks

Greg
 
The following will allow you to run a program for a set number of hours, and then rerun a set number of times, with each run outputting to a different log file. It's probably not the most effective or efficient way to do it but it works.

Code:
#!/bin/bash

number_of_hours_to_run="1"
number_of_interations="2"
log_file="/home/geuben/program"

function start_program
{
        local logfile=${1}
        ./program_name >> ${logfile} &
}

function stop_program
{
        killall program_name
}

for (( i=0;i<${number_of_interations};i++ )) ; do
        start_program "${log_file}.${i}.log"
        for (( j=0;j<${number_of_hours_to_run};j++ )) ; do
                sleep 3600
        done
        stop_program
done

With some more information about what exactly it is you're trying to do I could come up with something better.
 
I've given this a go and thinking about it now, I don't really need the log file. If I could just get it to restart a program every hour then how would I need to take out from the script.

Also, for some reason, it doesn't like the { } brackets and runs with an error. Any ideas?

I'm basically just wanting a program to restart itself every x number of seconds because its really flakey and would just like it to restart itself so that it doesn't crash after 2 hours or so. Make sense?

Thanks

Greg
 
To stop it logging to a file simply remove

>> ${logfile}

Can you post the exact wording of the error you are getting.

Also, if the program crashses and exits then you should use init to respawn the program for you, a much neater way. Have a google for inittab
 
Back
Top Bottom