Predictive Hacks

How to schedule a Cron Job in Linux

Cron Job In Linux

Many times there is a need to run a script in our server periodically. We can schedule our work using cron. The first thing that we need to do is to go to the terminal and to open the crontab by typing the command:

crontab -e

At the first time it will ask you to choose your editor, which can be then nano, vim etc. Personally, I prefer the nano which is the simplest one. Once we open the editor we are ready to schedule our cron job.

There are 5 placeholders which are referred to minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-6) starting from Sunday=0

Let’s say that I have a python script called mytest.py and I want to run it every minute. Then we have to write within crontab:

* * * * * python /path/to/mytest.py

Let’s say that I want to run it every ten minutes. Notice that we can use the “/” to define steps:

# it runs the script every 10 minutes
*/10 * * * * python /path/to/mytest.py

Let’s give some other examples. Look at the commends.

# it runs the script every Friday at 3am
0 3 * * 5 python /path/to/mytest.py

# it runs the script every week
0 0 */7 * * python /path/to/mytest.py

# it runs the script every 1st and 15th of the month
0 0 1,15 * * python /path/to/mytest.py

Q: How can I see which cron jobs have been scheduled?

crontab -l

Q: How can I remove a specific cron jobs?

You can open the text editor and erase the particular line of the corresponding cron job.

Q: How can I remove all the cron jobs without opening the editor?

crontab -r

If you get confused and you are not sure about what you have scheduled, there is a nice crontab-generator which can write the command of crontab for you! Then you just need to copy-paste it at the crontab after typing crontab -e.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

1 thought on “How to schedule a Cron Job in Linux”

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

recommender systems
Python

How to run Recommender Systems in Python

A Brief Introduction to Recommender Systems Nowadays, almost every company applies Recommender Systems (RecSys) which is a subclass of information