Skip to main content

How to write a Cron Job in Linux?




What is a Cron Job?

Cron is a Linux utility where you can setup a task in your machine to run automatically, if required in a repetitive manner at a specific time and date.
Such a task that you schedule is called a Cron Job.

Here we'll look into how we can setup a cron job in a Linux machine.
Follow the steps bellow.

Step 1 :

Place the script you want to schedule as a Cron Job to one of the following directories in your machine depending on how often you need to repeat the execution of the script.
  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly
For example, if you want to schedule your task daily, place the script in the /etc/cron.daily directory.

Step 2 :

Then give correct permissions to the script as follows. Assume script.sh is the script that you need to schedule.

cd /etc/cron.daily
chmod 755 script.sh
 
Step 3 :
Then you should add a new Cron Job to crontab.

crontab -e
 
Step 4 :

Then you will be prompted with your vi editor in the terminal. Type in the following into the editor and to save it hit ESC key, and then type :w followed by :q to exit.

0 0 * * * /etc/cron.daily/script.sh
 
This command will make the Cron Job run every night.
Note : Look at the following to identify the different ways of customizing the command given in Step 4.


Comments

  1. Blog commenting is an excellent. Thank you for sharing this wonderful piece of information and for sharing helpful insights. Otherwise If any one who want to achieve a goal in linux contact us on 9311002620 or https://htsindia.com/Courses/modular-courses/redhat-linux-administration-training-course

    ReplyDelete
  2. Crontab files are simple text files that have a particular format. Each line of a crontab file follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values. A single cron job should take up exactly one line, but this can be a long line (more than 80 characters).
    for more information click here:how to setup a cron job

    ReplyDelete

Post a Comment

Popular posts from this blog

How to import the Public Certificate of one WSO2 product to the trust store of another?

To demonstrate this point, I will use the 2 products WSO2 API Manager 2.1.0 (referred as APIM from here onwards) and WSO2 Enterprise Integrator 6.1.1 (referred as EI from here onwards). When using EI as the Business Process Server during configuration of Workflows in APIM, one step to perform is to import the public certificate of EI to the truststore of APIM [1]. So now let's see how this can be done. Step 1: Go to <EI_HOME>/repository/resources/security/ folder and execute the following keytool command. This command is used to export the public certificate of EI as a certificate file called wso2carbon.cer. Since the default keystore in EI is wso2carbon.jks, we have specified it as the keystore and the default alias is wso2carbon. Provide wso2carbon as the keystore password when prompted as it is the default password. After executing the above command from within the security folder in EI, you will see that a file with the name of wso2carbon.cer is created...

How to connect my database instance with elastic beanstalk instance in AWS?

If you have deployed your web application in Elastic Beanstalk in AWS and now you need to connect a database to this instance, and your database is actually residing in a different instance, how can you actually connect them? It's easy with Elastic Beanstalk. I will explain an example scenario that I used for connecting my elastic beanstalk web application with another instance containing my MongoDB database. By looking at this, you can customize as per your need. Don't worry. This is easy. :) The only things you need here are the details about the 1. Database name that you need to connect to. Ex:- "myDB" 2. Port at which the database instance is listening. EX:- In the case of MongoDB, the listening port is 27017 3. Host name of your database instance. EX:- Like localhost, in this case, it will be the Public DNS of your database instance 4. The password of your database if exists. First these details need to be set as environment variables in Elastic Be...