Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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).
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:
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
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.
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!
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.