# Executing shell commands from a Ruby script

**URL:** <https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540>\
**Category:** Ruby Chat\
**Created:** [June 12, 2016, 5:52pm UTC](https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540 "2016-06-12T17:52:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [June 12, 2016, 5:52pm UTC](https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540/1 "2016-06-12T17:52:17Z")

</div>

Anyone have any experience with executing shell commands from a Ruby script?

I want to convert this to Ruby :003:

```auto
#!/bin/bash

web_service='haproxy'
config_file='/usr/local/etc/le-renew-haproxy.ini'
domain=`grep "^\s*domains" $config_file | sed "s/^\s*domains\s*=\s*//" | sed 's/(\s*)\|,.*$//'`
http_01_port='54321'
combined_file="/etc/haproxy/certs/${domain}.pem"

le_path='/opt/letsencrypt'
exp_limit=30;

if [! -f $config_file]; then
        echo "[ERROR] config file does not exist: $config_file"
        exit 1;
fi

cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"
key_file="/etc/letsencrypt/live/$domain/privkey.pem"

if [! -f $cert_file]; then
	echo "[ERROR] certificate file not found for domain $domain."
fi

exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
datenow=$(date -d "now" +%s)
days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)

echo "Checking expiration date for $domain..."

if ["$days_exp" -gt "$exp_limit"] ; then
	echo "The certificate is up to date, no need for renewal ($days_exp days left)."
	exit 0;
else
	echo "The certificate for $domain is about to expire soon. Starting Let's Encrypt (HAProxy:$http_01_port) renewal script..."
	$le_path/letsencrypt-auto certonly --agree-tos --renew-by-default --config $config_file --http-01-port $http_01_port

	echo "Creating $combined_file with latest certs..."
	sudo bash -c "cat /etc/letsencrypt/live/$domain/fullchain.pem /etc/letsencrypt/live/$domain/privkey.pem > $combined_file"

	echo "Reloading $web_service"
	/usr/sbin/service $web_service reload
	echo "Renewal process finished for domain $domain"
	exit 0;
fi

```

(Yep, we’re finally on [https://metaruby.com](https://metaruby.com) 🙂 )

---

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [June 12, 2016, 6:57pm UTC](https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540/2 "2016-06-12T18:57:28Z")

</div>

Use backticks to execute commands, their output will come back as a string. If the file to execute isn’t found it will raise an error. For those situations you can do a `rescue nil`. All the grep/sed is doing is gsub. You will need to change the regex to be Ruby compatible (the ^ means something different). There are other ways to execute commands but backticks will work best for something like this.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://metaruby.com/user_avatar/metaruby.com/astonj/32/8_2.png) [@AstonJ](https://metaruby.com/u/AstonJ)\
**Post date:** [June 12, 2016, 9:20pm UTC](https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540/3 "2016-06-12T21:20:16Z")

</div>

Thanks. What about when what I want to run already contains backticks? Will it be an issue?

```auto
date -d "`openssl x509 -in /etc/letsencrypt/live/site.com/fullchain.pem -text -noout|grep "Not After"|cut -c 25-`" +%s

```

_Tried to open the same file with Ruby’s ‘openssl’ library but it says it’s not a pub or priv key. Edit: got it._

---

<div class="post-metadata">

**Author:** ![kofno](https://metaruby.com/user_avatar/metaruby.com/kofno/32/159_2.png) [@kofno](https://metaruby.com/u/kofno)\
**Post date:** [June 13, 2016, 3:58am UTC](https://metaruby.com/t/executing-shell-commands-from-a-ruby-script/540/4 "2016-06-13T03:58:34Z")

</div>

Use %x().
