write a program to reboot the computer

write a program to reboot the computer

Post by boaz » Wed, 10 Jan 2001 01:47:53



Hi there,
    I am trying to write some kind of monitoring program to monitor if any
of the running programs has crashed or not responding.  If any of the
running programs is crashed, this monitoring program will reboot the
computer.

any idea how I can do this?

--
--

 
 
 

write a program to reboot the computer

Post by Chri » Wed, 10 Jan 2001 02:33:49



> Hi there,
>     I am trying to write some kind of monitoring program to monitor if any
> of the running programs has crashed or not responding.  If any of the
> running programs is crashed, this monitoring program will reboot the
> computer.

This seems a bit extreme, but fair enough.

Quote:> any idea how I can do this?

Obviously you need to be able to monitor the
relevant running programs. In some cases this
is simple, for instance, the programs might
leave a /var/run/foo.pid file in existence when
they start, and you could read the PID out of
it, and see whether that program is still
running (see whether kill(pid, 0) returns -1
with errno set to ESRCH). Of course, since PIDs
are recycled after a while, you need to check
that the program is what it really seems to be
(though this may not be a serious problem in
actual operation); to do this, look in
/proc/<pid> and see whether the various
pseudofiles look plausible. For instance, you
could lstat /proc/<pid>/exe and verify that it
is the correct program.

Of course, it might be more useful to see
whether the program is still *working*. I would
have thought the best approach here is to write
a little script for each program which tests for
correct operation; for instance, if the program
you are monitoring is a webserver, you could do
lynx -source -dump http://server/ and check that
you get something plausible back. If it's a
generic network server of some sort, try using
netcat ("nc") to connect to it; this won't check
whether it's _properly_ working, though --
simply that it's still listening on its port. For
a complete check, you need to be able to run a
test which verifies operation in enough detail to
assume that the whole thing is still OK.

To reboot the system, just do

void reboot_now() {
    char *Xargv = {"/sbin/shutdown",
                   "-r",
                   "now",
                   "NULL"};
    char *Xenvp = {"PATH=/bin:/sbin:/usr/bin:/usr/sbin",
                   NULL};

    execve(Xargv[0], Xargv, Xenvp);

    /* If we get here, something has gone wrong. */

Quote:}

or something similar.

--
Chris Lightfoot -- http://www.ex-parrot.com/~chris/
ignore From: line; try "chris at ex hyphen parrot dot com"
  "Any person who knowingly causes a nuclear weapon test explosion
  or any other nuclear explosion is guilty of an offence...."
  (Nuclear Explosions Act, 1998)

 
 
 

write a program to reboot the computer

Post by boaz » Wed, 10 Jan 2001 02:53:32


Allright!  Thanks!  I will give it a try.
But can I do all these checking with Java?
I am writing this monitoring program in Java.



> > Hi there,
> >     I am trying to write some kind of monitoring program to monitor if
any
> > of the running programs has crashed or not responding.  If any of the
> > running programs is crashed, this monitoring program will reboot the
> > computer.

> This seems a bit extreme, but fair enough.

> > any idea how I can do this?

> Obviously you need to be able to monitor the
> relevant running programs. In some cases this
> is simple, for instance, the programs might
> leave a /var/run/foo.pid file in existence when
> they start, and you could read the PID out of
> it, and see whether that program is still
> running (see whether kill(pid, 0) returns -1
> with errno set to ESRCH). Of course, since PIDs
> are recycled after a while, you need to check
> that the program is what it really seems to be
> (though this may not be a serious problem in
> actual operation); to do this, look in
> /proc/<pid> and see whether the various
> pseudofiles look plausible. For instance, you
> could lstat /proc/<pid>/exe and verify that it
> is the correct program.

> Of course, it might be more useful to see
> whether the program is still *working*. I would
> have thought the best approach here is to write
> a little script for each program which tests for
> correct operation; for instance, if the program
> you are monitoring is a webserver, you could do
> lynx -source -dump http://server/ and check that
> you get something plausible back. If it's a
> generic network server of some sort, try using
> netcat ("nc") to connect to it; this won't check
> whether it's _properly_ working, though --
> simply that it's still listening on its port. For
> a complete check, you need to be able to run a
> test which verifies operation in enough detail to
> assume that the whole thing is still OK.

