Forum Moderators: coopster & phranque

Message Too Old, No Replies

Cron Job

         

reubensant

8:30 am on Jul 13, 2005 (gmt 0)

10+ Year Member



I have a perl script which I need to execute every 5 minutes. Basically it polls some data from MySQL and sends it to another server. I am also using GnuPG. When I execute the script using shell or ssh or call it via http, there are no problems. Everything works fine. But when i add a cron job:

perl myscript.pl > output.log

I got some errors when "require"ing some files. I entered the absolute address "/home/mydir/myscript.pl" for the require command and it worked. The problem is now with GnuPG. I am calling a perl module:

use lib qw ( /home/mydir );
use mymodule;

which happens to be in the same directory as the perl script. This module calls GnuPG:

use GnuPG::Interface;

This module also contains a line:

my $gpg_dir = "/home/mydir/.gnupg"

to locate the gpg keys.

When the cron job is being executed i am getting an error:

exec() error: No such file or directory at /usr/local/lib/perl5/site_perl/5.8.6/GnuPG/Interface.pm line 323.

Interface.pm exists in that directory. I never touched that file. Why am i getting an error in this file only when running a cron job?

Romeo

12:07 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Hi Reubensant,

one reason may be that the cron-user may have another set of environment variables -- like paths -- than you have as user when running the script in your own shell.

Regards,
R.

reubensant

1:25 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



as far as i know, the same user which is running HTTP is the same user running the cron job. how can i check that?

reubensant

2:45 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



I mentioned that the script runs ok using SSH. Actually, I run the script as 'root' in ssh.

So maybe I need to login as root before. How can I run a cron job as root?

Romeo

3:27 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



if you run the script under root, it may depend, how you became root: per native login, or by using su from another user login. If you did just a 'su', you keep the user's environment, if you 'su -', you work under root's login environment.
You can try to compare the environments and add a missing path to the root's environment, if necessary.
Or you may check if you need to run that script as root or could swith to user instead
su - user -c "<your-script>"

I hope this may address your problem -- I am not sure, though.

To run cron as root, just start with "crontab -e" under root login. This will get you a root crontab.

Regards,
R.