Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 4 of 4

This is a discussion on CRON Jobs in the Shared & Semi-Dedicated forum
I have a cgi file that I would like to run as a cron job. What it does is update a table in a database ...

  1. #1
    DIS
    DIS is offline
    JPC Addict
    Join Date
    Sep 2001
    Posts
    188

    CRON Jobs

    I have a cgi file that I would like to run as a cron job. What it does is update a table in a database

    I have tried to set up the cron job through the control panel but it does not work. The hint says to set it up like this
    Hint: to run CGI files, enter the command as follows (modify to fit your needs):
    GET http://yourdomain.com/cgi-bin/path_to_file/file.cgi > /dev/null


    Here is what I have tried
    GET http://MyDomain/MyDirectory/MySubDirectory/inv.cgi > /dev/null

    and
    http://MyDomain/MyDirectory/MySubDirectory/inv.cgi

    and
    /usr/local/bin/perl /home/MyDomain/public_html/MyDirectory/MySubDirectory/inv.cgi

    and
    GET /usr/local/bin/perl /home/MyDomain/public_html/MyDirectory/MySubDirectory/inv.cgi > /dev/null

    this is the inv.cgi file located in MySubDirectory

    use CGI;
    use CGI::Carp qw/fatalsToBrowser/;

    use DBI;

    @days = ("Sun","Mon","Tue","Wed","Thu","Fri","Sa t");
    @months = ("Jan","Feb","Mar","Apr","May","Jun","Ju l","Aug","Sep","Oct","Nov","Dec");

    print "Content-type: text/html\n\n";

    $sc_mysql_server_name = "localhost";
    $sc_mysql_database_name = "MyDomain_Mdb";
    $sc_mysql_user_name = "Display";
    $sc_mysql_password = "Purpose";

    $sc_mysql_dsn = "DBI:mysql:$sc_mysql_database_name:$sc_m ysql_server_name";
    $sc_mysql_prd_table = "product";
    $inv_file = "./files/inv.file";

    ($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time);

    print qq~
    Start Update $hour:$min:$sec
    ~;

    $dbh = DBI->connect("dbi:mysql:$sc_mysql_database_n ame:$sc_mysql_server_name","$sc_mysql_us er_name","$sc_mysql_password") || die("Couldn't connect to database!\n");

    open (REC, "< $inv_file") || die "Cant open $inv_file";
    while (<REC>)
    {
    @data = split(/\|/, $_);
    $query = "UPDATE $sc_mysql_prd_table SET userone=\'$data[1]\' WHERE usertwo=\'$data[0]\'";
    # print "$query\n";
    $dbh->do($query);
    }
    close (REC);

    $dbh->disconnect;

    ($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time);


    print qq~
    Inventory Updated $hour:$min:$sec
    ~;


    Any Ideas where I have gone wrong

  2. #2
    JPC Senior Member
    Join Date
    Sep 2001
    Posts
    50
    2 problems,
    1. to run the scipt as a cron job, you need to call it directly "/home/mysite/public_html/cgi-bin/myscrit.cgi"
    NOT through http.

    2. your script is designed to output html to a web browser. not just to update a database. create a log file and have the script direct all output to the log instead.
    Last edited by apgraham; 07-26-2003 at 07:55 AM.
    --
    Alien Heat - Affordable Web Hosting and Design
    http://www.AlienHeat.com

  3. #3
    DIS
    DIS is offline
    JPC Addict
    Join Date
    Sep 2001
    Posts
    188
    I see.

    So if I comment out the print comment and print qq~ lines would that work?

    Soes the script have to be run from the cgi-bin to work with a cron job?

  4. #4
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    There are actually two ways to run cron jobs. The first is the direct method as explained above. The second is by using GET in the command with the URL (as explained in CPanel). I recommend the former, as it is more secure (with the latter, someone could figure out the URL of your script and run it whenever they wanted.

    If you use the direct method, the script doesn't have to be in cgi-bin. In fact, it shouldn't be in your public_html directory at all. I recommend making a bin directory off your home directory and putting the script in there. That way no one will be able to reach it from your website.

    The script should have 700 or 755 permissions set and the very first line of the script should read

    #!/usr/bin/perl

    That tells the operating system that the file is a script and should be run with the perl interpreter. That way, all you need to put into the cron command is the path to the script, such as /home/username/bin/inv.cgi.

    Adding the > /dev/null sends any output from your script to /dev/null, the black hole of Unix. Anything sent to /dev/null is simply discarded by the system, so the generated output is ignored. If you don't include that, and you don't comment out the print statements, the resultant html that the script generates will be emailed to your default email address every time the cron job runs. If you don't want the output and you comment it all out, you can omit the > /dev/null, but including it makes it easier to get the output back if you ever have an problem with the script in the future. An alternative would be to use >> /home/username/log.txt which would add the output to the bottom of a file called log.txt in your home directory.

    Hope this helped you out...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •