shell Bash脚本手动运行,但在crontab上失败

nnt7mjpx  于 2023-02-16  发布在  Shell
关注(0)|答案(6)|浏览(139)

我是shell脚本的新手。我写了一个shell脚本来做MySQL数据库的增量备份。这个脚本是可执行的格式,手动执行时可以成功运行,但通过crontab执行时会失败。
Crontab条目是这样的:

*/1 * * * * /home/db-backup/mysqlbackup.sh

下面是shell脚本代码-

#!/bin/sh
MyUSER="root"       # USERNAME
MyPASS="password"         # PASSWORD
MyHOST="localhost"  # Hostname
Password="" #Linux Password

MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi
MYSQLADMIN="$(which mysqladmin)"
if [ -z "$MYSQLADMIN" ]; then
    echo "Error: MYSQLADMIN not found"
    exit 1
fi
CHOWN="$(which chown)"
if [ -z "$CHOWN" ]; then
    echo "Error: CHOWN not found"
    exit 1
fi
CHMOD="$(which chmod)"
if [ -z "$CHMOD" ]; then
    echo "Error: CHMOD not found"
    exit 1
fi

GZIP="$(which gzip)"
if [ -z "$GZIP" ]; then
    echo "Error: GZIP not found"
    exit 1
fi
CP="$(which cp)"
if [ -z "$CP" ]; then
    echo "Error: CP not found"
    exit 1
fi
MV="$(which mv)"
if [ -z "$MV" ]; then
    echo "Error: MV not found"
    exit 1
fi
RM="$(which rm)"
if [ -z "$RM" ]; then
    echo "Error: RM not found"
    exit 1
fi
RSYNC="$(which rsync)"
if [ -z "$RSYNC" ]; then
    echo "Error: RSYNC not found"
    exit 1
fi

MYSQLBINLOG="$(which mysqlbinlog)"
if [ -z "$MYSQLBINLOG" ]; then
    echo "Error: MYSQLBINLOG not found"
    exit 1
fi
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y-%T")"

DEST="/home/db-backup"
mkdir $DEST/Increment_backup.$NOW
LATEST=$DEST/Increment_backup.$NOW
$MYSQLADMIN -u$MyUSER -p$MyPASS flush-logs
newestlog=`ls -d /usr/local/mysql/data/mysql-bin.?????? | sed 's/^.*\.//' | sort -g | tail -n 1`
echo $newestlog
for file in `ls /usr/local/mysql/data/mysql-bin.??????`
do
        if [ "/usr/local/mysql/data/mysql-bin.$newestlog" != "$file" ]; then
     echo $file             
     $CP "$file" $LATEST         
        fi
done
for file1 in `ls $LATEST/mysql-bin.??????`
do
 $MYSQLBINLOG $file1>$file1.$NOW.sql 
 $GZIP -9 "$file1.$NOW.sql"     
 $RM "$file1"
done
$RSYNC -avz $LATEST /home/rsync-back
  • 首先,当在crontab上进行调度时,它没有显示任何错误。我如何才能知道脚本是否正在运行?
  • 其次,在crontab中执行shell脚本的正确方法是什么?一些博客建议更改环境变量。最好的解决方案是什么

当我做$echo PATH的时候,我得到了这个

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin
tsm1rwdh

tsm1rwdh1#

问题可能是您的$PATH在手动环境中与crontab运行的环境不同。因此,which无法找到您的可执行文件。要解决此问题,请首先在手动环境(echo $PATH)中打印您的路径,然后在您在crontab中运行的脚本顶部手动设置PATH。或者仅使用完整路径引用程序。
编辑:在脚本顶部附近,所有which调用之前添加以下代码:

export PATH="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin"
rpppsulh

rpppsulh2#

另一种更通用的方法是让cron运行用户的bash登录过程。除了PATH之外,这还会拾取任何LD_LIBRARY_PATH、LANG设置、其他环境变量等。要做到这一点,请编写crontab条目的代码,如下所示:

34  12  * * *   bash -l /home/db-backup/mysqlbackup.sh
mw3dktmi

mw3dktmi3#

我的问题是我在/etc/cron. d(Centos 7)中设置了cron作业。似乎在这样做时,我需要指定执行脚本的用户,这与在用户级别输入cronjob不同。
我要做的就是

*/1 * * * * root perl /path/to/my/script.sh
*/5 * * * * root php /path/to/my/script.php

其中"root"表示我以root身份运行脚本。还需要确保在文件顶部定义了以下内容。您的路径可能不同。如果您不确定,请尝试命令"which perl","which php"。

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
wbrvyc0a

wbrvyc0a4#

只需在脚本的开头导入您的用户配置文件。
即:

. /home/user/.profile
up9lanfz

up9lanfz5#

1.在crontab文件中,脚本条目可以具有

* * * * * /bin/bash /path/to/your/script

1.确保在你的脚本中使用完整的路径。我已经看过了,我没有看到任何,但以防万一我错过了它。

vjrehmav

vjrehmav6#

首先运行命令env〉env.tmp,然后运行cat env.tmp复制PATH=.....................整行并粘贴到crontab -e中cronjobs前面的行,如下所示

++=============================================================++
(# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
PATH=/root/.nvm/versions/node/v18.12.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/sna>

# m h  dom mon dow   command
0 3 * * * bash /home/ubuntu/test.sh
* * * * * bash /home/ubuntu/test2.sh >> /home/ubuntu/cronlog.log
* 3 * * * bash /home/ubuntu/logscron.sh)
++=====================================================================++

相关问题