Ubuntu – Crontab: Ubuntu running script (noob)

bashcronscripts

I'm new to crontab and would like to run the following script from /etc/crontab:

0 15    * * *   root    bash-c 'for i in /home/dell/Downloads/*.{pdf,docx,png,jpg,PDF,DOCX}; do shred -zvu "$i" -n20; done'

I have tried with and without bash-c option, yet the script doesn't run.

My objective is:

  1. Get this script running from Crontab
  2. Get this script running on startup

Help is appreciated.

Best Answer

You're missing a space after the command bash and the argument -c.

This should work:

0 15    * * *  root bash -c 'for i in /home/dell/Downloads/*.{pdf,docx,png,jpg,PDF,DOCX}; do shred -zvu "$i" -n20; done'

Some additional hints:

  • Don't run a crontab as user root if you don't need to.
  • You wrote that you put it in /etc/crontab file. Don't edit crontab files directly, rather use crontab -e command or sudo crontab -e for commands which need root rights. Note, that you don't put the user field in the "other" crontab files.
  • If you have more than one command you can use bash -c as you do, but I'd rather put the commands in a script and execute this from crontab.

  • To run a script on startup, you can use @reboot instead of 0 15 * * *.

Related Question