Solved

cron job not running as root

Posted on 2010-09-13
16
1,253 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
 
LVL 76

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 76

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 76

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now