LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-22-2013, 11:54 AM   #1
jimtrenton
Member
 
Registered: Oct 2007
Distribution: Slackware 14.0, 14.1
Posts: 37

Rep: Reputation: 10
script notrunning from crontab, running manually


Hello All,

I have a problem with running a script from crontab. This is the case for one of the systems I support so there is a limit to the information I can provide about it but here goes.

We have a script that is suppose to run once a day. It has been obviously put into crontab for that purpose. But it will not execute from the crontab. The exactly same line will work correctly when issued on the command line, so I know for a fact there is nothing broken with the script itself. It is actually several commands in one line semicolon separated but the first ones are working. Only the last command is not working.
All paths on the line are absolute, as this was one of the first advice that I have managed to find regarding similar problem but it has not resolved the problem.
I have also checked the environment in which it runs. It would appear that the crontab uses a different environment while running but to resolve this I have tried exporting the values from the users environment just before the command is executed(on the same line that is). This has not resolved my issue.

Can anyone please advise on what to check next as I'm running out of ideas as to why this is not working. Any troubleshooting step I could try?
Any information would be helpful.

Thanks
 
Old 08-22-2013, 12:03 PM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
with all this info you have provided no one can actually understand where the issue lies, it could be in the cron entry where you have listed it but to make sure and help you further, we on the least require some detail on how the entry looks like in the crontab ?

whenever ppl posting issues, i always suggest use proper examples if not able to post the original code.. unless you provide us something to look into !! we cant help you the way you are looking for.
 
Old 08-22-2013, 02:15 PM   #3
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 55
Without any details, I would say the first thing I'd do is redirect std out and std err to a file when running the command. That way you can see any error messages that may have come up and check if the output to std out is as expected.

Also, did you check to see if there's any relevant info in the system log?
 
Old 08-22-2013, 02:25 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
cron doenst source the $PATH variable. i think you will need to put the /full/path/to/executable in the cron entry as well as inside any script that is called by cron.
 
1 members found this post helpful.
Old 08-23-2013, 01:59 AM   #5
jimtrenton
Member
 
Registered: Oct 2007
Distribution: Slackware 14.0, 14.1
Posts: 37

Original Poster
Rep: Reputation: 10
example

Hi

Thanks for the replies.
The example I can provide is:
Code:
00 16 * * * . /home/user/config; cd /home/user/export/; /home/user2/bin/Start Update.cfg
I've not checked the system.log but the /var/log/cron would indicate the command is being executed. The config file passes some variables to the environment, $PATH among others. The main issue is with the Start script which calls the Update.cfg for details on what to do.
The exact same command works on the command line without any issues.
I have tried to pass the complete list of the env variables from a file, inline, just before changing the directory. This did not do anything either.

Yesterday we have also tried to redirect stderr to stdout and the to file. But it would have seemed from the /var/log/cron that this has reloaded the crontab but did not execute the command.

But it was late yesterday so maybe I'm just imagining things. Any additional info is more than welcome.

Thanks
 
Old 08-23-2013, 02:05 AM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
cd /home/user/export/; /home/user2/bin/Start Update.cfg
i cant understand what a cd is doing there when you have passed all commands with absolute paths and this dir does not seems to be used.
Anyways considering /home/user2/bin/Start is a bash script, can you try putting this only command preceeded with
Code:
sh [command] [arguments]
into cron entry and then see if it works.
 
Old 08-23-2013, 09:17 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
There's a space after the period which probably won't source /home/user/config.
Not sure if that is your intention or not.
 
Old 08-23-2013, 10:32 AM   #8
jimtrenton
Member
 
Registered: Oct 2007
Distribution: Slackware 14.0, 14.1
Posts: 37

Original Poster
Rep: Reputation: 10
Hello,

@Habitual

The line in the crontab actually goes like this:
Code:
00 16 * * * . config; cd /home/user/export/; /home/user2/bin/Start Update.cfg
sorry I was writing from memory.
this is what I found regarding the dot
http://www.linuxquestions.org/questi...ommand-334090/
And I believe that this is the way it is suppose to be, it has not been written by me and is working for other line in the same crontab without any issues.

@SAbhi

The cd is there to set the directory where the Update.cfg sits. Yes there is a space between Start and Update.cfg.
 
Old 08-23-2013, 10:51 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
so, you are sourcing "config" from the current directory
Which is the $HOME of the cron user

who's crontab is it?
Does /home/<cron user>/config exist?

better code
Code:
00 16 * * * . config 2>&1 && ( cd /home/user/export/ 2>&1 && /home/user2/bin/Start Update.cfg 2>&1 )
the 2>&1 's are redirecting stderr to stdout, this way you cronjob will mail you on error ( with the error )

Code:
Command && action if true || action if false
Command || action if false && ( action if true && action if true || action if false )
 
Old 08-23-2013, 10:31 PM   #10
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,283

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
In the cron entry, after you have (supposedly) set up your environment by sourcing "config" or whatever you are doing, run the "env" commmand and redirect stdout and stderr to a file. Then compare the captured output from a scheduled cron execution to your environment on the command line where things actually run (use sort and diff to do this).

When something runs correctly from command line, but not from cron (assuming the job is appropriately scheduled in cron): (1st) check your environment, (2nd) check your environment, and finally (3rd) check your environment. And if none of that turns up a clue, (4th) check your environment.

If you want to further convince yourself that it really is an environment issue, take the exact commandline that works manually, but instead of invoking it via cron, invoke it using "at", as in "at not + 5 minutes ... blah blah blah". "at" picks up your environment when you schedule the job, whereas cron gives you a minimal environment at execution time.
 
1 members found this post helpful.
Old 08-28-2013, 08:28 AM   #11
jimtrenton
Member
 
Registered: Oct 2007
Distribution: Slackware 14.0, 14.1
Posts: 37

Original Poster
Rep: Reputation: 10
Hello All,

Thank you everyone for the advice.
Thank you haertig for the voice of reason and the effective solution. I knew there were differences between the environments used in crontab and and the users. The first time I have tested exporting users env inside the crontab I must've made some mistakes with the sourcing which caused it to fail. But let's not dwell on the obvious human error sitting in front of the terminal.

The problem is resolved, the solution was to revert to the backup version of the crontab(why this wasn't the first step of the person working on this initially I don't know).
What also worked was sourcing the complete set of env variables for user's home environment but this was abandoned in favour of the backup.

Thanks again, marking as solved.
 
  


Reply

Tags
cron, crontab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Unable to shell script thru crontab but the same runs manually mukheja.nitin Linux - Newbie 12 10-11-2012 02:41 PM
crontab does not run all lines of a script, but manually it works perfect. Annielover Linux - General 7 05-23-2011 08:17 AM
Script won't run in crontab.... only manually ScorchPipe Linux - Newbie 7 03-22-2011 11:11 AM
[SOLVED] script not running via crontab, run's fine manually astroboiii Linux - Software 10 02-08-2010 11:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration