LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-15-2007, 03:59 PM   #1
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Rep: Reputation: 15
Can't get a cron job to run


So I'm a newb to cron, I've never used it before. After reading a bit online, I did the following:

ran "contrab -e" as the user I wanted to run my script as. I entered the line "*/10 * * * * zoklet.net /home/zoklet.net/zok_OTG/run", saved and quit (:wq)

When I view the log file /etc/log/crontab and it showed the script running correctly:
Code:
Dec 15 16:20:01 zoklet crond[15319]: (zoklet.net) CMD (zoklet.net /home/zoklet.net/zok_OTG/run)
Dec 15 16:30:01 zoklet crond[15389]: (zoklet.net) CMD (zoklet.net /home/zoklet.net/zok_OTG/run)
However, it's obvious that the script hasn't been run because it hasn't changed any of the things it's supposed to (script posted below). Just in case, I tried restarting crond - didn't help. I don't think it's a problem with the script, because when I run it from the command line it works just fine. Ideas?

Run Script:
Code:
perl getmail.pl > out.txt
getmail.pl:
Code:
# Script connects to said gmail account, checks for new messages
# If a new messages are found, it takes the attachment (.jpg) and posts
# it in a public_html directory on the server for public viewing
# Pretty cool, eh?

use Mail::POP3Client;
use IO::Socket::SSL;
use Email::MIME::Attachment::Stripper;
use DBI;
use strict;

# GMAIL INFO:
my $username  = 'edited'; # edit this
my $password  = 'edited';        # edit this

my $mailhost  = 'pop.gmail.com';
my $port      = '995';
my $time = time();

my $pop = new Mail::POP3Client( USER     => $username,
                                PASSWORD => $password,
                                HOST     => $mailhost,
                                PORT     => $port,
                                USESSL   => 'true',
                                DEBUG    => 0,
                             );
if (($pop->Count()) < 1) {
        print "No mail at $time\n";
        exit;
}

# SQL INFO:
my $database = "edited";
$port = "3306"; # overwrites
$password = "edited"; # overwrites
$username = "edited"; # overwrites
my $dsn = "DBI:mysql:database=$database;host=localhost;port=$port";
my $dbh = DBI->connect($dsn,$username,$password);
for(my $i = 1; $i <= $pop->Count(); $i++) {
        my $message = $pop->HeadAndBody($i);

        my $stripper = Email::MIME::Attachment::Stripper->new($message);

        my $msg = $stripper->message;
        my @attachments = $stripper->attachments;
        my $filename = $attachments[0]{ 'filename' };
        my $out = "/home/zoklet.net/public_html/otg/pics/" . $filename;
        open OUT, ">$out" or die "cannot open the file yo: $!";
        print OUT $attachments[0]{ 'payload' };
        close OUT;
        system("convert /home/zoklet.net/public_html/otg/pics/$filename -resize 640x480 /home/zoklet.net/public_html/otg/pics/thumb_$filename");
        my $q = "INSERT INTO `otg` (`date`,`filename`) VALUES ('$time','$filename')";
        my $sth = $dbh->prepare($q);
        $sth->execute();
}
$pop->Close();
exit;
Code:
[zoklet.net@zoklet zok_OTG]$ ls -al
total 448
drwxrwxr-x  2 zoklet.net zoklet.net   4096 Dec 15 16:55 .
drwx--x--x 24 zoklet.net zoklet.net   4096 Dec 15 16:55 ..
-rw-rw-r--  1 zoklet.net zoklet.net   1594 Dec 15 12:25 getmail.pl
-rw-rw-r--  1 zoklet.net zoklet.net      0 Dec 12 15:51 out.txt
-rwxrwxr-x  1 zoklet.net zoklet.net     26 Dec 15 12:22 run
 
Old 12-15-2007, 04:39 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,336

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
The most likely cause of this problem is that perl may not be on cron's PATH. Try giving the full path names for the perl command and getmail.pl in your script.

------------------
Steve Stites
 
Old 12-15-2007, 04:43 PM   #3
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Original Poster
Rep: Reputation: 15
I just edited the file now. We'll see what happens in 6 minutes.
 
Old 12-15-2007, 04:51 PM   #4
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Original Poster
Rep: Reputation: 15
Dec 15 17:50:01 zoklet crond[16235]: (zoklet.net) CMD (zoklet.net /home/zoklet.net/zok_OTG/run)

