Shell Script to reboot multiple servers.

Here is simple bash script where you can add the server names in the command line and then it will go on rebooting every server which you have listed.

Script Usage:-

[root@server /]# ./ appserver1 sqlserver1 dataserver1

Above command will reboot appserver1 sqlserver1 and dataserver1, if you have not setup ssh keys it will ask for password.

To use this script create a file called and following contents.

for server in "${@}"
scriptPath=$(dirname $0)
command=`base64 -w0 $script`
ssh -t $server "echo $command | base64 -d | sudo bash"

Create a another file named boot.txt in the same location where you have created

for ((i=03; i>0; i--)); do sleep 1 ; printf "\rREBOOTING SERVER in $i Second. PRESS CTRL + C to cancel " ; done
shutdown -rf now

Save both the files, give execute permissions to and start rebooting.

It’s just not reboot script but you can add any commands which you want to run on multiple Linux servers in the file boot.txt

There are many other ways to do the same task but this works best for my environment. Add in comments what works best for you.

Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[root@server ]# echo "JustGeek" | cut -b 1

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[root@server ]# echo "JustGeek" | cut -b 1,3,5
[root@server ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below


The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[root@server ]# cat names.csv | cut -d ',' -f 1
[root@server ]#

You can comma “,” separate for multiple entries. Example.

[root@server ]# cat names.csv | cut -d ',' -f 1,2

If you want display age then you can just specify position with -f. Example

[root@server ]# cat names.csv | cut -d ',' -f 3

And if you want to Display City, then:

[root@server ]# cat names.csv | cut -d ',' -f 4

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[root@server ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
[root@server ]#

How to Check “cut” command version

 [root@server ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
 Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Difference between Softlink and Hardlink

Here we will see what’s exactly is soft-link first.

  1. Symbolic Link – Links the files and directories located across diferent file systems.
  2. A Softlink has different inode number, Original File and Soflinked file don’t have same inode number.
  3. Softlink can be created across different Filesystems.
  4. Deleting original folder will break Symlink

How to Identify Softlink
You can do ls -ltr, you will see a “l” in the start of the permissions.

[root@server ]# ls -ltr
lrwxrwxrwx 1 root root 24 Dec  9 08:57 symlink -> /usr/local/orginalfolder

How to create a softlink
Create a directory

[root@server ]# mkdir /usr/local/orginalfolder

and now you can create symlink using.

[root@server ]# ln -s /usr/local/orginalfolder symlink

In the output below, you will notice that Soft link and Orginal folder are different.

[root@server ]# stat symlink
  File: ‘symlink’ -> ‘/usr/local/orginalfolder’
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 256014      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:57:22.942621767 +0000
Modify: 2019-12-09 08:57:19.394593754 +0000
Change: 2019-12-09 08:57:19.394593754 +0000
 Birth: -

[root@server ]# stat /usr/local/orginalfolder
  File: ‘/usr/local/orginalfolder’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd01h/64769d    Inode: 395887      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:56:45.194323755 +0000
Modify: 2019-12-09 08:56:44.055314766 +0000
Change: 2019-12-09 08:56:44.055314766 +0000
 Birth: -

What’s the Hard-link.

  1. The main thing is you can’t create Harlink for Directories.
  2. Unlike Softlink, Hardlink can’t be created across different file-systems.
  3. Hardlinks share same inode number.

How to create a Hardlink

[root@server ]# touch originalFile.txt
[root@server ]# ln originalFile.txt hardLink.txt

Now you see a hard-link is created with same-content.

[root@server ]# cat originalFile.txt hardLink.txt
Hey Buddy!!
Hey Buddy!!

You will also notice that both have same inode number.

[root@server ]# stat originalFile.txt hardLink.txt
  File: ‘originalFile.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -
 File: ‘hardLink.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -

and if you remove original file it does not affect Hardlink

[root@server ]#  rm -fv originalFile.txt
removed ‘originalFile.txt’

[root@server ]#  ll
total 4
-rw-r--r-- 1 root root 12 Dec  9 09:07 hardLink.txt

[root@server ]#  cat hardLink.txt
Hey Buddy!!
[root@server ]# 

Error: /bin/bash^M: bad interpreter

How to fix below error?

[root@server /]# ./script test
 bash: ./script: /bin/bash^M: bad interpreter: No such file or directory

There nultiple way to fix this error, the problem is with the file format, you need to convert it to Unix format and your error should be gone.
You can fire the command below to fix.

[root@server /]# dos2unix filename

You can try steps below

 Open file, vi script
 then press ESC and enter :set fileformat=unix
 save file and try to run script again. 

How to redirect a domain name using .htaccess

If your domain name is changed for some reason and you want users/search engines to redirect to new Location then add the below code in .htaccess file of your website.


If you want to redirect to

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)${REQUEST_URI} [R=301,L]

After you add above code in .htaccess file it will as also redirect links.
Example: will redirect to