0

I have the following bash script:

clean-tmp.sh

 #!/bin/bash
 tmpreaper 1h /tmp --test > ./tmpreaper.log

When I run it in the terminal using ./clean-tmp.sh, it writes to the file ./tmpreaper.log.

I added the script to the list of cron jobs using crontab -e:

*/5 * * * * cd /home/cron-jobs && ./clean-tmp.sh

I then checked cron's logs and this entry is in there every 5 minutes:

Feb 19 00:45:01 ip-172-31-23-184 CRON[1475]: (ubuntu) CMD (cd /home/cron-jobs && ./clean-tmp.sh) 

But it's no longer writing to ./tmpreaper.log.

What on earth am I doing wrong?

11
  • 1
    BTW, the usual cause for this kind of problem is tmpreaper not being in cron's PATH. If it's in /usr/local/bin for example, that's not there by default. Feb 19, 2017 at 1:26
  • 1
    (As an aside, I'd tend to suggest avoiding .sh -- UNIX executables don't generally have extensions; you run ls, not ls.elf, after all; and .sh in particular implies that sh is an adequate interpreter to run something, whereas #!/bin/bash contradicts that by indicating that bash is its intended interpreter). Feb 19, 2017 at 1:29
  • 1
    @CharlesDuffyI actually had 2 problems, but you were absolutely right. My first problem is that I used an alias in the crontab which you shouldn't do: unix.stackexchange.com/questions/1496/…. As for the second problem, I had to change my script to use /usr/sbin/tmpreaper instead of just tmpreaper. I figured out where it was by using which tmpreaper. See this link for more info: askubuntu.com/questions/47800/… Feel free to post an answer.
    – Kacy
    Feb 19, 2017 at 2:02
  • 1
    For the future, I'd suggest using type instead of which -- while which is an external command that looks through the PATH, type is built into the shell and can inspect its internal state, finding what a command would do even if it's not in a PATH lookup (identifying aliases, functions, etc). Feb 19, 2017 at 2:11
  • 1
    BTW, your other option is to put an explicit PATH line in your crontab -- any variable assignment you put in the header will be exported to the environment. Thus, PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin or such at the top of your crontab will ensure that contents in /usr/sbin can be found for all scripts launched by cron. Feb 19, 2017 at 2:13

2 Answers 2

0

Just specify an absolute path for your file, like tmpreaper 1h /tmp --test > /var/log/tmpreaper.log

2
  • If the cd succeeded, then we know the current working directory with certainty. Unless there's another script called clean-tmp.sh somewhere else, then . can't possibly be anything other than /home/cron-jobs when ./clean-tmp.sh is successfully invoked. Feb 19, 2017 at 1:23
  • ...and if it's not successfully invoked, then this doesn't fix anything. Feb 19, 2017 at 1:24
0

@Kacy: Little difficult to say without cron logs, you could have a look to cron logs(/var/log/cron etc).

0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/cron-jobs; ./clean-tmp.sh

May be some systems wouldn't allow time period as the way you tried, try once in above way and let us know then.

3
  • 1
    If */5 weren't valid, then crontab -e would have rejected the edit. Also, the question makes it clear that this is logged as invoked every 5 minutes as it is. Feb 19, 2017 at 1:25
  • @CharlesDuffy: Thank you for your reply, I am not saying it is not valid, I have seen systems where it allows to save it with crontab -e but it doesn't work so only I suggested if user could try once. Feb 19, 2017 at 1:48
  • 1
    As the OP specified in the question, their syslogs say that cron is actually running the command every 5 minutes. Feb 19, 2017 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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