shell脚本手动执行成功,定时任务执行脚本不成功

上一篇文档脚本手动能执行成功,crontab定时任务执行脚本不成功。

原因:cron的环境变量和shell脚本的环境变量不一样

 

解决办法:
#方法一:定时任务里面切换到root执行()
*/5 * * * * su - root -c "/data/pkgs/mysql/tools/auto_backup.sh"

#方法二:在脚本里面添加环境变量(亲测可用)
vim /data/pkgs/mysql/tools/auto_backup.sh
#!/bin/bash
source /etc/profile
source ~/.bash_profile

 

  

#!/bin/sh
# 在这修改程序名和程序所在目录,其他不用改
source /etc/profile
source ~/.bash_profile
name="carte"
path="/app/bigdata/data-integration/"
pid=`ps -A |grep $name| awk '{print $1}'`
now=`date  "+%Y-%m-%d %H:%M:%S"`
# 检测是否在运行
if [ ! $pid ]
then
	echo "$now $name is not running, start it now..."
	# 启动程序命令
	cd $path
  echo "$now $path come in..."
	nohup /app/bigdata/data-integration/carte.sh  /app/bigdata/data-integration/pwd/carte-config-master-8080.xml >> /data/bigdata/kettle/log/spoon.log 2>&1 &
	new_pid=`ps -A |grep $name| awk '{print $1}'`
	# 检测是否启动成功
else
	echo "$now $name is running, pid is $pid"
fi

定时任务可以通过cat /var/spool/mail/root查看是否生效

 

posted @ 2022-03-04 16:44  大日很忧伤  阅读(2333)  评论(0编辑  收藏  举报