Linux.com

Home Linux Community Forums Linux Distributions Ubuntu Anyone Know Howto Create a KeepAlive Script?

Anyone Know Howto Create a KeepAlive Script?

Link to this post 15 May 09

Here's the basics.

0. this is a Perl script, you have been warned ;)

1. cut and paste this into a file called "mydaemon.pl", then edit...

2. define your sleep interval at the top of the script (my $sleepInt = ...)

3. the program it looks for is "foo.sh". substitute w/your process name.

4. it prints everything to STDOUT. you probably don't want this. ideally, they should go to a log, or be suppressed.

5. test before you run with:


perl -cw mydaemon.pl

6. let me know if you need help tweaking...


#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(setsid);
use Fcntl ':flock';
use FindBin qw($Script);

# define sleep interval here (in seconds)
my $sleepInt = 3;

# make output line-buffered
$| = 1;

# daemonize
open(SELFLOCK, "<$0") or die "couldn't open $0: $!\n";
flock(SELFLOCK, LOCK_EX | LOCK_NB) or die "another $Script is already running\n";
chdir('/') or die "can't chdir to '/': $!\n";
umask 0;
defined(my $pid = fork) || die "can't fork: $!\n";
exit if($pid);
setsid || die "can't start a new session: $!\n";

# eternal loop
while ( 1 ) {

# do thing here...

# eg look for pid of 'foo.sh'
chomp(my $pid = `/sbin/pidof -x foo.sh`);
if($pid){
print "foo.sh (pid $pid) is running\n";
}else{
print "foo.sh is stopped\n";
print "starting foo.sh...";

# start the command
system("foo.sh &");
}

print "sleeping $sleepInt seconds...\n";
sleep $sleepInt;
}

# you should never get here
exit(1);

Link to this post 15 May 09

atreyu wrote:

Here's the basics.

0. this is a Perl script, you have been warned ;)

1. cut and paste this into a file called "mydaemon.pl", then edit...

2. define your sleep interval at the top of the script (my $sleepInt = ...)

3. the program it looks for is "foo.sh". substitute w/your process name.

4. it prints everything to STDOUT. you probably don't want this. ideally, they should go to a log, or be suppressed.

5. test before you run with:
[code]
perl -cw mydaemon.pl
[/code]

6. let me know if you need help tweaking...

[code]
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(setsid);
use Fcntl ':flock';
use FindBin qw($Script);

# define sleep interval here (in seconds)
my $sleepInt = 3;

# make output line-buffered
$| = 1;

# daemonize
open(SELFLOCK, "<$0") or die "couldn't open $0: $!\n";
flock(SELFLOCK, LOCK_EX | LOCK_NB) or die "another $Script is already running\n";
chdir('/') or die "can't chdir to '/': $!\n";
umask 0;
defined(my $pid = fork) || die "can't fork: $!\n";
exit if($pid);
setsid || die "can't start a new session: $!\n";

# eternal loop
while ( 1 ) {

# do thing here...

# eg look for pid of 'foo.sh'
chomp(my $pid = `/sbin/pidof -x foo.sh`);
if($pid){
print "foo.sh (pid $pid) is running\n";
}else{
print "foo.sh is stopped\n";
print "starting foo.sh...";

# start the command
system("foo.sh &");
}

print "sleeping $sleepInt seconds...\n";
sleep $sleepInt;
}

# you should never get here
exit(1);
[/code]

Thanks! I will work on it when I get home and let you know how it goes!

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board