> To reboot the system, just do

> void reboot_now() {
>     char *Xargv = {"/sbin/shutdown",
>                    "-r",
>                    "now",
>                    "NULL"};
>     char *Xenvp = {"PATH=/bin:/sbin:/usr/bin:/usr/sbin",
>                    NULL};

>     execve(Xargv[0], Xargv, Xenvp);

>     /* If we get here, something has gone wrong. */
> }

> or something similar.

> --
> Chris Lightfoot -- http://www.ex-parrot.com/~chris/
> ignore From: line; try "chris at ex hyphen parrot dot com"
>   "Any person who knowingly causes a nuclear weapon test explosion
>   or any other nuclear explosion is guilty of an offence...."
>   (Nuclear Explosions Act, 1998)

 
 
 

write a program to reboot the computer

Post by nathan wagn » Wed, 10 Jan 2001 02:36:21



>any idea how I can do this?

man -k reboot

--
Chance is irrelevant.  We will succeed.  -- Seven of Nine

 
 
 

write a program to reboot the computer

Post by el.. » Wed, 10 Jan 2001 03:30:02




>    I am trying to write some kind of monitoring program to monitor if any
>of the running programs has crashed or not responding.  If any of the
>running programs is crashed, this monitoring program will reboot the
>computer.

>any idea how I can do this?

See:

man 2 reboot

--
http://www.spinics.net/linux

 
 
 

write a program to reboot the computer

Post by Dave Bla » Wed, 10 Jan 2001 03:56:36



> Allright!  Thanks!  I will give it a try.
> But can I do all these checking with Java?
> I am writing this monitoring program in Java.

Checking and restarting programs is easily done with cron and
bash. That is why they are there. Rebooting is ALMOST never a
good idea in linux, except for incompetent admins. Most systems
have initscripts with a script for every system function. It is
fairly easy to clone one of these for whatever function you
desire.

In the cron job, check the status using the initscript, parse
the output, and stop; start the script if the program has
crashed. Don't reinvent system administration with java - shell
tools were really made to do this. Java was made for the web.

I suggest the book

Portable shell programming
by Blinn

--
Dave Blake

 
 
 

write a program to reboot the computer

Post by Chri » Wed, 10 Jan 2001 05:32:33



> Allright!  Thanks!  I will give it a try.
> But can I do all these checking with Java?
> I am writing this monitoring program in Java.

Possibly. Java itself is not a problem, so long
as you are prepared to trust in the stability of
its virtual machine (note that your monitoring
program must be more stable than the --
presumably -- buggy software you are using it to
monitor).

The question is more whether Java supports a
sufficiently complete interface to the POSIX
API. Maybe it does, maybe it doesn't. You might
be able to get away without very much, given
that much of your investigation of running
processes can be done through /proc on Linux;
however, I do not know whether you would be able
to call kill(2), fork(2) and so forth.

Network-type stuff can fairly readily be done in
Java, though as I recall mapping the Java
exceptions to the socket-level errors can be a
bit of an adventure at times.

