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?

1

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)
  • 1
    Please incorporate the advice about escaping % into your answer. – glenn jackman May 11 '18 at 13:33
  • What glenn said; your sample cron command has un-escaped % in it. – Jeff Schaller May 11 '18 at 15:33
  • Please edit an answer if you see an error in it. Anyway, thanks for pointing it out, I added escapes. – Ctrl-C May 11 '18 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.