I've written a Perl script (db2sql.pl) which reads records in a flat ASCII database and adds them to a MySQL database using the Perl DBI.

In my cron file, I have a line which runs this script every five minutes:

1,6,11,16,21,26,31,36,41,46,51,56 * * * * cgi-bin/dbman/db2sql.sh

(I've also tried renaming this db2sql.pl - it doesn't make a difference what I call it).

But it never produces any results.

The strange thing is that if I logon to my site via SSH and run the same script, it does what it's supposed to do. The records are added to the MySQL database.

The script permissions are 755, like all my Perl scripts.

As I say -- it runs fine from the command line, but not via cron. Any ideas?

I'm pasting the entire code below, with the MySQL details blacked out.

Thanks very much.


#!/usr/bin/perl use DBI; $state = " "; open (FILE1, "default.db"); @records = <FILE1>; close(FILE1); $db = DBI->connect('dbi:mysql:dbxxxxxxxx:dbxxx.oneandone.co.uk','dboxx +xxxxxx','xxxxxx'); my $sth = $db->prepare("insert into news(url, header, country, country +2, date, priority, userid, source, formoreinfo, language, regreqd, ke +ywords, image, actnowcampaigncode, state) values(?,?,?,?,?,?,?,?,?,?, +?,?,?,?,?)"); open(COUNTER,"db2sql.count"); @counters = <COUNTER>; close(COUNTER); foreach $count (@counters) {$rc = $count;} foreach $record (@records) { @fields = split(/\|/,$record); $rr++; # counts records read in default.db if ($rr > $rc) { # only reads new records if ($fields[5] > 0) { &convdate; &convlang; $sth->execute($fields[0], $fields[1], $fields[2], $fields[3], +$date, $fields[5], $fields[6], $fields[7], $fields[8], $language, $fi +elds[10], $fields[11], $fields[12], $fields[13], $state); } } } $db->disconnect(); open(COUNT, ">db2sql.count"); # Keeps a record of last item added to S +QL DB print COUNT "$rr"; close(COUNT); sub convdate { @dfields = split(/-/,$fields[4]); # Convert date to yyyymmdd ($dd) = $dfields[0]; ($month) = $dfields[1]; ($yyyy) = $dfields[2]; if ($month eq "Jan") {$mm = "01"}; # Get month number if ($month eq "Feb") {$mm = "02"}; if ($month eq "Mar") {$mm = "03"}; if ($month eq "Apr") {$mm = "04"}; if ($month eq "May") {$mm = "05"}; if ($month eq "Jun") {$mm = "06"}; if ($month eq "Jul") {$mm = "07"}; if ($month eq "Aug") {$mm = "08"}; if ($month eq "Sep") {$mm = "09"}; if ($month eq "Oct") {$mm = "10"}; if ($month eq "Nov") {$mm = "11"}; if ($month eq "Dec") {$mm = "12"}; $dd = sprintf("%02d", $dd); $date = "$yyyy$mm$dd"; } sub convlang { ($languagename) = $fields[9]; $language = ""; # blanks out the field first if ($languagename eq "English") {$language = "en";} if ($languagename eq "Indonesian") {$language = "id";} if ($languagename eq "Chinese") {$language = "zh";} if ($languagename eq "Danish") {$language = "da";} if ($languagename eq "German") {$language = "de";} if ($languagename eq "Spanish") {$language = "es";} if ($languagename eq "Esperanto") {$language = "eo";} if ($languagename eq "French") {$language = "fr";} if ($languagename eq "Italian") {$language = "it";} if ($languagename eq "Creole") {$language = "cpf";} if ($languagename eq "Dutch") {$language = "nl";} if ($languagename eq "Norwegian") {$language = "no";} if ($languagename eq "Polish") {$language = "pl";} if ($languagename eq "Portuguese") {$language = "pt";} if ($languagename eq "Russian") {$language = "ru";} if ($languagename eq "Finnish") {$language = "fi";} if ($languagename eq "Swedish") {$language = "sv";} if ($languagename eq "Turkish") {$language = "tr";} }

In reply to Script runs from command line but not via crontab by ericlee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.