Hello, please sign in or register
You are here: Home

Perl vs PHP

I have to run some cron jobs on my linux server, the script uses the bash function cp and chmod, etc... Which can normally be type into the command shell, additionally it uses the mysql function 'REPAIR TABLE'.

There are three different script languages i can think to that'll do this. But have only written a PHP and Perl version here, ihope to try and make this a Bash script, when i know how.

Perl

[CODE=perl.pl] #!/usr/local/bin/perl sub write_line_to_file { my($t) = @_; my $data_file = '/var/updates/log/daily.log'; open DATA, ">>$data_file" or die "can't open $data_file $!"; print DATA $t; close (DATA); print $t."\n"; } use DBI; write_line_to_file "START"; $db = 'booksbeta'; $rdir ="/mnt/igweb04data/books"; $ldir ="/var/lib/mysql/$db"; if($r = `cp -f --reply=yes $rdir/stock.* $ldir && cp -f --reply=yes $rdir/bestsellers.* $ldir && echo true`) { write_line_to_file "Successfully copied"; } if($r = `chmod 777 $ldir/stock.* && chown mysql:mysql $ldir/stock.* && chmod 777 $ldir/bestsellers.* && chown mysql:mysql $ldir/bestsellers.* && echo 1`) { write_line_to_file "Successfully changed permissions"; } $db_handle = DBI->connect("dbi:mysql:database=books;host=localhost:3360;user=root;password=") or die write_line_to_file "Couldn't connect to database: $DBI::errstr\n"; $sql = "REPAIR TABLE $db.bestsellers"; $statement = $db_handle->prepare($sql) or die write_line_to_file "Couldn't prepare query '$sql': $DBI::errstr\n"; if($statement->execute()) { write_line_to_file "Repaired table $db.bestsellers"; } else { write_line_to_file "Couldn't execute query '$sql': $DBI::errstr"; } $statement->finish; $sql = "REPAIR TABLE $db.stock"; $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errstr"; if($statement->execute()) { write_line_to_file "Repaired table $db.stock"; } else { write_line_to_file "Couldn't execute query '$sql': $DBI::errstr"; } $statement->finish; $db_handle->disconnect; write_line_to_file "FINISH"; [/CODE]

PHP

[CODE=php.php] [/CODE]

Comments

Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share