0

I use this ansible cron to push the script to the host. I noticed that the cron task will run but the script will not run. But if I run the script from the CLI, it will run well. Please, I need help resolving this. What do I have to do at the "job" parameter to make it start the script? Even when the script run on the CLI, the docker compose command will not run? Is there a problem with the docker-compose up/down command?

  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

Here is the bash script (/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

I have tried making changes to 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
Constantin Hong
  • 701
  • 1
  • 2
  • 16
  • What is the file permission of the shell script? – Constantin Hong Mar 09 '23 at 15:24
  • `.bashrc` file is not a program. It is an initialization file. It is something like a config file for bash. To use another `.bashrc` file, you need to use `bash --rcfile <.bashrc>`. But I don't think it would solve your problem. – Constantin Hong Mar 09 '23 at 15:30
  • 2
    I think the redirection of the stdout and stderr dont work like that with tee. – ikora Mar 09 '23 at 15:36
  • The file permission is mode: a+x – evergreen-dev Mar 09 '23 at 15:37
  • @evergreen-dev then, please, try `sh /opt/apt_upgrades/update.sh | tee /opt/apt_upgrades/log/upgrade.txt 2>&1` as @ikora suggested with your first code block. But what is the purpose of `2>&1` ? – Constantin Hong Mar 09 '23 at 15:38
  • @ikora , please how best to redirect to stderr ? Thank you – evergreen-dev Mar 09 '23 at 15:41
  • First of all... can you try to execute your .sh by hand? without cron, and use that redirection to check if it works? Before checking ansible i would suggest to check if you can run your sh EXACTLY as you are trying to do from the cronjob. Ansible is doing fine it creates your cron job, this issue is related to your .SH or your cronjob. – ikora Mar 09 '23 at 15:43
  • @Constantin Hong I can see some output in the file upgrade.txt, but the docker-compose will not stop or start. it jumps the command. – evergreen-dev Mar 09 '23 at 15:43
  • @ikora running the command directly work using sh update.sh. it works well, but with cron it runs partially. – evergreen-dev Mar 09 '23 at 15:45
  • `sh /opt/apt_upgrades/update.sh |& tee /opt/apt_upgrades/log/upgrade.txt`. I think this is what you want. @evergreen-dev I believe that would be a different question, you should write another question in stackoverflow. – Constantin Hong Mar 09 '23 at 15:45
  • @Constantin Hong the purpose of 2>&1 is to redirect error and output – evergreen-dev Mar 09 '23 at 15:46
  • @evergreen-dev Would you tell me the results of `ls -l /opt/docker-compose` and `which docker-compose` ? – Constantin Hong Mar 09 '23 at 21:27
  • `./root/.bashrc;` does not do anything useful; you probably wanted `. /root/.bashrc` with a space after the dot; but then, this only works properly when you are actually running `bash` instead of `sh`. There are some systems where `sh` is a symlink to Bash, in which case it might not throw syntax errors, but also might not do what you expect, because it's running in POSIX compatibility mode. The second duplicate explains how to troubleshoot `cron` problems more generally. – tripleee Mar 10 '23 at 05:59
  • Probably also review the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre] if you still need help. – tripleee Mar 10 '23 at 05:59

1 Answers1

0

There is a syntax error. Please, replace the one in the first script with this below.

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

If the command won't work because of the bash version, try

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

Constantin Hong
  • 701
  • 1
  • 2
  • 16