out.txt is still empty and the mail has yet to be downloaded, no luck :/
 
Old 12-15-2007, 05:21 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try to redirect standard error to standard output, otherwise if your script generates some errors you don't catch them. So you can simply add redirection to the perl commandline
Code:
perl getmail.pl > out.txt 2>&1
Also check the system mail (I mean that one accessed by the command mail) to see if the cron daemon sent some message (it happens when the cron job generates output or error that have not been redirected anywhere).
 
Old 12-15-2007, 05:34 PM   #6
hbar
Member
 
Registered: Dec 2007
Location: Canada
Distribution: Debian
Posts: 50

Rep: Reputation: 15
I think I know what it is. Cron is picky and usually doesn't like file extensions. Remove the ".pl" from the filename (leaving just "getmail"). Then you need to put this line (shebang) as the very first line of the file (before the comments):
Code:
#!/usr/bin/env perl -w
Also might want to ensure that the script has exec permission:
Code:
chmod +x getmail
 
Old 12-15-2007, 06:53 PM   #7
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Original Poster
Rep: Reputation: 15
I added the shebang to my perl file, removed the .pl from the end of the file name, and added 2>&1 to my run file. None of those seemed to work :/

out.txt is still empty, the script hasn't been run, there are no errors in the cron log file, and there is no mail for the users zoklet.net or root.

Last edited by derzok; 12-15-2007 at 06:57 PM.
 
Old 12-15-2007, 07:30 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Unless you have not previously touched (that is created an empty) out.log, the script has run. Anyway, you can

1. verify that the cron daemon works properly (and that particular user is allowed to run cron jobs) by testing with a simple crontab, like
Code:
* * * * * /bin/echo $HOME >> $HOME/test.log
2. if previous test is ok, you can do a backup copy of the perl script, substitute it with another simple perl script that prints something out, remove the redirection to out.log (into the run script) and restore the crontab as this
Code:
MAILTO=zoklet.net
*/10 * * * * /home/zoklet.net/zok_OTG/run
3. if none of the above works, have a cup of coffee and post here again!
 
Old 12-15-2007, 07:55 PM   #9
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Original Poster
Rep: Reputation: 15
The first test did work, I'm trying the second now. What does the MAILTO= line do? Will that send any error messages to my system mail? Also, will the perl script output to the console if it's run by cron? Either way, I'll sit and wait another 5 minutes to see what happens.

Edit: No output:
Code:
[zoklet.net@zoklet zok_OTG]$ date
Sat Dec 15 20:59:40 EST 2007
[zoklet.net@zoklet zok_OTG]$ date
Sat Dec 15 21:00:06 EST 2007
Edit #2: I just realized that my run script is not using an absolute path to the perl script. I'll edit that change in for any further tests.

Last edited by derzok; 12-15-2007 at 08:03 PM.
 
Old 12-15-2007, 08:11 PM   #10
derzok
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Debian, Slackware
Posts: 58

Original Poster
Rep: Reputation: 15
Problem solved! It turned out to be the absolute path that was causing problems. That makes a lot of sense, now that I think about it. Thanks for all of your help, guys!
 
Old 12-16-2007, 04:00 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I assumed that it was not an issue, since jailbait correctly suggested the solution in post #2 and you told to have followed his advice in post #3...! Anyway, regarding the MAILTO question, here is an excerpt from "man 5 crontab":
Quote:
In addition to LOGNAME, HOME, and SHELL, cron(8) will look at MAILTO if it
has any reason to send mail as a result of running commands in ``this'' crontab. If
MAILTO is defined (and non-empty), mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=""), no mail will be sent. Otherwise mail is sent to the owner of the crontab.
Test it by sending output to nowhere (indeed, any output from the cron job cannot be sent to any console - if not explicitly coded in your scripts) and look at the user's system mail (or to the mail specified by the MAILTO variable). Cheers!

Last edited by colucix; 12-16-2007 at 04:01 AM.
 
  


Reply

Tags
cron



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script does not run in cron job kashyapvirgo Linux - General 8 03-20-2007 10:55 AM
unable to run cron job fahad26 Linux - General 3 06-30-2005 01:51 AM
how to run this cron job ashley75 Linux - General 5 05-24-2004 11:20 AM
Did my Cron job run? ryedunn Linux - Newbie 2 02-25-2004 08:59 AM
Cron job does not run brentos Linux - General 6 12-12-2003 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration