/usr/bin/find && -exec /bin/rm never work as expected

hi there,

Would you able to advise that why the syntax or statement below couldn't work as expected ?

/usr/bin/find /backup -name "*tar*" -mtime +2 -exec /bin/rm -f {} \; 1> /dev/null 2>&1 

In fact, I was initially located it as in crontab job, but it doesn't work at all. So, I was thinking to rather put it into a shell script and executed as crontab script. Anyway, both do not work at all. The system environment is bash shell.

I am purely execute it as in terminal, it works pretty fine. Here are the bash_profile and bashrc for reference.

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
# Set autologout after 30 min inactivity for root
if [ "`id -u`" = "0" ]; then
TMOUT=1800
trap clear 0
fi
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@tas-tem-1 ~]# cat /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# by default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ "`id -gn`" = "`id -un`" -a `id -u` -gt 99 ]; then
        umask 002
else
        umask 022
fi

# are we an interactive shell?
if [ "$PS1" ]; then
    case $TERM in
        xterm*)
                if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
                        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
                else
                PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
                fi
                ;;
        screen)
                if [ -e /etc/sysconfig/bash-prompt-screen ]; then
                        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
                else
                PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\033\\"'
                fi
                ;;
        *)
                [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
            ;;
    esac
    # Turn on checkwinsize
    shopt -s checkwinsize
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
fi

if ! shopt -q login_shell ; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
            if [ -r "$i" ]; then
                . $i
            fi
        done
        unset i
fi

Please kindly enlighten. Thanks

A good way to figure it out would be not to drop its standard and error outputs in the first place.

hi

In fact, even without the standard output, it won't work as well before I put up to the forum.

Thx.. Hope to hear more suggestion

No error messages ?

try it without the exec. See if anything returns.

Hi,

No error at all. If without exec, it won't work at all.

It doesn't work only when been scheduled in crontab.

Please provide facts, i.e. a reproducible case and more details about what you expect the command to do but do not observe. "It won't work" or "It doesn't work" isn't sufficient to understand your issue.

The command to delete won't work, I mean will it actually LIST anything based on your find......

hi,

Finally it works perfectly when the following statement constructed into a shell script instead.

By locating it to part of crontab file, it will not work as it supposes to be. Don't quite sure why.

[COLOR="\#738fbf"]

---------- Post updated at 01:13 PM ---------- Previous update was at 01:06 PM ----------

Thread can be closed. Thx anyone for their comments.

If this problem exists when you run as a cron script but not when you run it manually, it is an issue with your environment. Source your .bashrc in the cron script and see if that changes the results.