?
Solved

cron job not running as root

Posted on 2010-09-13
16
Medium Priority
?
1,278 Views
Last Modified: 2013-11-08
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?
0
Comment
Question by:SimpleJoe
  • 7
  • 3
  • 3
  • +2
16 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33661990
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
 
0
 
LVL 11

Expert Comment

by:jgiordano
ID: 33662436
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.
0
 
LVL 11

Expert Comment

by:jgiordano
ID: 33662442
actually since you can edit cron ignore the allow deny statement
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 81

Expert Comment

by:arnold
ID: 33662873
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
0
 

Author Comment

by:SimpleJoe
ID: 33663077
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...
0
 

Author Comment

by:SimpleJoe
ID: 33663268
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
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33663342
Please post the complete sendmail command as issued from the script!
0
 

Author Comment

by:SimpleJoe
ID: 33663726

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.
0
 
LVL 11

Expert Comment

by:jgiordano
ID: 33664324
are you getting errors in the logs when the email doesn't send? Are your email variables sourced?
0
 
LVL 81

Expert Comment

by:arnold
ID: 33664971

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?
0
 

Author Comment

by:SimpleJoe
ID: 33666707
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
0
 
LVL 4

Expert Comment

by:rajendraone
ID: 33666966
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


0
 
LVL 81

Expert Comment

by:arnold
ID: 33666983
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

0
 

Author Comment

by:SimpleJoe
ID: 33684437
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
0
 

Author Comment

by:SimpleJoe
ID: 33684534

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
0
 

Accepted Solution

by:
SimpleJoe earned 0 total points
ID: 33684799
unexplicitly, now it's working from root.

perhaps setting it up as the user made some system change that kicked it into gear

it's working now. but I can't say for sure how, the last thing I did was setup a cron for the user

i since deleted the user cron and it's running, sending email and all, as root

 
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question