cron 作业不会运行 bash 脚本 [重复]

问题描述 投票:0回答:1

我使用这个 ansible cron 将脚本推送到主机。我注意到 cron 任务将运行但脚本不会运行。但如果我从 CLI 运行脚本,它会运行良好。 拜托,我需要帮助解决这个问题。 我必须在“作业”参数上做什么才能启动脚本? 即使脚本在 CLI 上运行,docker compose 命令也不会运行? docker-compose up/down 命令有问题吗?

  ansible.builtin.cron:
    name: daily apt upgrade
    minute: "0"
    hour: "3"
    weekday: "6"
    job: "sh /opt/apt_upgrades/update.sh | tee > /opt/apt_upgrades/log/upgrade.txt 2>&1"
    state: present

这是 bash 脚本 (

/opt/apt_upgrades/update.sh
)

#!/bin/bash
cd /opt/docker-compose/; #does not run
docker-compose down;   #does not run
sudo apt update && sudo apt full-upgrade -y && sudo apt autoremove && sudo apt autoclean | tee > /opt/apt_upgrades/log/upgrade.txt 2>&1;
sleep 5; 
echo "Current time: $(date +%T)";
cd /opt/docker-compose/;  #does not run
docker-compose up --detach;   #does not run

我试过对ansble进行更改

 ansible.builtin.cron:
    name: daily apt upgrade
    minute: "0"
    hour: "3"
    weekday: "6"
    job: "./root/.bashrc; /opt/apt_upgrades/update.sh | tee > /opt/apt_upgrades/log/upgrade.txt 2>&1"
    state: present
bash ansible cron-task
1个回答
0
投票

语法错误。 请用下面的替换第一个脚本中的那个。

sh /opt/apt_upgrades/update.sh |& tee /opt/apt_upgrades/log/upgrade.txt

如果该命令由于 bash 版本而无法运行,请尝试

sh /opt/apt_upgrades/update.sh 2>&1 | tee /opt/apt_upgrades/log/upgrade.txt

© www.soinside.com 2019 - 2024. All rights reserved.