Author Archives: MasonCloud

Process Checker (Running or Not?)

Published / by MasonCloud / Leave a Comment
# Name : service.chk
# URL:
# Purpose: A simple process checker. Find out if service is running or not.
# Tested on: Debian and RHEL based system only.
# ----------------------------------------------------------------------------
# Author: nixCraft
# Copyright: 2009 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 13/Mar/2013 - Added support for email and other enhancements
# Last updated: 05/Dec/2011 - Added support for binary path check
# ----------------------------------------------------------------------------
# Runn the script as below to only show "running" processes
# clear ; ./ | grep -v "not running"
# ----------------------------------------------------------------------------
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
sleep 1 ; chkconfig --list
echo "------------------------------------------------------"
## Change as per your distro
## Add binary list here
_chklist="/usr/bin/php-cgi /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/nginx /usr/sbin/httpd"
#_chklist="/usr/bin/php-cgi /usr/sbin/lighttpd /usr/sbin/named /usr/sbin/pgsqld /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/nginx /usr/sbin/httpd"
## yes | no
## Add your email id
## Do not change below
_running() {
 local p="${1##*/}"
 local s="true"
 $_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
 [[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
## Check if your service is running or not
for s in $_chklist
 _running "$s"
## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)";
 $_mail -s 'Service not found' "$_email" < "${_mess}";

Create AMI from EC2 Image

Published / by MasonCloud / Leave a Comment
# vim /root/bin/


# @Purpose Creates an image (AMI) of the given EC2 instance
# @Background Meant to be run as a cronjob. Requires that awscli is installed. Assumes that the
# instance running this command has the permission ec2:CreateImage assigned via IAM.
# @Usage: ec2-create-image <instance-id>

DATE=$(date +%Y-%m-%d_%H-%M)
AMI_NAME="WordPress Backup - $DATE"
AMI_DESCRIPTION="WordPress Backup - $DATE"

printf "Requesting AMI for instance $1...\n"
aws ec2 create-image --instance-id $1 --name "$AMI_NAME" --description "$AMI_DESCRIPTION" --no-reboot

if [ $? -eq 0 ]; then
 printf "AMI request complete!\n"

# /root/bin/ i-0b84b3b0de37cce1a >> /root/log/crontab.log 2>&1

# aws ec2 deregister-image --image-id i-0b84b3b0de37cce1a

Understand CURL and HTTP Headers

Published / by MasonCloud / Leave a Comment

curl is a command line tool which is used to transfer data over the internet. It began as a project by Daniel Stenberg to transfer data over HTTP but has now evolved into a very robust tool that transfers data not just over HTTP but also FTP, TELNET, IMAP, and many more.In this guide we will cover the basics of curl and look at how we can use it to retrieve and send data, and also take a deep dive and look at HTTP headers.


To follow this guide, you’ll need access to a Linux or Mac machine. You’ll need some familiarity with using the terminal to execute commands. If you aren’t familiar with using the terminal, feel free to take advantage of the resources available at Linux Academy to get up to speed. These resources are listed at the end of this guide.

Using curl

The best way to understand curl is to use it. This guide is an introduction-by-example. We’ll use curl to send requests to which is an HTTP client testing service. Let’s get started.

GET request

Fire up a terminal and execute the following command:

me@home:~$ curl

This command will fetch the HTML for the HTTPBin homepage. Notice that we’ve not mentioned the protocol or the HTTP method. They’ve defaulted to HTTP and GET, respectively.

HEAD request

Sometimes you may want to see just the headers that the server would return when it’s issued a GET request. In such a case, we’d use the HEAD method. The head request is issued with -I (capital i) or –head.

me@home:~$ curl -I    
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 01 Dec 2016 18:50:56 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12150
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Connection: keep-alive
Verbose output

Sometimes, you may want to debug a problem because the curl command hasn’t returned what it was supposed to or just hasn’t worked at all. In such cases, you can use the verbose mode using -v or –verbose to get more output from curl. Let’s modify the previous HTTP request to become verbose.

me@home:~$ curl -Iv

Now there’s a lot more output in the terminal. Let’s see what it means.

Lines prefixed with an asterisk (*) show additional work curl has done. For example:

* Rebuilt URL to:
*   Trying
* Connected to ( port 80 (#0)

The first line shows that curl added a slash at the end of the URL.

The second line shows that curl has resolved the URL to an IP address.

Finally, the third line shows that curl has connected to the URL on port 80.

Lines prefixed with a greater-than (>) sign show the data curl has sent to the server. For example:

> HEAD / HTTP/1.1
> Host:
> User-Agent: curl/7.47.0
> Accept: */*

Lines prefixed with a less-than (<) sign show the data curl has received from the server. For example:

< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx
Server: nginx
Multiple requests

You can also send requests to multiple URLs by separating them by space. Here’s how:

me@home:~$ curl

The above example will send two GET requests, both to the same URL, one after the other. You should see two little teapots printed in your terminal.

POST request

You can send a POST request by using the -d (or –data) flag. It͛s up to you to properly URL encode the data or specify it as JSON. We’ll see the examples for both of those. Here’s how you’d send URL encoded data as a part of the POST body

me@home:~$ curl -d 'name=john+doe&course=linux'

The response sent by HTTPBin will simply echo what you͛ve sent.

To send JSON:

me@home:~$ curl -d '{"name":"John Doe","course":"Linux"}'

Notice that in both the examples, the data is enclosed in single quotes. Using double quotes is okay but it may cause problems if the body also contains double quotes like JSON. Using single quotes is the safer approach.

Setting headers

You can set headers by using the -H flag. In our previous example, we’ve sent JSON without setting the Content-Type header to application/json. Here’s the previous example modified to set the header:

me@home:~$ curl -H 'Content-Type: application/json' -d '{"name":"John Doe","course":"Linux"}'

You set the header as a key-value pair consisting of the header name followed by a colon and the value.

Posting form data

You can send form data by using the -F flag. Use this flag for every form field that you want to send. Here’s how you’d send form data:

me@home:~$ curl -F "name=John Doe" -F "course=linux"

Notice that we’ve written the name as John Doe without a plus sign in between. The response received will have the Content-Type header set to multipart/form-data.

Uploading files

Using curl, you can upload files both as a part of a form field or to a ReST API endpoint. We’ll first create a file and then upload it both as a part of a form and to a ReST endpoint.

me@home:~$ echo "hello, world" >> file.txt     
me@home:~$ curl -F "file=@file.txt"

Since we’re using the -F flag, the file is uploaded as a part of the form. You specify the file to upload by an @ followed by the path to the file. Since our file is in the current directory, we just specify the file name.

Uploading to a ReST endpoint is similar. We just use the -d flag instead.

me@home:~$ curl -d @file.txt

Notice now that there are no quotes around file.txt. This is because we want the contents of the file to be the body of the POST request and not string “@file.txt” itself.

PUT request

ReST APIs use the PUT request to update an existing resource that the server manages. Making a PUT request with curl is similar to making a POST request. Here’s how you’d make a PUT request:

me@home:~$ curl -X PUT -d '{"name":"Jane Doe"}'
DELETE request

Similarly, ReST APIs use DELETE request to delete an existing resource. Here’s how to make a delete request:

me@home:~$ curl -X DELETE
Basic authentication

Curl includes support for basic authentication. With basic authentication, you send your username and password as a part of the URL. There’s a two ways in which you can do this in curl. Here’s how:

me@home:~$ curl -u john:abc123
me@home:~$ curl

In the first one you specify the username and password using the -u (short for –user) flag and curl appends this to the URL you provide. In the second case, you do it manually. Basic authentication does not encrypt your credentials in any way and is thus insecure unless used with HTTPS.

Setting cookies

You can set cookies using the -b (short for –cookie) flag. You specify the key and the value for the cookie with the flag. Here͛s an example showing how to set two cookies:

me@home:~$ curl --cookie "name=john;course=linux beginner"

Notice that cookies are separated by a semicolon and that you do not need to encode spaces as a plus sign.This brings us to the end of the guide on using curl. The next part of the guide will introduce you to HTTP headers.

HTTP headers

The HTTP protocol allows a client to send a request to a server. With every HTTP request and response, there are associated headers that provide some more information. Let’s make a HEAD request and see what we get:

me@home:~$ curl -Iv     
*   Trying
* Connected to ( port 80 (#0)
> HEAD /get HTTP/1.1
> Host:> User-Agent: curl/7.49.1
> Accept: */*
< HTTP/1.1 200 OKHTTP/1.1 200 OK
< Server: nginxServer: nginx
< Date: Wed, 14 Dec 2016 16:23:11 GMTDate: Wed, 14 Dec 2016 16:23:11 GMT
< Content-Type: application/json
Content-Type: application/json
< Content-Length: 186
Content-Length: 186
< Connection: keep-alive
Connection: keep-alive
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
Access-Control-Allow-Credentials: true

In the output that’s printed, the following are headers: Host, User-Agent, Accept, Server, Content-Type, Content-Length, Connection, Access-Control-Allow-Origin, and Access-Control-Allow-Credentials.

Headers can be classified as request and response headers that contain information about the request and response, respectively, or as general headers that contain information about both request and response. There’s also another class of headers known as entity headers that contain more information about an entity, such as a file (i.e. it’s length and MIME-type). Here’s some of the most common headers and what they mean.

Authorization header

The Authorization header is used to provide authentication information such as bearer tokens.The server then uses this information to find out if the request should be processed further or not, depending on the validity of the authentication information provided. Example:

Authorization: Bearer 3beca038a248ff027d0445342fe285
Cache-Control header

The Cache-Control header decides how long applications, such as the browser, can keep a local copy of the data. This helps improve the efficiency since it helps avoid a round-trip to the server. Cache-Control has various directives that control various aspects of caching, such as the length of time data may be cached or if it shouldn’t be cached at all. Example:

Cache-Control: max-age=100
Expires header

The Expires header is also used for caching and specifies the date and time after which a particular cached resource is considered stale. When both Expires and Cache-Control header are set, the Cache-Control header takes higher priority. Example:

Expires: Wed, 21 Oct 2015 07:28:00 GMT
Connection header

The Connection header specifies whether a connection should last for the duration of the request or if it should stay open, allowing it to be reused for subsequent requests. The default value is “close” which creates a connection that only lasts for the duration of the request. Setting the header to “keep-alive” allows it to be persistent. Example:

Connection: keep-alive

Accept header

The Accept header tells the server what kind of content the client is expecting. The value of this header is the MIME type of the content. Example:

Accept: application/json
Cookie header

The cookie header contains all the cookies and their values. Cookies are used to identify the users, maintain sessions, and so on. Example:

Cookie: name=John;course=Linux
Content-Length header

The Content-Length header specifies the length of the content in bytes. Example:

Content-Length: 111020
Content-Type header

The Content-Type header tells the client the MIME-type of the content that it has received. Example:

Content-Type: application/json

There are a lot more headers than that can be covered in a simple guide, which brings us to the end of the guide on curl and HTTP headers. If you are eager to learn more about Linux command line, please check out the following section.