Jump to content

[Answered] The Proper Way To Use Cron With Perl


mkk

Recommended Posts

Hi there,

 

starting a perl script with cron seems to be a problem for not only me... at least according to google.

In another post, it was recommend to start perl scripts either as CGI or with cron.

I would prefer cron but cannot get it to work.

 

Supposing my script is "script.pl" and is stored in "/home/mkk/www":

How would I set up a cron job for it?

 

I would also be interested in hearing about a way to debug cron: Where are the errors/warning stored when a cron job died?

 

By the way:

The script I am talking about does something, sleeps 10sec., does something, sleeps... and this is being done in an endless while-loop.

Is that a problem for your server?

 

Thanks for your help.

Link to comment
Share on other sites

Maybe you deleted it after you posted but that file doesn't exist:

root@stevie [/home/mkk/public_html/cgi-bin]# ls -la
total 20
drwxr-xr-x 2 mkk mkk    4096 Sep 11 09:16 ./
drwxr-x--- 3 mkk nobody 4096 Sep 10 22:56 ../
-rw-r--r-- 1 mkk mkk    6339 Sep 10 22:57 einsatzsms.cgi
-rw-r--r-- 1 mkk mkk    1168 Sep 10 23:49 test.cgi

Also, looking at those two .cgi files you do have there, you need to make sure your scripts have the proper shebang line at the beginning of the file. If the code is perl then the shebang should look something like:

#!/usr/bin/perl

Link to comment
Share on other sites

As stated in my initial question, I am looking for a way to execute a perl script with cron.

Your suggestion was to execute it as CGI, which I did despite the fact that I do not understand why I should do so.

 

I have a perl script named myScript.pl (filename made up) and I want to execute this script.

You do not provide SSH so cron seems like a possible workaround.

 

My question was simply: What's the command to put in the cron job to have a script being executed which is stored in "/home/mkk/www"?

 

To answer your questions:

- No, I cannot execute the script using my browser. I get ERROR 500. Could be because it's not a CGI script

- The /bin/sh has only been added in the email.

 

By the way:

 

My script starts with:

BEGIN {

my $b__dir = (-d '/home/mkk/perl'?'/home/mkk/perl':(/&--#62;/&--#62; getpwuid($&--#62;) )[7].'/perl');

unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux',map { $b__dir . $_ } @INC;

}

Right after that, my code starts...

Link to comment
Share on other sites

If you can't execute the script from your browser, neither can cron. Make sure your script will execute first and then add it to cron.

 

As stated in my initial question, I am looking for a way to execute a perl script with cron.

Your suggestion was to execute it as CGI, which I did despite the fact that I do not understand why I should do so.

 

It doesn't matter if the script ends in .cgi or .pl as long as it's a cgi/perl script. I only ended my example in .cgi because that was from one of my cron jobs. Go ahead and end yours with .pl

Link to comment
Share on other sites

I hate it!

I cannot even get the easiest script to run.

Here's the code:

 

#!/usr/bin/perl
BEGIN {
   my $b__dir = (-d '/home/mkk/perl'?'/home/mkk/perl' getpwuid($>) )[7].'/perl');
   unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux',map { $b__dir . $_ } @INC;
}
print "Hello World!";

 

The "BEGIN {}" was given from cPanel > Perl Modules.

 

The script is located at:

http://einsatzsms.heliohost.org/cgi-bin/test2.pl

 

Whatever I try, I get error 500.

Link to comment
Share on other sites

Perl is already installed without using the module. Just code a simple hello world script like this and give it file permissions 755:

 

#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
print header; 

print "Hello World";

 

http://byrondallas.heliohost.org/cgi-bin/hello-world.cgi

 

You actually don't have to keep all cgi/perl scripts in the cgi-bin if you don't want. They'll work in any part of the public_html folder.

 

Link to comment
Share on other sites

I really like the possibility to install cpan modules automatically. 5 stars for that! Is there anything else I need to do after installing a new module before I can use it in my scripts?

 

As far as I know cpanel should tell you ever thing you need to know but you didn't need that perl module to run cgi/perl scripts on your site. The server is already set up to run those languages.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...