Link to home
Start Free TrialLog in
Avatar of SimpleJoe
SimpleJoeFlag for United States of America

asked on

cron job not running as root

Hi, this has stumped some techs. I'm on a cPanel server and added this root cron job entry using "crontab -e" --

0,15,30,45 * * * * perl /home/chat8/public_html/ftp-mon/client/ftp-mon.cgi

it runs fine when I run it from the command line, but it is not running as a
cron job every 15 minutes starting at the top of the hour as it should

I've edited other existing cron entries and that works, so I know crons are running and I know I'm editing the correct root cron file

any ideas?
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,
I think that perl is not in the standard path /usr/bin (maybe it's /usr/local/bin?)
Issue "which perl" as root and change the crontab enty accordingly, so if "which perl" would return "/usr/local/bin/perl" use
0,15,30,45 * * * * /usr/local/bin/perl /home/chat8/public_html/ftp-mon/client/ftp-mon.cgi
Also make sure that all variables are set inside ftp-mon.cgi , and that all executables are called by full path (if no PATH variable is being set inside the script).
This is because shell initialization profiles are not executed under cron.
wmp
 
also check the /var/log/cron see if there are any errors

check that there are no cron.allow or cron.deny files and if there are make sure root is allowed.
actually since you can edit cron ignore the allow deny statement
The other item you should check is whether you have Setuid on the cgi and/or whether the CGI has a set effective owner/group.

As woolmilkproc pointed out, you may not be using full paths in the cgi script and the system commands you are trying to run might not be found

you can use env - to pass the PATH to the
i.e.
your cron entry:
0,15,30,45 * * * * perl /home/chat8/public_html/ftp-mon/client/ftp-mon.cgi
will be:
0,15,30,45 * * * * env PATH=/bin:/sbin:/usr/bin:/usr/local/bin:$PATH - /usr/bin/perl /home/chat8/public_html/ftp-mon/client/ftp-mon.cgi
Avatar of SimpleJoe

ASKER

ok I figured something out, it seems the cron is running as root, what's not happening is the script is supposed to send an email when done executing, it's just the email that's not being send.

even though the path is correct in the script:

/usr/sbin/sendmail

i'm checking the mail logs...
I verified from the logs that the email sends when run from the shell, but not when run as a root cron.

any ideas?

thanks,
Joe
Please post the complete sendmail command as issued from the script!

variable defined:

$mailprog = "/usr/sbin/sendmail";


then later:

    # Open The Mail Program
    open(MAIL,"|$mailprog -t");
    print MAIL "To: $sendto_email\n";
    print MAIL "From: $sender_email\n";
    print MAIL "Subject: FTP update\n";
    print MAIL "A file was uploaded:\n\n";
    close (MAIL);


again, it works from the shell, not from cron job.
are you getting errors in the logs when the email doesn't send? Are your email variables sourced?

Are you certain the scripts when run from cron gets as far as generating the email?

Are there checks in the script that when failed, exit?
there is a command at the end of the script that I verified runs, nothing in email logs sent to the unique address I setup... will keep checking stuff
Use perl module to better handle email. It is much more robust than using sendmail. Can easily send an attachment as well and easy to send html message as well.

use MIME::Lite;
my $message = MIME::Lite->new(
   To => 'user@example.org',
   Subject => 'message',
   Data => 'Message body'
);

$message->send();

If you don't have MIME::Lite module installed you will get error. If so try installing the module by issuing following command:
# cpan -i MIME::Lite


Add the following above the Mail
just trying to see whether something goes wrong and the message is rejected.
The problem is that you are not checking whether open MAIL,"|$mailprog" actually worked.

Have you look at the emails on the system sent to root as the username under whose credentials the cron runs for error messages from cron when there was output or errors?
$date_example=localtime();
open (LOGFILE,">>/home/chat8/public_html/ftp-mon/client/ftp-mon.log") || die "Error opening file for appending: $!";
print LOGFILE <<EOF
To: $sendto_email
From: $sender_email
Subject: FTP update

A file was uploaded: $date_example
EOF
;
close (LOGFILE);

Open in new window

I tried the alternate mail method from rajendraone:, worked great from the shell but no email was sent from the cron.

In reply to arnold, I grepped the entire /var/log/* for the address it sends to, but  entries only appear when I run it from the command line, yet I know the cron job runs.

I'm beginning to think this is more related to a security setting in cron not allowing the sending of email (on a cpanel server)

will keep researching.... thanks

i changed it to a user's cron job instead of a root cron job, and it appears to maybe be working.

will post update when I know for sure
ASKER CERTIFIED SOLUTION
Avatar of SimpleJoe
SimpleJoe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial