SBT and Github commands using Linux Cronjob for github project


When i was in the middle of my project I encountered a challenge to run sbt commands and github commands for my cloned project at regular intervals(say after every hour or two). When I looked up to the problem it seemed a bit challenging until I did not bump into Linux Cronjob(A time based job scheduler ).

Alright, for those who do not know about Cronjobs.

Cron- The soft utility cron is a time based job scheduler.It can be used to schedule jobs at regular interval(interval can be fixed accordingly). It will also let you execute commands at regular intervals.

Cronjobs are the jobs scheduled by the scheduler to run at regular intervals.
Cronjobs are created using using command line prog called the crontab.

The crontab schedule consists of 6 fields

Crontab format

minute hour day month day_of_week command-line

To lean more about the cron jobs ya can follow the link below

Click here

To write a cron job

Goto the terminal and type

crontab -e

Now ya will see a list of cronjobs if any, ya can also schedule a fresh job there  by writing a new one

Example

If i want a script to be execute every minute, I will say

1 * * * * PATHTO/test.sh

PATHTO -Path for my script

Test.sh is the name for my script

Now what will happen is,this test.sh will be executed every minute.

Above were the basics for cronjob.

Now, I will demonstrate how to write cronjobs for a github project to run github and sbt commands at regular intervals.

For that matter I will write a cronjob and a script which will have sbt and github commands in it .

Test.sh will be like this

Test.sh

#!/bin/bash
# My first script
cd PATHTO/crontest && PATHTO/git clone gitCloneURL
cd PATHTO/crontest && PATHTO/git pull origin master
cd PATHTO/Project_Name && PATHTO_SBT/sbt cpd
cd PATHTO/Project_Name && PATHTO_SBT/sbt scalastyle

 

Code Line 3 Explanation

In line line 3 cd has been used to change the directory where you want your github project to be cloned in your directory structure.

&& is a linux script keyword to add another command which can be your github or sbt command

In line 3 what I am tryin to do is,I am tryin to clone a github project in a folder called crontest.You can write the same set of lines  for any other github command like below

  • git pull origin master
  • git push origin master(only if you are using ssh not otherwise,because it needs authentication then)
  • git anyCommand 

Code line 5

In line 5 again cd has been used to change the directory where your github project is located in your directory structure.

&& is a keyword to use another command in conjuction with previous command,here i have used full path for your sbt followed by sbt command. In PATHTO_SBT, you need to mention the full path of the sbt which has been set in the .bashrc file.

In line 5 what I am tryin to do is,I am tryin to run a sbt command for the  github project in a folder called Project_Name. You can write the same set of lines  for any other sbt command like below

  • sbt scalastyle
  • sbt cpd
  • sbt scapegoat
  • sbt coverage test
  • sbt test
  • sbt anyTool

Now we have written the script to run github commands and sbt commands, since this script can be executed from the terminal using ./Test.sh but we were aiming to execute the script at regular intervals so that we could fetch latest reports for sbt projects like scalastyle reports,cpd reports or any other report for that matter. We can also automate latest pulls from the cloned github projects.

Our main aim was to automate things for us,for that matter we have written a script already i.e Test.sh and now we will write the cronjob using Crontab 

In terminal we will say $crontab -e and write in fresh cronjobs.

In order to execute the above written script i.e Test.sh, I will perform a cronjob. Let’s I am interested in executing the script every five minutes,I would say

Example

5  * * * * PATHTOScript/Test.sh

and  Ctrl +X to exit and save the cronjob,the job will be auomatically started and it will execute the script every five minutes as mentioned in the job.

PATHTOScript – It is the path of your script file in the directory structure where your script is located.

Now your tasks have been automated.That’s it you are good to go.

Cheers..!

About deepak028

There is nothing much to describe me.However, I am a very ordinary person who believes in sharing knowledge.
This entry was posted in Scala and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s