2

When I manually type:

root@username:~# ./myscript.sh

The script executes, however I tried the following combinations to have crontab execute myscript.sh every 30 minutes (forever) and none seem to do anything:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/30 * * * * root sh /root/myscript.sh
*/30 * * * * root sh ./myscript.sh
*/30 * * * * root sh myscript.sh
*/30 * * * * root ./myscript.sh 

Contents of myscript.sh:

KEYWORDS=("Word1" "Word2" "Word3" "Word4")
IFS=$'\n'
find . -size +1c -type f ! -exec grep -qwF "${KEYWORDS[*]}" {} \; -exec truncate -s 0 {} \;
  • 1
    Please post your script, also try adding 2> logfile to your cron line so you can capture any errors. – terdon Jan 17 '14 at 16:33
  • # cat /var/log/cron | grep myscript.sh Any errors? – setevoy Jan 17 '14 at 16:34
  • /var/log/cron - in CentOS, in Debian/Ubuntu can be different logfile. – setevoy Jan 17 '14 at 16:45
  • I bet it's a PATH issue. – coteyr Jan 17 '14 at 16:49
  • setevoy: Yeah I enabled logging by editing /etc/rsyslog.conf -> uncommenting cron.* /var/log/cron.log -> /etc/init.d/rsyslog restart (DEBIAN) – Guest in need of help Jan 17 '14 at 16:50
2

By the way - to run cron task every 30 minutes - try to use:

00,30 * * * * [path_to_file]/myscript.sh

  • So my format was incorrect? – Guest in need of help Jan 17 '14 at 16:55
  • */30 format must work too - but you can try other way if have problems. – setevoy Jan 17 '14 at 16:56
  • I've tried a million different combinations nothing works... pastebin.com/N3GzHFXU – Guest in need of help Jan 17 '14 at 19:20
0

To ensure your crontab works, you should always have full path to your code. To get the full directory to your code:

  1. Go to the directory of your code.
  2. Use the command pwd, which will print the directory you're in.
  3. Use this directory with your script when appending to the crontab.

If you want your code to run every 30 minutes you can

*/30 * * * * /full/directory/to/your/code/mycode.sh

If you want your code to run on the hour ie XX:00 and 30 past the hour ie XX:30

 00,30 * * * * /full/directory/to/your/code/mycode.sh

If all of these are done, and you're still not getting the output you're expecting try looking at your /var/log/syslog files, you should be able to see what is going wrong.

-1

To run cron task every 30 minutes, cron expression will be like this:

*/30 * * * * /path-to-file/filename
  • This answer uses per user crontab syntax, which although root does have one, I recommend against using it as most people forget it is thare, and root can just use the system one anyway. – hildred Jun 2 '15 at 14:27

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.