LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-29-2007, 12:26 PM   #1
WingnutOne
Member
 
Registered: Sep 2007
Location: Kansas City
Distribution: Mixed, mostly RH / Fedora
Posts: 76

Rep: Reputation: 16
Question Works from command line but not from the cron


Running RH 7.3, with a bash shell:

I've got a command (binary) called "ipstorxray" that's used inside of a script. From a command line, both the script and the separate command run successfully, but the script errors out if I try to run it with a cron job. In both cases, the command includes the full path to the binary.
Since it does run correctly from the command line, I don't think it's an issue with the command itself, but rather that there's something present in the shell that's not there for the cron job. None of my attempts to feed environment variable definitions into the script has fixed the problem though, so there has to be something else going on that I don't know about. More details below...

The command being issued is:
/usr/local/ipstor/bin/ipstorxray

The error message I'm getting is:
/usr/local/ipstor/bin/ipstorxray: error while loading shared libraries: libfsnxml.so: cannot open shared object file: No such file or directory


I looked for the libfsnxml.so file and found that it does exist at:
/usr/local/ipstor/lib/libfsnxml.so

After a lengthy set of one-at-a-time tests adding various shell environment variable definitions into the script, I got frustrated enough to run an "env" command and dump it's entire output into the beginning of my script. A select few of these env var definitions caused new errors, but the same error message was still there.

My mass adding of environment variable definitions appears to have proven that an environment variable that's present in the shell but unknown to the cron isn't the source of the problem.

What else might be causing the cron-run command to be unable to locate the /usr/local/ipstor/lib/libfsnxml.so file when it seems to have no problem finding it from a command line?

I'm stumped.
Thanks in advance!
WN1
 
Old 10-29-2007, 01:37 PM   #2
WingnutOne
Member
 
Registered: Sep 2007
Location: Kansas City
Distribution: Mixed, mostly RH / Fedora
Posts: 76

Original Poster
Rep: Reputation: 16
Exclamation

I should also mention that I did define the LD_LIBRARY_PATH in the script. It's set to:

LD_LIBRARY_PATH=/usr/local/ipstor/lib:/usr/local/ipstor/lib

(I've no idea why the same path is in there twice.)

Thanks again.
 
Old 10-29-2007, 02:04 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Is the cron run under your user or another user?
Does the cron user see the same gcc as you? (output from gcc -v)?
Are the PATH env vars the same when you're logged in vs when the cron runs? (not the lib path, but the regular path)

Can you dump env to a file as part of the cron then cross reference that file when dumped as you (I know you said you did something of the sort, though I didn't follow perfectly what you said you did) - a simple "env > foo" or "export > foo" or whatever your shell is happiest with should do the trick.
 
Old 10-29-2007, 02:12 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Whose cron job are you running the job as? Your own, or root's?
If it is root's, try using su to set up the environment as your own, before you execute the command, like this:
Code:
su  -  yourusername  -c   "/path/to/the/script"
 
Old 10-29-2007, 03:29 PM   #5
WingnutOne
Member
 
Registered: Sep 2007
Location: Kansas City
Distribution: Mixed, mostly RH / Fedora
Posts: 76

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by BrianK View Post
Is the cron run under your user or another user?
Does the cron user see the same gcc as you? (output from gcc -v)?
Are the PATH env vars the same when you're logged in vs when the cron runs? (not the lib path, but the regular path)

Can you dump env to a file as part of the cron then cross reference that file when dumped as you (I know you said you did something of the sort, though I didn't follow perfectly what you said you did) - a simple "env > foo" or "export > foo" or whatever your shell is happiest with should do the trick.

I should have included the user information the first time. I'm running as root, in a bash shell, and am using root's cron (crontab -e command).
The the gcc -v command output (the output of which, incidentally, goes to stderr) was the same from the command line and from the script.


What I'd done previously was run the env command from the command line, then manually paste it's output from the screen into the beginning of the script.

I ran an env inside the script as you suggested, and redirected it's output to a file. Most of the environment definitions that I'd placed into the script however, did NOT appear in the redirected output file.

I knew you could define $PATH in a script, but didn't realize that you couldn't define some of the other env vars the same way.

Any suggestions as to how to get a cron-executed script to absorb variables like LD_LIBRARY_PATH ?
Or is there some other way I should be approaching this?

Thanks again.

Last edited by WingnutOne; 10-29-2007 at 03:34 PM. Reason: Ed for gramatical fixes.
 
Old 10-29-2007, 04:13 PM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
So, you are executing the command as the user root. Please try running the command (from root's crontab) as I suggested in post #4, which will execute it as yourself. This should resolve any PATH or library problems.

Edit: If you are really running as root, you shouldn't be. It'll cause you big problems later (See the many threads on this board about this). But for testing purposes try:
su - root -c "/path/to/the/script"
/Edit

Last edited by tredegar; 10-29-2007 at 04:18 PM. Reason: Marked as such
 
Old 10-30-2007, 08:56 AM   #7
WingnutOne
Member
 
Registered: Sep 2007
Location: Kansas City
Distribution: Mixed, mostly RH / Fedora
Posts: 76

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by tredegar View Post
Please try running the command (from root's crontab) as I suggested in post #4, which will execute it as yourself. This should resolve any PATH or library problems.
Setting the cron to run the script by 'su - MYuserID -c /path/to/the/script' tried to work. I set the permissions on the script allow MYuserID to run it but everything else on the system, including the binaries I'm trying to run, are only available to root. (I agree that root shouldn't be running everything, but it's not my server and it's been running that way since it was built, long before I ever touched it.)

Quote:
Originally Posted by tredegar View Post
for testing purposes try:
su - root -c "/path/to/the/script"
/Edit
Setting the cron to run the script by 'su - root -c /path/to/the/script' worked; probably because it got the right environment variables. For my follow-up questions:
1. What determines the env vars for a command run from (root's) cron? and
2. What determines the env vars for a command run from the cron with an su - root -c command?
 
Old 11-07-2007, 12:20 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by WingnutOne View Post
Setting the cron to run the script by 'su - MYuserID -c /path/to/the/script' tried to work. I set the permissions on the script allow MYuserID to run it but everything else on the system, including the binaries I'm trying to run, are only available to root. (I agree that root shouldn't be running everything, but it's not my server and it's been running that way since it was built, long before I ever touched it.)



Setting the cron to run the script by 'su - root -c /path/to/the/script' worked; probably because it got the right environment variables. For my follow-up questions:
1. What determines the env vars for a command run from (root's) cron? and
2. What determines the env vars for a command run from the cron with an su - root -c command?
I know this is a bit late but...

when run from root's cron, you get the general env vars - those that are defined for all users at login which is different depending on your shell, startup scripts, etc... maybe it's the stuff in /etc/profile? maybe somewhere else?

when you su root to run the command, you get root's env - whatever is in root's .bashrc or .tcshrc (or whatever root uses).

In your script, you *should* be able to set env vars, just make sure you're setting them correctly - export for bash, setenv for tcsh. Also note that setting them in a script only makes those new setting active for that script. As soon as the script finishes, the new settings are gone unless you source the script as opposed to setting it's execute bit & just running it.
 
Old 11-08-2007, 08:18 AM   #9
WingnutOne
Member
 
Registered: Sep 2007
Location: Kansas City
Distribution: Mixed, mostly RH / Fedora
Posts: 76

Original Poster
Rep: Reputation: 16
Thumbs up

Quote:
Originally Posted by BrianK View Post
...In your script, you *should* be able to set env vars, just make sure you're setting them correctly - export for bash, setenv for tcsh...
Aaah, that's the piece I was missing. I'd been putting the environment variable definitions in the script but expected them to work while the script was running just like any other variable you might create, rather than needing to be exported before they'd work.

Thanks Brian!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command works in shell, fails as a cron task angeljusto Linux - Software 3 06-13-2007 07:06 AM
command works in shell, but not in cron bret Linux - Newbie 10 09-04-2006 02:16 AM
btdownload*.py, works from Command Line, not from cron SkipHuffman Linux - Software 11 12-30-2005 02:55 PM
works on command not on cron rickict Linux - General 1 11-17-2005 02:52 PM
Command works on cmdline, not in cron? UrbanSlayer Linux - General 6 11-06-2005 11:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration