How to set a cron job to run a shell script?

I had set a cron job:

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

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

The contents of is:

mkdir jh
cd jh 

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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:


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/

Don’t forget to make the script executable:

chmod +x /home/lucky/

Solution 2

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 should be

mkdir <absolutePath>/jh

cd <absolutePath>/jh

20 * * * * /usr/bin/sh /home/lucky/ 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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply