0

Command on crontab not working but If I run the same command on terminal, it works.

[root@ds backup]# crontab -l
* * * * * find /opt/backup -type f -regextype posix-extended -not -regex '.*('$(sed 's/ /|/g' <<<$(echo {6..0} | xargs -I{} -d ' ' date --date={}' days ago' +"%Y%m%d" | xargs echo))').*' &> /home/ocdn_adm/f.txt

#
#

After 2 mins, I run these commands,

[root@ds backup]# cat /home/ocdn_adm/f.txt
cat: /home/ocdn_adm/f.txt: No such file or directory

Seems like cron command fails to run

But If I run the same command on terminal

[root@ds backup]# find /opt/backup -type f -regextype posix-extended -not -regex '.*('$(sed 's/ /|/g' <<<$(echo {6..0} | xargs -I{} -d ' ' date --date={}' days ago' +"%Y%m%d" | xargs echo))').*' &> /home/ocdn_adm/f.txt
[root@ds backup]# cat /home/ocdn_adm/f.txt
/opt/backup/conf-backup-20180407.tar.gz

it works, weired. Can I make the command to work on crontab?

Avinash Raj
  • 3,703

1 Answers1

2

Beware, cron does not use bash by default. You have to specify used shell, as well as PATH. For example:

SHELL=/bin/bash
PATH=~/bin:~/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin

* * * * * find /opt/backup -type f -regextype posix-extended -not -regex '.*('$(sed 's/ /|/g' <<<$(echo {6..0} | xargs -I{} -d ' ' date --date={}' days ago' +"\%Y\%m\%d" | xargs echo))').*' &> /home/ocdn_adm/f.txt

While we're at it, I also like to add info about display so commands dependant on X are working:

DISPLAY=:0

To debug your cron commands you can take a look at syslog, info about them running and possible errors are there:

tail -f /var/log/syslog

Example output of a correct command xset m 1 1:

May 11 14:29:01 cage CRON[25152]: (Ctrl-C) CMD (xset m 1 1)
Ctrl-C
  • 241