-1

English is not my native language, please accept my apologies for any language issues.

I want to execute a script (bash / sh) through CRON, which will perform various maintenance actions, including backup. This script will execute other scripts, one for each function. And I want the entirety of what is printed to be saved in a separate file for each script executed.

The problem is that each of these other scripts executes commands like "duplicity", "certbot", "maldet", among others. The "ECHO" commands in each script are printed in the file, but the outputs of the "duplicity", "certbot" and "maldet" commands do not! I want to avoid having to put "| tee --append" or another command on each line. But even doing this on each line, the "subscripts" do not save in the log file. That is, ideally in the parent script, you could specify in which file each script prints.

Does not work:

  1. sudo bash /duplicityscript > /path/log

    or

    sudo bash /duplicityscript >> /path/log


  1. sudo bash /duplicityscript | sudo tee –append /path/log > /dev/null

    or

    sudo bash /duplicityscript | sudo tee –append /path/log


  1. Using exec (like this):

    exec > >(tee -i /path/log) 
    sudo bash /duplicityscript 
    exec > >(tee -i /dev/null)`
    

Example:

./maincron:

    sudo ./duplicityscript > /myduplicity.log
    sudo ./maldetscript > /mymaldet.log
    sudo ./certbotscript > /mycertbot.log

./duplicityscript:

    echo "Exporting Mysql/MariaDB..."
    {dump command}

    echo "Exporting postgres..."
    {dump command}

    echo "Start duplicity data backup to server 1..."
    {duplicity command}

    echo "Start duplicity data backup to server 2..."
    {duplicity command}

In the log file, this will print:

    Exporting Mysql/MariaDB...
    Exporting postgres...
    Start duplicity data backup to server 1...
    Start duplicity data backup to server 2...

In the example above, the "ECHO" commands in each script will be saved in the log file, but the output of the duplicity and dump commands will be printed on the screen and not on the log file.

I made a googlada, I even saw this topic, but I could not adapt it to my necessities.

There is no problem in that the output is also printed on the screen, as long as it is in its entirety, printed on the file.

Community
  • 1
  • 1
guinalz
  • 37
  • 7
  • 3
    Have you checked if the commands that do not obey your redirection are printing on stderr? Try adding `2>&1` at the end of the line (after the first redirection, which is for stdin). – Fred Feb 03 '17 at 00:26
  • `sudo` only applies to the *command*, the *redirection* is a *separate* process, so `su sh -c "/duplicityscript > /path/log"`. (or `su bash -c "command"`... your choice) **note** update `sudo` to `su`. – David C. Rankin Feb 03 '17 at 00:30
  • Don't work without sudo. I remove sudo, cron is runing as root. Thanks. – guinalz Feb 03 '17 at 00:45

1 Answers1

0

try 2>&1 at the end of the line, it should help. Or run the script in sh -x mode to see what is causing the issue.

Hope this helps