View Full Version: Auto-reboot

C++ Learning Community > C++ for Linux > Auto-reboot


Title: Auto-reboot
Description: Issues


feldmarschall - December 18, 2006 08:01 AM (GMT)
I've been looking through google and tons of places for references to Linux C++ commands and what not.

What I am going to do:

Write a program that checks say every 5 minutes if a specific program is already running, and if it is not, start it up.

Problem:

I have no idea how to do it.

For the program Im thinking to create a new thread that just has an infinite loop with some sort of pause. to start the program I probably would do system("./startup &)

But what I need to figure out is how to check if a program is already running?

Any help would be hot. Also if possible a link to some good documentation on generic Linux C++ programming.

myork - December 18, 2006 12:52 PM (GMT)
You need to look up cron.
cron is a service provided by the OS to run a program at set intervals.

Use
QUOTE
crontab -e
To edit your own personal crontab file.
The following command will run a script in your home directory every 5 minutes.
QUOTE
# run a command every 5 minutes. Output to log file
    5 * * * *      $HOME/mybin/myJob  >> $HOME/mylog/mmyJobLog


To check if a process is running use ps.

feldmarschall - December 19, 2006 08:20 AM (GMT)
K cron is driving me nuts. Im logged into the shell as user drakhl, and my home directory is /home2/drakhl.

I do crontab -e and I add my line of commands in.

So far what im doing is trying to run this every five minutes so I do

*/5 * * * * /src/startup

That should run the startup script but nothing happens..

Im a total noob with linux, does it matter if I have to run it with ./startup &

These are all the lines I've tried so far and none of them have worked.

*/5 * * * * /src/startup

*/5 * * * * /home2/drakhl/src/startup

*/5 * * * * startup

*/5 * * * * ./startup &

*/5 * * * * /src/ ./startup &

*/5 * * * * /home2/drakhl/src/ ./startup &

30 * * * * /src/ ./startup &

30 * * * * ./startup &

5 * * * * $HOME/src/startup



A little help please?

myork - December 19, 2006 09:47 AM (GMT)
Don't put a slash before the 5.
QUOTE
*/5 * * * * /src/startup
Should probably be
QUOTE
* 5 * * * * /src/startup



Check that you can run /src/startup manually. Note that you must specify a full path name to the program.

Note: this command will not produce any output to the console. Therefore it is best to send output to a log file.

Note:
QUOTE
5 * * * * $HOME/src/startup
will run this command every 5 hours.

feldmarschall - December 19, 2006 06:45 PM (GMT)
Ok * 5 * * * * /src/startup gives errors, because that is too many fields. You can only have 6.

the /5 is how you tell cron to run it every five minutes.

I tried all those commands and outputting them to a log..the log is there but it is blank..

What do you mean by running startup manually? If you mean can I run it from the shell then yes I've done so many times.

and Im trying the full path. I cded as far back as I can go and used that..

This is just weird that it doesnt work.

myork - December 20, 2006 01:19 PM (GMT)

What shell are you using?
Is the command you are running a script?
If so what is the first line (ie does it specify the shell it should be run in)?

feldmarschall - December 21, 2006 03:04 AM (GMT)
Like I said I am a linux noob. I am doing this on a remote shell. What command can I type to give me the name of the flavor Im running?

Here is the entire script. I know I go to the /src/ directory and type ./startup & to start it up.

QUOTE

#! /bin/csh -f

# Set the port number.
set port = 4100
if ( "$1" != "" ) set port="$1"

# Change to area directory.
cd ../area

# Set limits.
limit coredumpsize unlimited
limit stacksize unlimited
if ( -e shutdown.txt ) rm -f shutdown.txt

while ( 1 )
    # If you want to have logs in a different directory,
    #  change the 'set logfile' line to reflect the directory name.
    set index = 1000
    while ( 1 )
set logfile = ../log/$index.log
if ( ! -e $logfile ) break
@ index++
    end

    # Record starting time
    date > $logfile
    date > ../area/boot.txt

    # Run SMAUG.
    # Check if already running
    set matches = `netstat -an | grep ":$port " | grep -c LISTEN`
    if ( $matches >= 1 ) then
        # Already running
        echo Port $port is already in use.
        exit 0
    endif

    ../src/swreality $port >&! $logfile

    # Restart, giving old connections a chance to die.
    if ( -e shutdown.txt ) then
rm -f shutdown.txt
exit 0
    endif
    sleep 10

end

myork - December 21, 2006 10:05 AM (GMT)


The script assumes that your current working directory is in "/src"
( or at least a directory where ../area and ../log are accessable)


To get it to work do:
CODE
cd
/src/stratup


If it runs now then it should run under cron.




Hosted for free by InvisionFree