If there is no compelling reason to write in
Java (maybe there is, I don't know), I would
write this program in perl or C, calling out to
the shell for the various monitoring scriptlets.

--
Chris Lightfoot -- http://www.ex-parrot.com/~chris/
ignore From: line; try "chris at ex hyphen parrot dot com"
  Glory may be fleeting, but obscurity is forever

 
 
 

write a program to reboot the computer

Post by Roland Smit » Wed, 10 Jan 2001 08:11:27



> Hi there,
>     I am trying to write some kind of monitoring program to monitor if any
> of the running programs has crashed or not responding.  If any of the
> running programs is crashed, this monitoring program will reboot the
> computer.

You don't have to reboot the computer for that. This ain't windows!

Just run a shell-script as a cron-job that greps through the outut of ps to
find a program, and relaunches the program if it doesn't find it.

E.g, something like this:

#!/bin/sh
list=foo bar baz
for prog in $list; do
        ps -xacu|grep $prog
        if [ $? -ne 0 ]; then
                # not found, restart program.
                $prog &
                # leave a not in the logfile.
                logger "restarting foo"
        fi
done

HTH, Roland

--
Roland Smith                        "When life hands you a lemon,

http://www.xs4all.nl/~rsmith/

 
 
 

write a program to reboot the computer

Post by Colin Smi » Thu, 11 Jan 2001 08:40:27



>Hi there,
>    I am trying to write some kind of monitoring program to monitor if any
>of the running programs has crashed or not responding.  If any of the
>running programs is crashed, this monitoring program will reboot the
>computer.

Reboot the *entire* system just because one progam isn't
responding/running?

At most all you should have to do is stop and start the program via the
init scripts.

e.g.
/etc/rc.d/init.d/programname stop
/etc/rc.d/init.d/programname start

or if the script has the option:

/etc/rc.d/init.d/programname restart

Quote:>any idea how I can do this?

Take a look at Big Brother (http://www.bb4.com). It's an entire web based
monitoring system which constantly monitors and tests the services running
on systems. The system is made up almost entirely of shell scripts which
check most of the things that a systems administrator want's to look at;
disk space, running processes, network services etc etc. It can alert via
email, pager, SMS as well if certain criteria are met.

One thing it *doesn't* do by default is attempt to restart processes or
services. The authors took this decision deliberately for good reasons but
you could easily alter the scripts to perform this task.

It's free too.

--

|Windows 2000 is a REALLY dumb name for an OS!!! |        AKA        |
|Linux is a MUCH better name for an OS!    :-)   |    The W2K Bug    |

 
 
 

write a program to reboot the computer

Post by Kaz Kylhe » Thu, 11 Jan 2001 08:52:38


On Tue, 9 Jan 2001 23:40:27 +0000, Colin Smith



>>Hi there,
>>    I am trying to write some kind of monitoring program to monitor if any
>>of the running programs has crashed or not responding.  If any of the
>>running programs is crashed, this monitoring program will reboot the
>>computer.

>Reboot the *entire* system just because one progam isn't
>responding/running?

Perhaps you haven't used Windows NT, where this is not unheard of, if
not in fact common.
 
 
 

write a program to reboot the computer

Post by Victor Wagn » Thu, 11 Jan 2001 06:14:06


: Hi there,
:     I am trying to write some kind of monitoring program to monitor if any
: of the running programs has crashed or not responding.  If any of the
: running programs is crashed, this monitoring program will reboot the
: computer.

It is just pure overkill to reboot the computer becouse just one program
crashed. Have a pity for poor user who just at this moment when monitor
notices that one program crashed, uses another program running on the
same computer.

Actually, there alreary is monitor, which notices if some program
crashes or exits, and respawns it immediately. It is called /sbin/init and
for its special significance always gets process ID 1.

read man inittab.

--
I'm telling you that the kernel is stable not because it's a kernel,
but because I refuse to listen to arguments like this.
        -- Linus Torvalds

 
 
 

1. Writing a program that cannot be killed except by reboot

Is there anyway to block ALL signals to a program so that it
may not be killed by kill???

I know the manual says that sig 9 may not be caught or blocked
but I was wondering if there might be a funny (strange, interesting)
hack to get around this...

thanks,
--
Lator,                                       We cheat the other guy,
                                             and pass the savings on to you.
Jeffrey B. McGough

2. using framebuffers with the intel 830

3. Allowing other computers to write to other computers drives.

4. SHMxxx defines problem

5. Question on writing c program implement cp function using UNIX system calls - read, write, etc.

6. TAR and CPIO Backup script

7. Need help writing C program using UNIX system calls (read, write, etc) that copies files

8. Is it possible to emulate an IBM RS/6000 running AIX 4.3.2 on a Windows 2000 Server?

9. reboot error reboot error reboot error

10. Writing drawing program for Linux -- program behaving incorrectly

11. How to write a C-shell program to print return value of a program

12. Compaq 6000 reboots and reboots and reboots

13. Installation: 3 distributions reboot my computer! :/