1

I tried to setup a cronjob with crontab to run a shell script every 10 minute. But it´s not working. I want the shellscript to delete the specific file "/var/log/messages" every 10 min.

The Shell Script file:

#!/bin/bash 
# Remove Files 
rm -f /var/log/messages 

The Crontab -E Config file:

*/10 * * * * /vpndel/script 

But when i check the log file the text is still there after 10 minutes have gone? What could be wrong?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • just put this in the `root` crontab: `*/10 * * * * /usr/bin/rm -f /var/log/messages/*` (this will delete all logs in the directory messages, if you want to delete a specific file just name it there, if messages is a file its just `/var/log/messages`) – Alex Nov 12 '13 at 21:58
  • @Alex: You're assuming `/var/log/messages` is a directory. On at least two systems I've just checked, its a file. – Keith Thompson Nov 12 '13 at 22:04
  • @Keith then its `*/10 * * * * /usr/bin/rm -f /var/log/messages` – Alex Nov 12 '13 at 22:05
  • @Alex: Yes, invoking `rm` (just `rm` should be fine, no need to specify `/usr/bin/rm`) should work -- but the OP might want to be able to modify `/vpndel/script` later on without changing the crontab. Incidentally, `rm` is typically `/bin/rm`, not `/usr/bin/rm`; `/usr/bin/rm` probably won't exist unless `/usr/bin` is a symlnk to `/bin`. – Keith Thompson Nov 12 '13 at 22:09
  • @Keith in a crontab you always have to use the full path to the program :) I'm runing `Arch Linux` and it's in `/usr/bin` on my system, but to find out where `rm` is you can always do a `whereis rm` on your system. – Alex Nov 12 '13 at 22:12
  • @Alex: No you don't, at least not on any system I've ever used. The default `$PATH` for cron jobs is typically `/usr/bin:/bin`. Try adding a cron job `* * * * * env > cron-env` and see what appears in `cron-env`. – Keith Thompson Nov 12 '13 at 22:15
  • @Keith Don't know which systems you use but in order to do that you have to do [this](http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths) on most systems. – Alex Nov 12 '13 at 22:18
  • @Alex Thanks , How do i add so everytime its done its sends a mail to a specific address? In order to being enable to check that the task was crontab was acually executed? – user2985180 Nov 12 '13 at 22:21
  • @user2985180 Put `MAILTO=email@address.com` on top of your crontab. And make sure `postfix` or your mail program is properly configured. – Alex Nov 12 '13 at 22:23
  • @Alex: Not on Debian, Ubuntu, or Linux Mint. cron jobs are given a very sparse environment, but with a valid `$PATH`. `man 5 crontab` if you're using Vixie cron; on my system it specifically says that `$PATH` is set to `"/usr/bin:/bin"`. Please try the `env > cron-env` that I suggested; I'd be interested in seeing the results. – Keith Thompson Nov 12 '13 at 22:39
  • I get a error in the mail i recived saying "/bin/sh: /usr/bin/rm: No such file or directory" And i was using Alex´s Cronjob code this time. "*/10 * * * * /usr/bin/rm -f /var/log/messages" – user2985180 Nov 12 '13 at 22:49
  • @user2985180: Replace `/usr/bin/rm` by `rm` – Keith Thompson Nov 13 '13 at 02:21
  • Still doesnt work , not it even wont send me a mail regarding the cron job :/ – user2985180 Nov 13 '13 at 16:18

1 Answers1

1

There are several things to check:

  1. Is your /vpndel/script file executable?

  2. Are you executing it from an account that has write permission on /var/log (that's probably just root)? Specifically, are you running the crontab command as root?

  3. Are you sure it's not deleting the file? Could the system just be recreating it?

(I won't ask why you want to delete /var/log/messages, but it seems like an odd thing to do.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631