sh script not executing in cron job

hello Hello,

I have a directory that i I want to backup along with a MySQL database using a sh script. if i If I ran the script manually if works fine but if i I run it through a cron job it only works partially meaning it backs up the directory but not the database. Have any idea what could be wrong with my scrip[t]? i I had set the right permissions, chmod +x backup.sh
 
Are you using absolute paths?

Are you setting the path inside the crontab?
Code:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:etc.
 
you You meant path to the sh script? here Here is how my crontab looks
Code:
*/5     *       *       *       *       root   /usr/local/scripts/ezu/backup.sh
 
Yes, but also the PATH variable, as mentioned before. In other words, when you edit the crontab, write
Code:
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
before the command. And add locations to the PATH variable as you see fit.
 
Thanks for your reply Beastie, below is my crontab patch variable seems to be different than your suggestion. I changed it to your suggested variable but not success.

Code:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin

Here is my script
Code:
#!/bin/sh
#
dbhost=localhost
dbuser=root
dbpass='password123'
dbname=mydatabase
webrootdir=/var/www/joomla
tempdir=/usr/local/backups/anew
tarnamebase=backup
email=
confdir=/usr/local/scripts/ezu/functions.sh
attach=
. $confdir

I believe my problems are permission issues. because Because if I log in as user to the machine without root rights and ran the script i get the same result as cron job. I get the result only if ran script manually and as root.
Thanks,
-Motty
 
motty said:
Thanks for your reply Beastie, below is my crontab patch variable seems to be different than your suggestion. I changed it to your suggested variable but not success.

Code:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin

It is probably the PATH, mysql normally installs under /usr/local so if you are relying upon mysql commands in your script you should include /usr/local/bin... ie:


Code:
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
 
Best thing to do is to never rely on PATH. Always use the full path to commands in scripts.
 
The key thing to know about cron(8) is that it runs with a very restricted environment. Notably, your PATH is set /usr/bin:/bin. If you want to run any commands outside of that restricted set, you will likely need to either adjust your PATH or use fully qualified paths in your scripts.

This is why when you login as a normal user and run the script, it works just fine, your PATH likely has many other components that you are unknowingly relying upon when you test the script by hand.
 
Yes, I included the PATH of mysqldump command in my script but no success.
Here is my crontab:
Code:
*/5     *       *       *       *       root   /usr/local/scripts/octs/uzb.sh

I set uzb.sh to chmod +x uzb.sh, also, my script

sh -c "/usr/local/bin/mysqldump -u root -ppassword database1 > /usr/local/backups/test/backup-`date +%y%m%d`.sql"

It works fine if I ran that manually but I can't get it to work with crontab.
 
I got it to work I edit crontab and added the line below:

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
It works fine after adding two lines above.
 
i need yur help

I have similar case where i'm running a crobtab on a script that run "df -k" and fills the data in a table on another host,

my script is as below

Code:
df -k | nawk 'NR > 1 {
$1 = $1; printf "insert into df values ( \47%s\47,", h
for (i = 0; ++i <=NF;)
printf "\47%s\47,", $i
print " current_timestamp );"
}' OFS=, h="SMS_JAR" |
mysql -h192.168.55.22 -uroot -prootp kpi



my crontab command

Code:
00 08 * * * /export/home/jinny/diskspace.sh


but it is not being executed.. i'm not very good in linux, i tried adding the below in the crontab before the related command, but i got an error

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
 
For me works doing this:

First set you password and DBDescription to a encrypted file with this command bellow:
mysql_config_editor set --login-path=DBDescription --host=DB#.pair.com --user=DBUser --port=3306 --password

See if that works:
mysql_config_editor print --login-path=DBDescription

The output will be something like this:
Code:
[DBDescription]
user = root
password = *****
host = localhost
port = 3306

Now create a shell script:

Bash:
#!/bin/sh
#Set paths
MYDUMP=/usr/local/bin/mysqldump
BACKUPDIR=/usr/local/www/apache24/data
MYGZIP=/usr/bin/gzip
MYFIND=/usr/bin/find

#Dump
$MYDUMP --login-path=DBDescription DBNAME > $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql

#GZIP the file
$MYGZIP $BACKUPDIR/mysql-DBNAME.`date '+%d-%B-%Y--%Hh'`.sql

And finally create a job with cron.
Here I use:
# nano /etc/crontab
At the final line of the file add:
Bash:
00 04 * * * root /usr/local/www/apache24/data/mybackup.sh

Based on the information of the page: link
 
allen.konstanz please look at the dates before responding. The last post in this thread was almost 5 years ago. The thread itself started three years before that (2010!).
 
Back
Top