1

Just an one line shell script below not working as a cron job but executing directly from terminal works fine:

#!/bin/bash

echo "Executed" >> ./crond.log 2>&1

What might be the problem?

Checked /var/log/cron and found cron is kicking the task in time:

May 16 10:30:01 vagrant-centos64 CROND[3015]: (root) CMD (sh /vagrant/my.sh)
May 16 10:35:01 vagrant-centos64 CROND[3122]: (root) CMD (sh /vagrant/my.sh)
May 16 10:40:01 vagrant-centos64 CROND[3189]: (root) CMD (sh /vagrant/my.sh)
May 16 10:45:01 vagrant-centos64 CROND[3270]: (root) CMD (sh /vagrant/my.sh)
May 16 10:50:01 vagrant-centos64 CROND[3343]: (root) CMD (sh /vagrant/my.sh)
May 16 10:55:01 vagrant-centos64 CROND[3430]: (root) CMD (sh /vagrant/my.sh)

Crontab job listing is like below:

*/5 * * * * sh /vagrant/mypagelogin.sh
*/5 * * * * sh /vagrant/my.sh
[root@vagrant-centos64 vagrant]#

Access permission for crond.log is:

-rwxrwxrwx 1 vagrant vagrant 0 May 16 10:51 crond.log

UPDATE: crond.log file is located in the same location as my.sh. [/vagrant]

7
  • Define not working
    – heemayl
    May 16, 2016 at 10:58
  • 1
    Where is your crond.log? (I'd advise against making the assumption that your cron job is started from the user's home directory.) May 16, 2016 at 11:03
  • 1
    I don't see any question. Is your question where is crond.log or why the permission is like that or anything else?
    – heemayl
    May 16, 2016 at 11:05
  • Updated question according to above question
    – Rahman
    May 16, 2016 at 11:06
  • I was looking for the cause why the file crond.log is not written to from the script if it is executed as a cron job?
    – Rahman
    May 16, 2016 at 11:07

2 Answers 2

6

First rule of crond club: you don't assume the working directory. My guess is that you'll find a crond.log in /root. If you want it in /vagrant, explicitly redirect the output to /vagrant/crond.log.

(FWIW, the second rule of crondclub is: don't assume there's anything in your PATH, use explicit paths to binaries, but since echo is also a bash builtin, you're fine on that one.)

2
  • 5
    also put the job in vagrant's crontab, not root's unless there is a particular need for the job to run as root.
    – cas
    May 16, 2016 at 11:10
  • Thank you! It worked for me. I just put the absolute path for the cornd.log file as /vagrant/crond.log.
    – Rahman
    May 16, 2016 at 11:31
1

It's written but not where you're looking for. The best way is using absolute paths.

1
  • Nice advice. I wii try it and update. Thank you.
    – Rahman
    May 16, 2016 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .