64

I had set a cron job:

20 * * * * /usr/bin/sh /home/lucky/myfile.sh

The main problem is that at the schedule time, there is an error: "mail have sent to /var/spool/mail/lucky".

The contents of myfile.sh is:

mkdir jh
cd jh 
Soroush
  • 103
  • 3
Abhishek Tripathi
  • 749
  • 1
  • 5
  • 7

2 Answers2

97

This is not ok for a script which is set as a cron job:

mkdir jh
cd jh 

You should give the full path where jh directory must to be created. Also, in this path you should have permission to create new files/directories.

For example, your script should look like:

#!/bin/sh

mkdir /home/lucky/jh
cd /home/lucky/jh

Also /usr/bin/sh is not the right path for sh. The right path is /bin/sh. You can check this with whereis sh command. And even so, your cron job should look like:

20 * * * * /home/lucky/myfile.sh

Don't forget to make the script executable:

chmod +x /home/lucky/myfile.sh
Radu Rădeanu
  • 169,590
  • 1
    Awsome, saved my life :) Mine works fine like this. 0 7 * * * /bin/sh /root/Scripts/command.sh > /dev/null 2>&1 – Louwki May 15 '16 at 08:16
  • One point of contention: which sh and whereis sh both show /usr/bin/sh. That's because /bin is a symlink: lrwxrwxrwx 1 root root 7 Feb 17 12:07 /bin -> usr/bin. The most correct shebang AFAIK is #!/usr/bin/env sh. – Avery Freeman Feb 25 '22 at 06:29
8

The path where this seems to be creating the folder at is / . This is because the crontab requires full path to folder and files in all the files that it executes .

So the path in the myfile.sh should be

mkdir <absolutePath>/jh

cd <absolutePath>/jh

20 * * * * /usr/bin/sh /home/lucky/myfile.sh This line is correct though you should check the path to see if sh exists at /usr/bin/sh or not (use which sh to see the path where sh exists; mine was /bin/sh)

If you are in a hurry to start writing crontab this link has pretty good examples

http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

Jos
  • 29,224