Skip to content

Instantly share code, notes, and snippets.

@muthuishere
Last active August 29, 2015 14:04
Show Gist options
  • Save muthuishere/c6e2a2015146fdb0ac54 to your computer and use it in GitHub Desktop.
Save muthuishere/c6e2a2015146fdb0ac54 to your computer and use it in GitHub Desktop.
A Dameon Script runnier and Log Rotator
YOUR_DIR=/opt/xxx
SCRIPT_NAME=daemonrunner.sh
LOG_FILE=daemonrunnerLog.log
cd $YOUR_DIR
if [ $# -ne 1 ]
then
echo "Usage: $SCRIPT_NAME -start|-stop"
exit 1
fi
if [ X$1 = X-start ]
then
if [ ! -d logs ]
then
mkdir logs
fi
TMP=/tmp/ps_$$
/usr/ucb/ps -wwaux > $TMP
if [ `grep -c "$SCRIPT_NAME -daemon" $TMP` -lt 1 ]
then
echo "Starting \"$SCRIPT_NAME\""
nohup $SCRIPT_NAME -daemon 2>&1 | logmgr.pl $LOG_FILE 1000000 4 &
fi
rm -f $TMP
exit 0
fi
if [ X$1 = X-stop ]
then
TMP=/tmp/ps_$$
/usr/ucb/ps -wwaux > $TMP
for p in `grep "$SCRIPT_NAME -daemon" $TMP | nawk '{ print $2 }'`
do
echo "Stopping $SCRIPT_NAME -daemon (PID=$p)"
kill -9 $p
done
rm -f $TMP
exit 0
fi
if [ X$1 = X-daemon ]
then
echo "`date`: Starting App"
#Write your code to execute here
echo "`date`: Exiting App"
done
fi
#!/usr/bin/perl
#
# logrotator.pl
#
#
$|=1;
if($#ARGV!=2)
{
print STDERR "Usage: logmgr.pl logfile max_bytes num_files\n";
exit(1);
}
$name=$ARGV[0];
$max_bytes=$ARGV[1];
$num_files=$ARGV[2];
&roll_files();
&do_open();
while(<STDIN>)
{
print OUT "$_";
$l=length($_);
$t+=$l;
if($t>=$max_bytes)
{
close(OUT);
&roll_files();
&do_open();
}
}
exit(1);
sub roll_files
{
$rm_file="${name}."."$num_files";
if(-e $rm_file)
{
unlink("$rm_file");
}
for($i=$num_files;$i>0;$i--)
{
$to_fn=$name.".".$i;
if(($i-1)==0)
{
$fr_fn=$name;
}
else
{
$fr_fn=$name.".".($i-1);
}
if(-e $fr_fn)
{
system("mv $fr_fn $to_fn");
}
}
}
sub do_open
{
open(OUT,"> $name") || die "Cannot open \"${name}\" for writing: $!";
select(OUT); $|=1; select(STDOUT);
$t=0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment