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

    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