Certain cases, you need to run the background tasks. In linux, the default scheduler tasks called “Cron Jobs”.  To view the list of cron jobs that are running in the machine by using the following command,

crontab -l
     # m h dom mon dow command
     33 11 * * * touch /mnt/test.sample >> /var/log/test.log
    0 8 * * * /mnt/CloudSmart/Start.sh > /var/log/cloudsmart_start.out 2>&1
   0 21 * * * /mnt/CloudSmart/Stop.sh > /var/log/cloudsmart_stop.out 2>&1
 

  To edit or add new cron jobs, by using the below command

      crontab -e

For the first time, it will ask you to choose the editor like, vi, nano etc. Every job, by default it represents along with 5 stars.

eg.,  * * * * * touch /mnt/test.sample >> /var/log/test.log

5 stars : Scheduling period

             1.  Minute Field – 0-59 (mins)
             2. Hour Field  0-23 (hours)
             3. Day of Month 1-31
             4. Month Field 1-12
            5. Days 0-6 (Sun – 0, Mon – 1, Tue – 2, Wed – 3, Thu – 4, Fri – 5, Sat -6)          
 
           Note : – For every field, you need to give tab, Don’t use keyboard space between the fields in   your cron
 

   In my above scenario, i want to execute the jobs in daily basis, to start the cloud machine daily @ 8.00 AM and to stop the machine (Virtual machine) @ 9.00 PM. My Cron job

 
                0 8 * * * /mnt/CloudSmart/Start.sh > /var/log/cloudsmart_start. out 2>&1 
                     0 – o mins
                     8 – 8 AM
                     *  – Every day (1-31)
                     * – Every Month (1-12)
                     * – No Custom days (all days) 
                     /mnt/CloudSmart/Start.sh  – Shell script file to execute your task
                    /var/log/cloudsmart_start. out 2>&1  – Log outting
              
               0 21 * * * /mnt/CloudSmart/Start.sh > /var/log/cloudsmart_start. out 2>&1 
                     0 – o mins
                     21 – 9 PM
                     *  – Every day (1-31)
                     * – Every Month (1-12)
                     * – No Custom days (all days) 
                     /mnt/CloudSmart/Stop.sh  – Shell script file to execute your task
                    /var/log/cloudsmart_stop. out 2>&1  – Log outting
                     

    If, i want to execute the task by only on weekdays,

               0 8 * * 1-5 /mnt/CloudSmart/Start.sh > /var/log/cloudsmart_start.out 2>&1
              0 21 * * 1-5 /mnt/CloudSmart/Stop.sh > /var/log/cloudsmart_stop.out 2>&1
                          – These jobs only executes on Monday  to  Friday
 

            To save and update the crons, you need to restart the cron service

           /etc/init.d/cron restart
 
 
 
Advertisements