Sam*_*man 2 cron debian shell-script
crontab奇怪的是没有执行我的脚本,尽管脚本从 shell 终端运行得很好。我已经确保脚本通过一个新的行空间彼此分开。但是,我对脚本的内容表示怀疑。crontab执行run.sh稍后将运行main.sh。
这是/etc/crontab:
* * */3 * * root source /opt/db_maintain/run.sh
Run Code Online (Sandbox Code Playgroud)
这是从内部run.sh调用的内容main.sh:
#!/usr/bin/env bash
#********* Saman *********
TM=$(date --date='40 days ago' '+%F %T')
TARGET=/opt/db_maintain/main.sh
TIMESTAMP=$(echo ${TM} | tr --delete ': -')
export TIMESTAMP
source $TARGET "$TM"
Run Code Online (Sandbox Code Playgroud)
这是开头main.sh:
#!/bin/bash
##!/usr/bin/env bash
#
# main program entry point
#
source /opt/db_maintain/functions.sh
source /opt/db_maintain/constants.sh
source /opt/db_maintain/settings.sh
source /root/PASSWD_PGRS.sh
#read -s -t 0 -n 9999
#read -s -p "Enter password for ${USERNAME}: " PASSWORD
Run Code Online (Sandbox Code Playgroud)
在 cronjob 中使用以下语法:
* * */3 * * root /bin/bash /opt/db_maintain/run.sh
Run Code Online (Sandbox Code Playgroud)
你用过source。这是一个内置命令外壳的bash。因此它只能在bashshell 内或通过bashshell 执行。crontab 只执行二进制文件,而不是 shell 命令。所以你必须调用一个二进制 ( /bin/bash),最好是通过它的绝对路径。