Stopping Redirection
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2016
    Location
    Cape Canaveral
    Posts
    58
    Windows 7 Firefox 61.0

    Stopping Redirection

    Hello,

    I have a BASH script that runs from cron. I generally like sending the output to an .out file as so:
    Code:
    10 10 * * 0 /app/scripts/MyScript.sh > /app/scripts/out/MyScript.out 2>&1
    However, I have a few commands in the script that I do not want to output to the .out file that uses a cat command.
    Code:
     cat $scrOutLog >> $scrHistLog
    I tried this but it just creates a 0 byte file:
    Code:
     cat $scrOutLog >> $scrHistLog > /dev/null 2>&1
    I think I don't completely understand redirection maybe.

    What can you recommend?


    Thanks,

    Michael

  2. #2
    Join Date
    Nov 2016
    Location
    Cape Canaveral
    Posts
    58
    Windows 7 Firefox 61.0

    Re: Stopping Redirection

    Maybe this will make my objective clearer...

    Is there a way from INSIDE a BASH script to stop standard out and error of all commands that run within it (or after the command to enable this)?

    Thanks.

  3. #3
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,416
    Linux (Fedora) Firefox 61.0

    Re: Stopping Redirection

    Perhaps you may not be aware of crontab's ability to accept a PATH statement. That could help with command searching.


    The that you call script should have the usual shebang line
    #!/bin/bash

    and qualify cat as /usr/bin/cat $scrHistLog

    you could also have that other redirected output by using $( the sub-bash >>other output 2>>other whatever ) .
    If you don't like $( ) then use ` the sub-bash where you control the outputs `
    The $( ) creates a bash subshell. I think you can redirect stdout and stderr to other locations.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  4. #4
    Join Date
    Dec 2017
    Location
    USA
    Posts
    57
    Windows 7 Firefox 60.0

    Re: Stopping Redirection

    You can use curly braces to have sets of statements, whose output you can redirect all together, without the need for sub-shells (which are separate processes). E.g.:
    Code:
    { ls -l /home ; df -h /home ; } 1>/var/log/home.du.log 2>/var/log/error/home.du.log
    Here is a great read: https://www.tldp.org/LDP/abs/html/io-redirection.html

    You can set in-line input and output redirection in the current script, if you wish.
    Code:
    exec 1>>/var/log/home.du.log
    However, it is wise to "preserve" the original destination so you can return to it when you need to. So you would set up a new file descriptor and point it to the stdout file descript (fd number 1) first.
    Code:
    exec 3>&1
    exec 1>/dev/null
    /usr/bin/unimportant-output-binary-here
    exec 1>&3
    exec 3>&-
    The 3>&- closes out the file descriptor. It's not terrible important if you're at the every end of your script, but it builds good practice to close out what you open.

    Now, about cron jobs and redirection. Some people feel that it is best to have the script itself handle its own logging inside the script, and just throw away what cron gets.
    Code:
    # cron entry looks like:
    10 10 * * 0 /app/scripts/MyScript.sh > 1>/dev/null 2>&1
    And the script contains commands similar to:
    Code:
    #!/bin/sh
    logfile=/app/scripts/out/MyScript.out.$( date "+%F" )
    exec 3>&1
    exec 1> "${logfile}"
    exec 2>&1
    # your commands here
    /path/to/unimportant-output-binary >/dev/null
    # the unnumbered redirection command above operates on stdout, or fd1
    # more commands
    exec 1>&3
    exec 2>&1
    exec 3>&-
    And yes, if you really want to just throw away stdout for the rest of the running process, just use:
    Code:
    exec 1>&-
    Which is pretty much the same effect as
    Code:
    exec 1>/dev/null

  5. #5
    Join Date
    Nov 2016
    Location
    Cape Canaveral
    Posts
    58
    Windows 7 Firefox 61.0

    Re: Stopping Redirection

    bgstack15 thank you so much for contributing that. Fantastic! Perfect!

    lsatenstein nice suggestion to.

  6. #6
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,416
    Linux (Fedora) Firefox 61.0

    Re: Stopping Redirection

    I also thought some more and you could have a function within that script to do what you want.
    You would call the function, providing an argument or a symbolic to allow the function to choose .out or /dev/null or the other, as required.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

Similar Threads

  1. KVM Spice USB Redirection
    By hzuser in forum Servers & Networking
    Replies: 4
    Last Post: 17th May 2013, 09:38 AM
  2. Squid redirection
    By rxveil in forum Security and Privacy
    Replies: 2
    Last Post: 11th April 2007, 05:01 PM
  3. about redirection using exec
    By jsanza@terra.es in forum Using Fedora
    Replies: 1
    Last Post: 2nd May 2006, 07:41 PM
  4. Output and redirection
    By greatscot in forum Guides & Solutions (Not For Questions)
    Replies: 10
    Last Post: 9th April 2005, 12:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •