Mailcow cron upgrade script

  • Hello together ,


    I wrote a little sh script to which is executed by a cronjob to update my backup mx everyday. I thought I should share it with you :)


  • Hello together,


    based on this script, I wanted to have one that doesn't install the update automatically, but informs me by email about an upcoming Mailcow update.


    I wrote the following:

    Shell-Script
    1. #!/bin/bash
    2. cd /opt/mailcow-dockerized && echo 'y' | ./update.sh --check
    3. exit_status=$?
    4. if [ $exit_status -eq 0 ]; then
    5. echo "Mailcow Update verfügbar" | mail -s "INFO: Mailcow Update verfügbar" test@example.com
    6. fi


    Unfortunately it does not work. I don't receive any information.


    If I redirect $exit_status into a file I see that is always 1, even if "./update --check" says an update is available.


    I also tested to redirect the crontab output into a file:

    Shell-Script
    1. cd /opt/mailcow-dockerized && echo 'y' | ./update.sh --check >>/opt/test.log

    Then I get the output:

    Quote

    Cannot find docker-compose, exiting...

    Is it possible that the script call has to be done differently in cronjob?


    Many thanks in advance for your help!


    sax

  • Hi sax,

    try this


  • Hi oliverr,


    Thank you for this code! But first of all sorry for the late feedback. Unfortunately I did not receive any notification about your answer.


    Unfortunately, this doesn't work.

    The output in the mail is: INFO: Mailcow Update unknown - Mailcow update.sh output: Cannot find docker-compose, exiting...

  • Hi, is this still open?

    If still open, try this:

    Shell-Script
    1. administrator@mailserver:~$ which docker-compose

    The output of this is either:

    Shell-Script
    1. administrator@mailserver:~$ which docker-compose
    2. /usr/local/bin/docker-compose

    or:

    Shell-Script
    1. [markus@james ~]$ which docker-compose
    2. /usr/bin/which: no docker-compose in (/home/markus/perl5/bin:/home/markus/perl5/bin:/usr/share/Modules/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/markus/.local/bin:/home/markus/bin:/usr/local/go/bin)
    3. [markus@james ~]$

    If first result is what you receive as answer, seems that you have to set your PATH environment within your shell script correctly.

    If second result is what you receive as answer, seems that you do not have docker-compose installed, or installed correctly (also maybe PATH variable, depends on how you set this up...)


    Greets

    Markus

  • Hi Markus,

    If first result is what you receive as answer, seems that you have to set your PATH environment within your shell script correctly.


    thx so much for your answer. My problem still exist. The output from which docker-compose is:

    Code
    1. sax@mail:~# which docker-compose
    2. /usr/local/bin/docker-compose


    I don't understand why I have to set the environment variable in my shell script? The path is already in the PATH-Environment:

    Code
    1. sax@mail:~# echo $PATH
    2. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin/docker-compose:/opt:/opt/mailcow-dockerized

    Is there my fault?

    Am I using the correct path? And how I should set it in the script? Like in the bashrc?

    Code
    1. PATH=$PATH:/usr/local/bin/docker-compose:/opt:/opt/mailcow-dockerized
    2. export PATH


    greets

    sax

  • Hi sax,

    first: PATH=$PATH:/usr/local/bin/docker-compose:/opt:/opt/mailcow-dockerized - docker-compose is the utility/command you want to execute and therefor not part of the path variable :-)

    so correct will be: PATH=$PATH:/usr/local/bin:/opt:/opt/mailcow-dockerized


    but: as "docker-compose" is within "/usr/local/bin", there is no need to extend the PATH variable. In my opinion it is also not nessessary to put "/opt" and "/opt/mailcow-dockerized" in the PATH variable


    now lets do a little deeper dive... :)

    as I can see you executed my commands as user "sax". Do you also run your script from within the users "sax" crontab?


    In my mailcow-dockerized installation I have to run the docker-compose command as priviliged user "root" from within the /opt/mailcow-dockerized" path.


    Tell me something more about your environment...


    What distro do you use? In which version? How did you install mailcow-dockerized? Did you use the installation manuel from this site (https://mailcow.github.io/mailcow-dockerized-docs/)? Espacially the part from the sub-menu "Installation" => 1. Learn how to install Docker and Docker Compose. Can you post the environment informations from within a cron job? You can do this with a little script that you execute the same way than your cronjob for checking after updates.


    [markus@james ~]$ vim export_env.sh

    Shell-Script
    1. #!/usr/bin/bash
    2. cd ~
    3. set > ~/env.log

    [markus@james ~]$ chmod u+x export_env.sh


    [markus@james ~]$ crontab -e


    If all this does not bring us clother to the solution the only thing I can do is to reproduce your environment by installing your distro, create the same users, etc... and install mailcow-dockerized and see what happens


  • Hi Markus,


    first of all sorry for the late reply. My wife gives me too little time for my hobby. :) I promise to improve in the future.

    Do you also run your script from within the users "sax" crontab?

    First I run the script with user sax, but later with root user. However, this didn't seem to be the problem, but the one with the environment variable.


    What distro do you use? In which version? How did you install mailcow-dockerized?

    I use Debian 9 (stretch) and installed Mailcow according to these instructions: https://mailcow.github.io/mail…rized-docs/i_u_m_install/


    The export_env.sh shows the differences between manual execution and execution by the crontab:

    export.png



    As soon as I add the environment variable to the script, it works.

    But I still wonder why the environment variable is different in crontab execution?

    Code: Environment
    1. export PATH=$PATH:/usr/local/bin:/opt:/opt/mailcow-dockerized


    See you soon

    sax