1

I have a Ubuntu Server 10.

At the root cron I have a script to run every day at the same hour.

Running the script manually it's run fine, but in the cron it doesn't run.

At the syslog I got this:

Aug 23 09:22:01 database CRON[6884]: (root) CMD (./bkp.sh >> /tmp/bkp.log)
Aug 23 09:22:01 database CRON[6883]: (CRON) error (grandchild #6884 failed with exit status 2)

I found the possible source of the problem, I added some LOGs (echo) in the script...

During script execution, it exits on a "function" definition...

It seems that I have no access to command "function"

How can I solve this?

Beto Neto
  • 3,962
  • 7
  • 47
  • 81
  • 2
    The cron environment isn't the same as your login shell environment. System environment variables are different for one, like PATH and library search path. Your script is probably using something that's not available when ran from cron. – Torp Aug 23 '12 at 12:36
  • How can I solve this? Can I specify the user for run the job? – Beto Neto Aug 23 '12 at 12:39
  • See also http://stackoverflow.com/questions/819944/proper-way-to-run-a-script-using-cron – tripleee Aug 23 '12 at 12:43
  • Another think... recently I have tryied to change the su password with "sudo su" and "passwd".... after run this commands, I noticed that had no effect.... can this be the cause of my cron job problem? – Beto Neto Aug 23 '12 at 12:44
  • Doesn't sound like a programming problem to me. This is a job for Server Fault. – Rob Kennedy Aug 23 '12 at 14:30

3 Answers3

0

Shell scripts launched by cron must carefully specify the environment that they're running in. Shells run within cron will be non-interactive, meaning that they will not read /etc/profile, where $PATH is typically set. This means that you will need to specify the paths to all executables called in your script.

If you run a lot of cron scripts, it may be worth your time to write a small shell script which will define and export environment variables to be used inside your script, starting with $PATH.

Barton Chittenden
  • 4,238
  • 2
  • 27
  • 46
0

Here's a link i got while googling for 'cron environment variables'. It may help. There are many others like it :)

Torp
  • 7,924
  • 1
  • 20
  • 18
  • The cron was working 100%, but.... Recently I have tryied to change the su password with "sudo su" and "passwd"... after run this commands, I noticed that had no effect the su password continues the same... I ignored this and now I having problems with it... – Beto Neto Aug 23 '12 at 13:36
0

Place you script inside following directory then it should run everyday.

/etc/cron.daily/
Satish
  • 16,544
  • 29
  • 93
  • 149