For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > script won't write to file ...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author script won't write to file ...
Gregory Machin

2007-07-20, 3:59 am

Hi
I have a script that i'm working on, I want it to write info to a log
file, but I can't get it to write to the file.. I run the script as
root, and I also chmod 0777 the file but still no out put ...
what have it missed ..

___script___


#!/usr/bin/perl
print "hello";

#### use
use strict;
#package ifwatch;

### global vars
my $primaryif = "/dev/ttyACM0";
my $secondaryif = "/dev/ttyACM1";
my $primaryconfig = "/etc/ifwatch/ACM0";
my $secondayconfig = "/etc/ifwatch/ACM1";
my $currentif;
my $mainloopsleep = 10;
my $LOG_DIR = "/var/log";

&daemonize();
&main();

#### process handeling kill and restart call
$SIG{INT} = sub
{
print STATUS_LOG localtime() . " Exiting...\n";
close (DEBUG_LOG);
close (ERROR_LOG);
exit;
};

$SIG{HUP} = sub
{
print STATUS_LOG localtime() . " Restarting...\n";
exec ($0, @ARGV) or die "Could not restart: $!\n";
close (STATUS_LOG);
close (STATUS_LOG);
exit;
};




sub ifcheck{
#my $files = "/dev/xtty0";
#Bunless (open(TESTFILE, $files)){
#die ("File does not exist");
#}
}


##### daemonize
sub daemonize {

# use POSIX qw(setsid);
open (STDIN, "/dev/null");
open (STATUS_LOG, ">> /var/log/ifwatch");
# defined(my $pid = fork) or die "Can't fork: $!";
# exit if $pid;

}

#### main loop
sub main {

print STATUS_LOG localtime() . " hello ";

my $loop = 1;
while ($loop >= -1)
{
print STATUS_LOG localtime() . " sleeping\n";
sleep $mainloopsleep;
}

print STATUS_LOG localtime() . " ifwatch exiting";
close (STATUS_LOG);
}


1
;





--
Gregory Machin
gregory.machin@gmail.com
www.linuxpro.co.za
Chas Owens

2007-07-20, 3:59 am

On 7/20/07, Gregory Machin <gregory.machin@gmail.com> wrote:
> Hi
> I have a script that i'm working on, I want it to write info to a log
> file, but I can't get it to write to the file.. I run the script as
> root, and I also chmod 0777 the file but still no out put ...
> what have it missed ..

snip

Simplify, simplify, simplify.


#!/usr/bin/perl

use strict;
use warnings;
use IO::File;

my $wait = 10;
my $logfile = "/var/log/ifwatch";
my $continue = 1;

open my $log, ">>", $logfile
or die "could not open $logfile:$!";

$log->autoflush(1);

#### process handeling kill and restart call
$SIG{INT} = $SIG{TERM} = sub {
print $log localtime() . " Exiting...\n";
$continue = 0;
};

$SIG{HUP} = sub {
print $log localtime() . " Restarting...\n";
exec ($0, @ARGV) or die "Could not restart: $!\n";
};

open STDIN, "<", "/dev/null"
or die "could not redirect stdin: $!";

print $log localtime() . " ifwatch starting\n";
while ($continue) {
#do stuff
print $log localtime() . " sleeping\n";
sleep $wait;
}
print $log localtime() . " ifwatch exiting\n";
Gregory Machin

2007-07-20, 3:59 am

Ok thanks, for the advise, 2 questions - what did i do wrong in my
original code, cause it's based on a working example, and is IO::File
included in the default perl package ? is this script has to go on an
embedded device with as min dependencies as possible ?



Many Thanks

On 7/20/07, Chas Owens <chas.owens@gmail.com> wrote:
> On 7/20/07, Gregory Machin <gregory.machin@gmail.com> wrote:
> snip
>
> Simplify, simplify, simplify.
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use IO::File;
>
> my $wait = 10;
> my $logfile = "/var/log/ifwatch";
> my $continue = 1;
>
> open my $log, ">>", $logfile
> or die "could not open $logfile:$!";
>
> $log->autoflush(1);
>
> #### process handeling kill and restart call
> $SIG{INT} = $SIG{TERM} = sub {
> print $log localtime() . " Exiting...\n";
> $continue = 0;
> };
>
> $SIG{HUP} = sub {
> print $log localtime() . " Restarting...\n";
> exec ($0, @ARGV) or die "Could not restart: $!\n";
> };
>
> open STDIN, "<", "/dev/null"
> or die "could not redirect stdin: $!";
>
> print $log localtime() . " ifwatch starting\n";
> while ($continue) {
> #do stuff
> print $log localtime() . " sleeping\n";
> sleep $wait;
> }
> print $log localtime() . " ifwatch exiting\n";
>



--
Gregory Machin
gregory.machin@gmail.com
www.linuxpro.co.za
Chas Owens

2007-07-20, 3:59 am

On 7/20/07, Gregory Machin <gregory.machin@gmail.com> wrote:
> Ok thanks, for the advise, 2 questions - what did i do wrong in my
> original code, cause it's based on a working example, and is IO::File
> included in the default perl package ? is this script has to go on an
> embedded device with as min dependencies as possible ?

snip

I don't know what you did wrong, there were far to many commented out
lines and other complexities in your original code. That is why I
simplified. You could try comparing the two sets of code. Most
likely you are trying to write to the file before opening it, you are
never reaching the code the prints to the file (you seem to have
called main() too early), or the open is failing (you aren't printing
the error message, so you have no way of knowing).

IO::File is part of IO which has been in core Perl since 5.003_07 or
earlier (that is the earliest release I could find in CPAN).
John W. Krahn

2007-07-20, 7:02 pm

Gregory Machin wrote:
> Hi


Hello,

> I have a script that i'm working on, I want it to write info to a log
> file, but I can't get it to write to the file.. I run the script as
> root, and I also chmod 0777 the file but still no out put ...
> what have it missed ..
>
> ___script___
>
>
> #!/usr/bin/perl
> print "hello";
>
> #### use
> use strict;


You should also use the pragma:

use warnings;


> #package ifwatch;
>
> ### global vars
> my $primaryif = "/dev/ttyACM0";
> my $secondaryif = "/dev/ttyACM1";
> my $primaryconfig = "/etc/ifwatch/ACM0";
> my $secondayconfig = "/etc/ifwatch/ACM1";
> my $currentif;
> my $mainloopsleep = 10;
> my $LOG_DIR = "/var/log";
>
> &daemonize();
> &main();
>
> #### process handeling kill and restart call
> $SIG{INT} = sub
> {
> print STATUS_LOG localtime() . " Exiting...\n";


You should probably print an error message to determine why it isn't printing
to the log file:

print STATUS_LOG localtime() . " Exiting...\n" or warn "Cannot print to the
log file: $!";


> close (DEBUG_LOG);
> close (ERROR_LOG);
> exit;
> };
>
> $SIG{HUP} = sub
> {
> print STATUS_LOG localtime() . " Restarting...\n";
> exec ($0, @ARGV) or die "Could not restart: $!\n";
> close (STATUS_LOG);
> close (STATUS_LOG);
> exit;


exec() ends the current process and starts a new one so the two closes and the
exit will never execute.


> };
>
>
>
>
> sub ifcheck{
> #my $files = "/dev/xtty0";
> #Bunless (open(TESTFILE, $files)){
> #die ("File does not exist");
> #}
> }
>
>
> ##### daemonize
> sub daemonize {
>
> # use POSIX qw(setsid);
> open (STDIN, "/dev/null");
> open (STATUS_LOG, ">> /var/log/ifwatch");


You should *ALWAYS* verify that the files opened correctly:

open STDIN, '<', '/dev/null' or die "Cannot open '/dev/null' $!";
open STATUS_LOG, '>>', "$LOG_DIR/ifwatch" or die "Cannot open
'$LOG_DIR/ifwatch' $!";


> # defined(my $pid = fork) or die "Can't fork: $!";
> # exit if $pid;
>
> }




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com