chrisrth
chrisrth

Reputation: 1212

Calling script from shell script - getting command not found

I am new to shell scripting. I am trying to work through this.

> script to execute in cron (util.sh)
#!/bin/sh
HOST='ahostname'
PORT='3306'
USER='auser'
PASS='apassword'
DB='adatabase'
. /mnt/stor/backups/backup.sh

(I also tried source /mnt/stor/backups/backup.sh)

 > script to execute (backup.sh)

When backup.sh is called (it does get called) it appears to simply be parsed and not executed. So no matter what I put in it I get messages like:

/mnt/stor/backups/backup.sh: line 8: date: command not found
/mnt/stor/backups/backup.sh: line 8: mysqldump: command not found
/mnt/stor/backups/backup.sh: line 8: tar: command not found
/mnt/stor/backups/backup.sh: line 8: rm: command not found

The idea is to have a domain localized file, execute it with variables, and call a master script that uses the variable to do the dirty work. Because of limitations with one of my hosts and multiple domains this is the best method.

Upvotes: 0

Views: 7003

Answers (2)

theglauber
theglauber

Reputation: 29595

When running from cron, you can't rely on any variables that are normally in your login shell's profile (e.g.: PATH, CLASSPATH, etc). You have to set explicitly what you need. In your case, i'm guessing that it's the lack of a PATH variable that's causing your troubles.

It's also good practice to put full paths to the programs you're executing from an unattended script, just to make sure you really are going to run that specific command, i.e., don't rely on the path.

So instead of

date

for example, use

/bin/date

etc.

Upvotes: 1

Mithrandir
Mithrandir

Reputation: 25337

The script with the Problem seems to be /mnt/stor/backups/backup.sh. Try setting the PATH to include all the usual directories with binaries, so the script can find its tools. Or, even better, change /mnt/stor/backups/backup.sh and use absolute paths in the commands like /bin/rm instead of just 'rm'.

Upvotes: 1

Related Questions