Solved

Cron environment issues

Posted on 2014-04-29
11
349 Views
Last Modified: 2014-04-30
In Ubuntu, I am having a lot of trouble running a script that uses a .pm module from crontab. When I try to do so, I get an error that reads:

Can't locate <perl_module>.pm in @INC (@INC contains:<list_of_directories_that_do_indeed_have_the_pm_file>)

I was wondering if it would be possible to get the crontab to run the script as a specific user, in their environment. The script works perfectly from he command line...
0
Comment
Question by:stakor
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40030554
In root's crontab (not /etc/crontab!) you can do:

* * * * * su - username -c /path/to/script ...
0
 
LVL 14

Expert Comment

by:small_student
ID: 40030712
What user are you using to run the command? From that user

crontab -e

then as woolmilkporc suggested

* * * * * su - username -c /path/to/script
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40030721
Only root can do "su - username" without password.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40030738
You could call your .profile first, like this:

00 10 * * * /home/user/.profile ; /path/to/script
0
 

Author Comment

by:stakor
ID: 40031026
I tried the root path. I put in:

*/15 * * * * su -l <username> -c <command>

And, in a sureal turn of events, it is having the same error that I had in <username>'s crontab.

I am now trying the /home/<username>/.profile;<command>

idea. I'll post in about 20-25 min with how that works out.  Something is up, but I don't know what...
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:stakor
ID: 40031039
Sadly, same error.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40031110
Why "su -l" ?  It's just "su - ..." I.e. a single dash surrounded by spaces after "su".

Nothing sureal here ...
0
 

Author Comment

by:stakor
ID: 40031204
I think I fat fingered it in the beginning. I used -<username>. I tried to get around this after man'ing su, and saw -l (at least I think I saw) that -l was another valid designator. I just tried it with - <username> and that had the same problem as "-l". It still can't find the .pm file.
0
 

Author Comment

by:stakor
ID: 40031276
Ok, I have something that works. If I add:

setenv PERL_MB_OPT "--install_base /home/<USER>/perl5"
setenv PERL_MM_OPT "INSTALL_BASE=/home/<USER>/perl5"
setenv PERL_LOCAL_LIB_ROOT "/home/<USER>/perl5"
setenv PERL5LIB /home/<USER>/perl5/lib/perl5/i686-linux-gnu-thread-multi-64int:/home/<USER>/perl5/lib/perl5
setenv PATH "$PATH":/home/<USER>/etc/turfserf/etc:/home/<USER>/perl5/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/usr/games

to a csh file that calls the perl script, it works...
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 40031301
>> Ok, I have something that works.
All the env variables you've added are usually set in .profile or .profile.user

How are these env settings done for that user normally? If you just add that 'setting' script or profile to your cron entry then it should work. Alternative is what you've done, put these settings in a script and call that before running the .pm - same solution just a different implementation.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 40032159
I believe you may as well put environment variables at the top of your crontab of the user in question.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS EC2 Linux 1 55
Auto channel for WiFi (Access Point) 3 78
bash script question (chmod) 10 56
Shell script to extract rar files in severa directories 7 74
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now