6

I am trying to create a directory having a timestamp in its in name in the /home directory. I have created the following cron job as the root user, but it's not running. What am I doing wrong?

Following is my cron job which I have created with root user privilege.

[root@bvdv-emmws01 home]# crontab -l
* * * * * /home/test.sh

Following are the contents of /home/test.sh. Update: added full path for directory.

[root@bvdv-emmws01 home]# cat /home/test.sh 
#!/bin/bash
mkdir /home/test_$(date -d "today" +"%Y%m%d%H%M%S")

Permission of /home/test.sh:

[root@bvdv-emmws01 home]# ls -ltr /home/test.sh
-rwxrwxrwx 1 root root 58 Dec  2 12:58 /home/test.sh

I have updated the /etc/crontab file. That file now has the following contents:

[root@bvdv-emmws01 home]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

* * * * * root /home/test.sh

Status of crond daemon

[root@bvdv-emmws01 home]# service crond status
crond (pid  1910) is running...
  • Does it work when you execute /home/test.sh in a shell? What is the output of ls -l /home/test.sh? – Hauke Laging Dec 2 '14 at 9:55
  • You might want to use absolute paths in your script as well. – John WH Smith Dec 2 '14 at 10:22
  • [root@bvdv-emmws01 home]# ls -l test.sh -rwxrwxrwx 1 root root 58 Dec 2 12:58 test.sh – Tahir Akram Dec 2 '14 at 10:32
  • @HaukeLaging when I run it through shell, it do create directory with name like test_00000000 – Tahir Akram Dec 2 '14 at 10:33
  • 1
    Those directories will be created in the home directory of the user (here root so something like / or /root). – Stéphane Chazelas Dec 2 '14 at 14:02
9

A crontab created with crontab -e and listable with crontab -l should not have a user specified for the command. Your entry should read:

* * * * * /home/test.sh

Or alternatively put the line that you have in /etc/crontab instead.

From man 5 crontab (section EXAMPLE SYSTEM CRON FILE):

# /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.

Note the last sentence.

  • I have added * * * * * root /home/test.sh in my /etc/crontab. But still no luck. Unable to see directory in my /home. – Tahir Akram Dec 2 '14 at 10:40
  • @TahirAkram can you do a cat /home/test.sh instead of cat test.sh and paste the output in your question? – Anthon Dec 2 '14 at 12:49
  • 1
    Is crond deamon running? – Albert Dec 2 '14 at 12:51
  • Does test.sh have the executable permissions set? – YoMismo Dec 2 '14 at 14:24
  • @Albert [root@bvdv-emmws01 home]# service crond status crond (pid 1910) is running... Yes it's running. – Tahir Akram Dec 2 '14 at 16:16

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.