Different ways to run Shell Commands in Ruby


Bookmark and Share

During development of an application, there will come cases when we need to access system command from our program itself. In some cases we just need to know if the status was complete, in some cases, we also need to know the output the application returns.But before we start, let’s find out what Shell Commands are. Simply put, the shell is a program that takes your commands from the keyboard and gives them to the operating system to perform. There are several additional shell programs available on a typical Linux system. Described below are 6 different way to execute a shell script from ruby and their properties.

  • Exec
  • System
  • Backticks ()
  • IO#popen
  • open3#popen3
  • open4#popen4
  • Exec

    Kernel#exec (or simply exec) replaces the current process by running the given command, which can take one of the following forms:

    exec(commandline)
    command line string which is passed to the standard shell

    exec(cmdname, arg1, …)
    command name and one or more arguments (no shell)

    exec([cmdname, argv0], arg1, …)
    command name, argv and zero or more arguments (no shell)

    In the first form, the string is taken as a command line that is subject to shell expansion before being executed.

    In the second form …

    Read More

    Mina: Faster deployment and remote server automation


    Bookmark and Share

    Even though we use CodeShip/Circle CI/Jenkins for continuous integration, we still need to write scripts to automate our deployment. We also need to write small commands to clear cache, restart queues, etc. It’s always a good practice to not have to enter the server directly but to have it done through scripts. There are many tools available for this purpose (in multiple languages) like Capistrano, Vlad, etc.

    Mina is a similar tool, but faster. The reason why it runs faster is because it generates a bash script, uploads it to the server and run there, rather than creating a new ssh session and run every command one by one. Mina is one of our default tools at Red Panthers.

    To use mina in your project, add mina to your Gemfile.

    and to get started do

     

    http://nadarei.co/mina/setting_up_a_project.html
    https://www.digitalocean.com/community/tutorials/how-to-deploy-with-mina-getting-started

     

    Having a one step deployment is an important requirement for any project so have one ready using mina or capistrano.

     

    Note:

    Assets pre-compile

     

    Run another rake task

    Run shell commands

    The syntax queue is referring to the fact that, the commands are all made into a single …

    Read More

    Enable log rotation within the rails application


    Bookmark and Share

    Any person who has worked with a rails application for a good amount of time would have faced the issue of log files growing and consuming the entire space in the hard disk. Although this sounds funny, it does happen if you do not place a log management for your rails app (in production). You can manage your logs using log rotation. Do note that log rotation is not required if you host your app in heroku, as heroku won’t let you store files for more than 15 minutes in their server. You also do not need it if you are using some third party services like loggly or papertrail to manage your logs .

    In the unix world, you can use log rotate service, which would be installed by default in all linux servers. But we at Red Panthers feel that everything that touches or involves our rails application should be placed along with our rails application as much as possible. If you feel the same way, it can be achieved by placing the code below in …

    Read More